Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Incorrect attribute value type module.network.private_subnets[0] is tuple with 3 elements

I am running into an issue while trying to upgrade from terraform 11 to terraform 12. I was previously using the following syntax to retrieve the 3rd element from a list of ids from a module. The module output is like so:

# Subnets
output "private_subnets" {
  description = "List of IDs of private subnets"
  value       = ["${aws_subnet.private.*.id}"]
}

Previously, this worked with terraform 11

subnet_id              = "${element(module.network.private_subnets,3)}"

I thought that I could use the index of 2 to get the same results, but I get the following error:

Error: Incorrect attribute value type

  on terraformfile.tf line 65, in resource "aws_instance" "myinstance":
  65:   subnet_id              = module.network.private_subnets[2]
    |----------------
    | module.network.private_subnets[2] is tuple with 3 elements

Any help with this would be greatly appreciated.

like image 949
Evan Gertis Avatar asked Aug 06 '26 23:08

Evan Gertis


1 Answers

The answer from @Marcin holds good but I hit a very similar error in a slightly different situation and I took a different approach to resolve it.

Lets say you have a aws_security_group_rule rule and each of the data -sourced item in the list below i.e data.aws_subnet.app1_subnets.*.cidr_block and data.aws_subnet.app2_subnets.*.cidr_block are a tuple with lets say X elements - You might hit an error -

Error: Incorrect attribute value type

  on stack.tf line 44, in resource "aws_security_group_rule" "ds_security_group_ingress_rule":
  44:   cidr_blocks = [
  45:     data.aws_subnet.app1_subnets.*.cidr_block,
  46:     data.aws_subnet.app2_subnets.*.cidr_block
  47:   ]
    |----------------
    | data.aws_subnet.app1_subnets.*.cidr_block is tuple with 3 elements
    | data.aws_subnet.app2_subnets.*.cidr_block is tuple with 3 elements

Inappropriate value for attribute "cidr_blocks": element 0: string required.

I had to use the flatten function to resolve the issue which quite literally flattens the content..

Change the below:

cidr_blocks = [
data.aws_subnet.app1_subnets.*.cidr_block,
data.aws_subnet.app2_subnets.*.cidr_block
]

as

cidr_blocks = flatten([
data.aws_subnet.app1_subnets.*.cidr_block,
data.aws_subnet.app2_subnets.*.cidr_block
])
like image 58
kiran01bm Avatar answered Aug 09 '26 17:08

kiran01bm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!