Reputation: 577
Happy Friday,
Using the below data set (as viewed from an output) how would I obtain the list of CIDR blocks to feed into the "cidr_blocks" (a list) attribute of the "aws_security_group" resource?
Changes to Outputs:
+ subnet_ids = {
+ subnet-<id_hidden> = {
+ arn = "<arn_hidden>"
+ assign_ipv6_address_on_creation = false
+ availability_zone = "<az_hidden>"
+ availability_zone_id = "<az_id_hidden>"
+ available_ip_address_count = 1000
+ cidr_block = "<cidr_hidden>" <== Want this
+ customer_owned_ipv4_pool = ""
+ default_for_az = false
+ filter = null
+ id = "<id_hidden>"
+ ipv6_cidr_block = null
+ ipv6_cidr_block_association_id = null
+ map_customer_owned_ip_on_launch = false
+ map_public_ip_on_launch = false
+ outpost_arn = ""
+ owner_id = "<owner_hidden>"
+ state = "available"
+ tags = {
+ "Environment" = "dev"
}
+ vpc_id = "<id_hidden>"
}
+ subnet-<id_hidden> = {
...
+ cidr_block = "<cidr_hidden>" <== Want this
...
...many more subnets...
EDIT (code blocks generating output):
data "aws_subnet" "management_vpc_private_subnets" {
for_each = data.aws_subnet_ids.management_vpc_private_subnet_ids.ids
id = each.value
}
data "aws_subnet_ids" "management_vpc_private_subnet_ids" {
vpc_id = <vpc_id_hidden>
filter {
name = "tag:Subnet"
values = ["private"]
}
}
output "subnet_ids" {
value = data.aws_subnet.management_vpc_private_subnets
}
Thank you!
Upvotes: 0
Views: 1825
Reputation: 1048
This exact example discussed over here in Terraform docs.
You should be able to do something like this.
data "aws_subnet_ids" "example" {
vpc_id = <vpc_id_hidden>
filter {
name = "tag:Subnet"
values = ["private"]
}
}
data "aws_subnet" "example" {
for_each = data.aws_subnet_ids.example.ids
id = each.value
}
output "subnet_cidr_blocks" {
value = [for s in data.aws_subnet.example : s.cidr_block]
}
Resulting output as follows
subnet_cidr_blocks = [
"10.0.8.0/21",
"10.0.136.0/21",
"10.0.72.0/21",
"10.0.104.0/21",
"10.0.40.0/21",
]
Upvotes: 2