Experiencing error " Can't access attributes on a primitive-typed value (string)."

I am using terraform vpc module to create vpcs and subnets and I have written a module for adding routes “aws_route” as it has to be used multiple times across my code. When i tried to pass the route table ids to from terraform module to one of the variables in my module i am experiencing error " Error: Unsupported attribute. Can’t access attributes on a primitive-typed value (string)." I am new to terraform and not sure what exactly the meaning of this error and what needs to be done to get rid of the error. Please help

Hi @kvsamv!

Would you mind sharing the full text of the error message (including the source snippet and detail paragraphs) and the relevant parts of the configuration that relate to it?

This message is telling you that you’ve tried to use the attribute access syntax, like foo.bar, but foo is a string and so isn’t something that could possibly have an attribute bar. I can’t say any more than that without seeing more details, but hopefully that helps as a starting point to understand what’s happening.

Error: Unsupported attribute
│
│   on main.tf line 48, in module "testvpc-test-transit-gateway-attachment":
│   48:   subnet_route_table_ids = "${module.test-vpc.private_route_table_ids[0].id}"
│     ├────────────────
│     │ module.test-vpc.private_route_table_ids[0] is "rtb-xxxxxx"
│
│ Can't access attributes on a primitive-typed value (string).
╵

I have provided the output as requested. Also, when i changed the code as below , i no longer see the error but what i found is only one route table being shown in terraform plan output instead of two.

count = length("${module.test-vpc.private_route_table_ids}") > 0 ? 1 : 0
subnet_route_table_ids = "${module.test-vpc.private_route_table_ids[count.index]}"

This is now resolved and thread can be closed. Thank you for the assistance.

What did you do to resolve this issue?

Hi @patrick.lynch,

I’m not @kvsamv so I can’t directly answer what they did but based on the response to my earlier question I would’ve recommended removing the .id part of the subnet_route_table_ids expression, like this:

  subnet_route_table_ids = module.test-vpc.private_route_table_ids[0]

Based on the earlier response it seems like they did something like that and it worked. (Also changed 0 to count.index, but that’s a separate concern from the error this was originally discussing.

The reason for this is that module.test-vpc.private_route_table_ids already seems to be a list of ID strings, not of route table objects, and so there is no need to access .id: the string value is already that ID.

2 Likes

A post was split to a new topic: Terraform console: Can’t access attributes on a primitive-typed value (string)