Search code examples
typescriptterraformcdktf

Data sources in CDKTF


I am trying to replicate one of my simple stacks from HCL to TypeScript (using CDKTF), I am having issues setting up a data source to get a list of AMIs. This is what I am trying to do:

const amis = new ec2.DataAwsAmiIds(this, "ami-ids", {
  owners: ["amazon"],
  nameRegex: "^amzn2-ami-hvm-*-x86_64-ebs",
  filter: [{
    name: "name",
    values: ["amzn2-ami-hvm-*-x86_64-ebs"]
  }]
});

I then try to reference this like so:

const instance = new ec2.Instance(this, "instance", {
  instanceType: "t2.micro",
  ami: `${amis.id}`,
  ...

The issue I am having is that this value will be a Token (${TfToken[TOKEN.1]}), how to I get the string value from this in order to be able to pass it in? Thanks.

The same thing applies to generating a key pair using the TLS provider, here is the complete source code.


Solution

  • ec2.DataAwsAmiIds will have attribute of ids; during synth time you'll see it represented as a Token but it will render as the terraform reference in the terraform JSON. The reason it is a Token is because it represents information that will only be known during/after Terraform runtime, can see more info here: https://www.terraform.io/cdktf/concepts/tokens#tokens