Tokens - how to read value from array

Hi there!
I use typescript for developing script.

I have problem with reading value from remote state.
As we can read from tokens after command cdktf synth string “#{TOKEN[TOKEN.9]}” will resolve the token to ${module.<module id>.public_subnets} .
I try to create some resources.

for (let index = 0; index < (blue.getList(“asg_ids”)).length; index++) {

    new AutoscalingAttachment(this, 
                              autoscalingGroupName: blue.getList("asg_ids")[index],
                              albTargetGroupArn: nlb.getString('tg_arn_stage_c')

where: blue(list of asg_ids) and nlb data from remote state

After command cdktf synth I`ve got file like this
“resource”: {
“aws_autoscaling_attachment”: {
“a911trafficcontroltypescript_a911bluetonlb_6CDD2D28”: {
“autoscaling_group_name”: “#{Token[TOKEN.1]}”,

As we can see Token is not replaced and after command terraform plan I got a new plan like this:

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

aws_autoscaling_attachment.stagea_9F839440 must be replaced

-/+ resource “aws_autoscaling_attachment”
“stagea_9F839440” {
alb_target_group_arn = “arn:xxxxx”
~ autoscaling_group_name = “stage-a-blue” -> “#{Token[TOKEN.1]}” # forces replacement
~ id = “00000001” -> (known after apply)

Plan: 1 to add, 0 to change, 1 to destroy.

but I’ve read an existing remote state and I expect that it sould be sync and I’ll get notification that it syncs No changes

How to read a value from array by using Tokens?
Is it possible?

Is blue a list coming from remote state? If so, I’m not sure what you’re doing with the for loop will ever work since the contents aren’t know until diff/deploy time, i.e. the length won’t be correct.

I think having support for count/for_each using some sort of tokens would be very valuable, but I’m pretty sure that at this time you’ll need to use the approach mentioned here.

Yes, the blue is the list from remote state and a can not get a value from the list of ids.
After command cdktf synth into file i can see only this string -> autoscaling_group_name : “#{Token[TOKEN.1]}”. it is not valid.

This approach here gives me the same result -> autoscaling_group_name : “#{Token[TOKEN.1]}” . it is not valid.

My main question is How I can get from array particular item by using this approach here or another one ?

My code by using this approach
const a911_blue_to_nlb = new AutoscalingAttachment(this,
autoscalingGroupName: “temp”,
albTargetGroupArn: Token.asString(a911_nlb.getString(‘tg_a911_grpc_arn_stage_c’))

a911_blue_to_nlb.addOverride('count', (a911_blue.getList("asg_ids")).length);
a911_blue_to_nlb.addOverride('autoscaling_group_name', 'element(a911_blue_to_nlb.getList("asg_ids")), count.index)';

I got this result: “autoscaling_group_name”: "element(a911_blue_to_nlb.getList(“asg_ids”)), count.index)", into It is not valid.

I have two different situations:
1. if I need to read string from remote state like this route_53.getString(‘zone_id’)
I got into “alb_target_group_arn”: “${}”. It is correct!!!

  1. if I need to read item of list from remote state like this
    autoscalingGroupName: remote_state.getList(“asg_ids”)[0]
    I got into “autoscaling_group_name”: “#{Token[TOKEN.0]}”. it is not correct!

It seems as though a handful of bugs exist with accessing lists, but it should be possible to work around them.

You could try treating as string since that seems to be working better:

a911_blue_to_nlb.addOverride('count', `length(${a911_blue.getString("asg_ids")})`);
a911_blue_to_nlb.addOverride('autoscaling_group_name', `element(${a911_blue.getString("asg_ids")}, count.index)`)';

Otherwise, you may have to resort to even further manual specification:

a911_blue_to_nlb.addOverride('count', `length(\${data.terraform_remote_state.${a911_blue.friendlyUniqueId}.outputs.asg_ids})`);
a911_blue_to_nlb.addOverride('autoscaling_group_name', `element(\${data.terraform_remote_state.${a911_blue.friendlyUniqueId}.outputs.asg_ids}, count.index`)';

Thanks for your attension! :handshake:

it works for me.

for (let index = 0; index < (a_green.getList(“asg_ids”)).length; index++) {

    new AutoscalingAttachment(this, 
                 "a_green_to_nlb" + index,{
                 count: a_green.getList("asg_ids").length,  
                 autoscalingGroupName: `\${data.terraform_remote_state.${a_green.friendlyUniqueId}.outputs.asg_ids[${index}]}`, 
                 albTargetGroupArn: Token.asString(a_nlb.getString('tg_a_arn_stage_c'))