简体   繁体   中英

How to reference resources created by for_each in Terraform

I have created multiple subnets [private/public] by using for_each in Terraform as below

resource "aws_subnet" "public" {
  for_each = toset(data.aws_availability_zones.azs.names)
  vpc_id = aws_vpc.vpc.id
  cidr_block = cidrsubnet(var.private_ranges,1 ,index(data.aws_availability_zones.azs.names, each.value))
  availability_zone = each.value
  map_public_ip_on_launch = true
  tags = merge(local.common_tags, map("Name", join("-", [local.prefix, index(data.aws_availability_zones.azs.names,each.value )]),
  "Availibility Zone", each.value, "Type", "Public"))

  lifecycle {
    prevent_destroy = false
  }
}

resource "aws_subnet" "private" {
  for_each = toset(data.aws_availability_zones.azs.names)
  vpc_id = aws_vpc.vpc.id
  cidr_block = cidrsubnet(var.private_ranges,1 ,index(data.aws_availability_zones.azs.names, each.value))
  availability_zone = each.value
  tags = merge(local.common_tags, map("Name", join("-", [local.prefix, index(data.aws_availability_zones.azs.names,each.value )]),
  "Availibility Zone", each.value, "Type", "Private"))

  lifecycle {
    prevent_destroy = false
  }
}

I need to reference these subnets in other resource aws_default_network_acl

resource "aws_default_network_acl" "default" {
  default_network_acl_id = aws_vpc.vpc.default_network_acl_id
  subnet_ids = <can you tell me how to reference the subnet ids here>
........

我使用以下解决方案做到了,

subnet_ids = concat([for subnet in aws_subnet.private: subnet.id], [for subnet in aws_subnet.public: subnet.id])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM