When referencing that output with terraform_remote_state, I get the following error:
β Error: Incorrect attribute value type
β
β on peering-sharedservices.tf line 117, in resource "aws_route" "test":
β 117: destination_cidr_block = data.terraform_remote_state.network_test.outputs.private_subnets_cidr_blocks
β βββββββββββββββββ
β β data.terraform_remote_state.network_test.outputs.private_subnets_cidr_blocks is tuple with 1 element
β
β Inappropriate value for attribute "destination_cidr_block": string required.
β Error: Incorrect attribute value type
β
β on peering-sharedservices.tf line 117, in resource "aws_route" "test":
β 117: destination_cidr_block = data.terraform_remote_state.network_test.outputs.private_subnets_cidr_blocks
β βββββββββββββββββ
β β data.terraform_remote_state.network_test.outputs.private_subnets_cidr_blocks is list of string with 3 elements
β
β Inappropriate value for attribute "destination_cidr_block": string required.
β΅
β·
β Error: Incorrect attribute value type
β
β on peering-sharedservices.tf line 117, in resource "aws_route" "test":
β 117: destination_cidr_block = data.terraform_remote_state.network_test.outputs.private_subnets_cidr_blocks
β βββββββββββββββββ
β β data.terraform_remote_state.network_test.outputs.private_subnets_cidr_blocks is list of string with 3 elements
β
β Inappropriate value for attribute "destination_cidr_block": string required.
β΅
β·
β Error: Incorrect attribute value type
β
β on peering-sharedservices.tf line 117, in resource "aws_route" "test":
β 117: destination_cidr_block = data.terraform_remote_state.network_test.outputs.private_subnets_cidr_blocks
β βββββββββββββββββ
β β data.terraform_remote_state.network_test.outputs.private_subnets_cidr_blocks is list of string with 3 elements
β
β Inappropriate value for attribute "destination_cidr_block": string required.
outputs.tf:
output "private_subnets_cidr_blocks" {
value = one(module.vpc[*].private_subnets_cidr_blocks)
}
As destination_cidr_block is expecting a single string I think you actually want destination_cidr_block = data.terraform_remote_state.network_test.outputs.private_subnets_cidr_blocks[count.index]
Your expression has two parts that I think suggest the problem:
The attribute name public_subnets implies that this is a collection of multiple subnets, rather than a single subnet.
The operator [*] constructs a list of values of the attribute on its right side.
Therefore it seems that you are constructing a list of collections rather than just a single collection, which seems to agree with the error message because your first element in that case would be a collection rather than a string as required.
Iβm not sure exactly what you were intending to assign to this argument, but since this argument seems to expect a set of strings perhaps the right idea would be to flatten the result (discarding the extra level of list) and then use the toset function to convert the result to be a set of strings.
In any situation where youβve used a condition to select between zero or one instances of something, you need to describe to Terraform how it should handle the situation where there are zero instances.
A concise way to do that is to use the function one, which transforms a list that might have zero or one elements into a single value that might be null. For example:
output "public_subnets" {
value = one(module.vpc[*].public_subnets)
}
In this case this reads a bit strangely because itβs returning one set of subnets, so itβs the list of sets that the function is changing, not the inner set itself.
The result is therefore either a set of strings describing public subnets or it is null, depending on whether this module is enabled.
If you need to use each of these subnets separately, such as if you are declaring something using for_each over the subnets, then the above expression should work as a for_each expression directly, and then you can use each.value inside the resource configuration to obtain the one subnet ID for that particular resource instance.