Search code examples
amazon-web-servicesterraformterraform-provider-awsterraform-modules

How to get all output values from map variable in terraform?


I'm trying to extract id variable from child module to pass it to another child module. My output block in networking module is looking like

output "database_subnets_properties" {
  description = "List of all* Database Subnets Properties"
  value = tomap({
    for k, subnet in aws_subnet.database : k => {
      arn = subnet.arn
      id  = subnet.id
    }
  })
}

I need to pass IDs from this output to lambda vpc_config block. VPC config block looks like

  vpc_config {
    subnet_ids         = var.database_subnets_ids
    security_group_ids = [aws_security_group.for_lambda.id]
  }

If I pass them like

database_subnets_ids = [module.networking.database_subnets_properties[0].id, module.networking.database_subnets_properties[1].id, module.networking.database_subnets_properties[2].id]

in main.tf, it's working.

I can't find a solution how to improve this code with implementing loop or something like that. Could you please advise? Thank you in advance


Solution

  • You can use splat expression:

    database_subnets_ids = values(module.networking.database_subnets_properties)[*].id