Reputation: 10349
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?
Upvotes: 1
Views: 1057
Reputation: 1650
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.
Upvotes: 1