Python: TerraformLocal and expression

Hello,

I didn’t find any examples of using TerraformLocal in Python.

I have a Terraform local variable on HCL and unable to do the same on TF CDK in Python.

h_names = formatlist("%s.${trimsuffix(data.aws_route53_zone.dns_zone.name, ".")}", random_id.hostname.*.dec)
p_ips = "${join(", ", [for instance in aws_instance.ec2test : instance.private_ip])}"

This code doesn’t work and raise an error:


        h_names = TerraformLocal(self,
                                      id="h_names ",
                                      expression="formatlist(\"%s.${{trimsuffix({zone}, \".\")}}\", {hostname}.*.dec)".format(
                                          zone=self.zone.name, hostname=self.hostname.friendly_unique_id)
                                      
                                      )

I’ve already do the same with Stack.add_overrride(…), and the result is the same.

Does anyone successfully implement expressions like this on TF CDK with Python?

Thanks for the advice!

I believe you’ll need to wrap the expression value:${formatlist(...)} and use fqn rather than friendly_unique_id. You may also need to manually create the zone with self.zone.fqn + '.name'.