1

I'm currently upgrading my cdktf dependencies in typescript and running into some issues during the upgrade. I currently have the following code which works perfectly on version 0.8.6

    const dataAwsAvailabilityZonesAll =
      new aws.datasources.DataAwsAvailabilityZones(this, "all", {});

    const publicSubnet = new aws.vpc.Subnet(this, "pub_subnet", {
      availabilityZone: `\${${dataAwsAvailabilityZonesAll.fqn}.names[count.index]}`,
      cidrBlock: "${cidrsubnet(aws_vpc.vpc.cidr_block, 8, count.index)}",
      mapPublicIpOnLaunch: true,
      vpcId: "${aws_vpc.vpc.id}",
    });
    publicSubnet.addOverride(
      "count",
      `\${length(${dataAwsAvailabilityZonesAll.fqn}.names)}`
    );

However, when I upgrade to version 0.12 I start getting errors. I've found that the fqn isn't easily available in 0.12 and that I should instead use Fn.lookup and Fn.element. One thing I'm specifically having trouble with is how to use count.index when using Fn.element because the second parameter to Fn.element is a number.

I've found the docs for iterators which are new in version 0.12. The docs mention

You cannot access the index of items when iterating over lists. This is because CDKTF implicitly converts lists to sets when iterating over them, but Terraform requires sets for iteration. This behavior prevents Terraform from accidentally deleting and recreating resources when their indices change. If you need an index, use an escape hatch with the count.index property.

However, it's not clear at all how to use an escape hatch with the count.index property. I thought I might be able to do something like

    const dataAwsAvailabilityZonesAll =
      new aws.datasources.DataAwsAvailabilityZones(this, "all", {});
    const iterator = TerraformIterator.fromList(dataAwsAvailabilityZonesAll.names)
    const index = iterator.count.index

but the iterator doesn't have a count property to be used as an escape hatch.

How can I migrate the old cdktf code which was working in version 0.8 to 0.12?

Paymahn Moghadasian
  • 9,301
  • 13
  • 56
  • 94

1 Answers1

1

I wasn't able to use an escape hatch to reference the index property, however using the index function solves the problem, e.g.:

    const zones = new DataAwsAvailabilityZones(this, "all", {});
    const iterator = TerraformIterator.fromList(zones.names)
    
    const subnet = new Subnet(this, "subnet", {
      forEach: iterator,
      vpcId: "XXXXX",
      availabilityZone: iterator.value,
      cidrBlock: Fn.cidrsubnet("172.31.0.0/16", 8, Fn.index(zones.names, iterator.value))
    });

Hope this helps.

javierlga
  • 1,409
  • 9
  • 14
  • Thanks for the help but I'm not sure how to use this...I'm not trying to iterate and create a new object for each item in the iteration. I just need to get the `count.index` – Paymahn Moghadasian Aug 02 '22 at 18:03
  • 1
    Well, the escape hatch to add the `count` attribute in the code you shared does that, creating X number of subnets based on `count` and `length` function. – javierlga Aug 02 '22 at 18:09
  • 1
    Basically, the code that I shared with you does the same as yours, instead of using `count.index` it uses the index function to get the index of an element. – javierlga Aug 02 '22 at 18:15
  • hmm, maybe I'm misunderstanding but it seems to me that your code block will create one subnet for each item in the iterator (for each zone name). I just need to create a single subnet. Am I misunderstanding something? – Paymahn Moghadasian Aug 03 '22 at 16:13
  • 1
    Yes, my code will create one subnet per availability zone, it will do the same as yours, the only difference is that your code uses `count` and `length`, are you familiar with Terraform? – javierlga Aug 03 '22 at 16:31
  • 1
    You’re setting an escape hatch to use count and length `publicSubnet.addOverride` that’s how CDKTF calculates how many subnets will be created. – javierlga Aug 03 '22 at 16:34
  • Ah, I guess I'm not familiar with terraform! I'm new to the project I'm working on and I thought the code I had above would create a single subnet, not a collection of them. Let me go test your solution and see how incorrect my understanding was – Paymahn Moghadasian Aug 12 '22 at 14:44