2

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

Marcin
  • 215,873
  • 14
  • 235
  • 294

1 Answers1

1

You can use splat expression:

database_subnets_ids = values(module.networking.database_subnets_properties)[*].id
Ervin Szilagyi
  • 14,274
  • 2
  • 25
  • 40
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Hi Marcin Thanks for reach me out, I was trying to do this way, but terraform sent me this error: `Planning failed. Terraform encountered an error while generating this plan. ╷ │ Error: Missing map element │ │ on main.tf line 82, in module "microservices": │ 82: database_subnets_ids = module.networking.database_subnets_properties[*].id #local.database_subnets_ids[*].id │ │ This map does not have an element with the key "id".` But key "id" is present in this mapping, I showed it in the original question body – Yaroslav Kovalevskyi Aug 04 '23 at 10:10
  • @YaroslavKovalevskyi Answer updated. – Marcin Aug 05 '23 at 03:56
  • Wow, it's working Thank you a lot! Cheers – Yaroslav Kovalevskyi Aug 05 '23 at 08:04