如何从 Terraform 中的 map 变量获取所有输出值?

huangapple go评论61阅读模式
英文:

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

问题

我尝试从子模块中提取id变量,以将其传递给另一个子模块。我的networking模块中的输出块如下所示:

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
    }
  })
}

我需要将这个输出的ID传递给lambda的vpc_config块。VPC配置块如下所示:

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

如果我像这样传递它们:

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

main.tf中,它能够正常工作。我无法找到如何通过实现循环或类似方法来改进这段代码的解决方案。您能否提供建议?提前谢谢。

英文:

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

答案1

得分: 1

你可以使用 splat 表达式

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

You can use splat expression:

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

huangapple
  • 本文由 发表于 2023年8月4日 05:08:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定