For in expression with CDK TF construct

I have a custom module, which returns a single output - instance_mappings

        const instanceMappingsOutput = new TerraformHclModule(this.stack, `${this.serviceName}-instance-type-filtering`, {
            source: resolve(__dirname, "./instance-type-filtering"),
            variables: {
                gpu_type: "V100"
            }
        }).get("instance_mappings");

The output is an array of objects, i.e.

[
{
 instance_type: "some_instance_type",
gpu_count: 1,
has_instant_storage: true
}
]

Now, I want to use the output to populate override property, which is an array, of CDK TF AutoScalingGroup

new AutoscalingGroup(this.stack, `${this.serviceName}-asg`, {
            name: `${this.serviceName}-asg`,
            ...
            mixedInstancesPolicy: {
                ...
                launchTemplate: {
...
                    override: 
                },
            }
            })
        })

In pure TF I want something like this:

override: [ for item in instanceMappingsOutput: {
instance_type: item.instance_type, 
weight: item.gpu_count,
launch_template_tpecification: {
                                launch_template_id: ${item.has_instant_storage == true ? someTemplate.id : anotherTemplate.id}
                            }
}]

but I can’t figure out how to do this in CDK TF

Hi @Truefiber :wave:

As the values of your Terraform module will only be available at runtime (i.e. plan / apply) and not during synth, you’ll need to use an Iterator. More specifically Iterator.dynamic() should be what you’re looking for.

1 Like

Thanks! Worked like a charm!

1 Like