Home > OS >  Terraform tuple to id for subnet
Terraform tuple to id for subnet

Time:05-12

I got following output:

output "private_subnets" {
  description = "List of IDs of private subnets"
  value       = module.vpc.private_subnets
}

which returns subnet ids like: [subnet-1***, subnet-2***, subnet-3***]

How can I use this in nlb resource?

dynamic "subnet_mapping" {
    #for_each = { for k,v in module.vpc.private_subnets : k => v}
    for_each = [for s in module.vpc.private_subnets : s] 
    content {
      subnet_id     = s 
    }
  }

I have tried s.id, s.value, nothing works. I get following errors: A reference to a resource type must be followed by at least one attribute access, specifying the resource name.

CodePudding user response:

It should be:

dynamic "subnet_mapping" {
    for_each = toset(module.vpc.private_subnets)
    content {
      subnet_id = subnet_mapping.value
    }
  }
  • Related