Terraform 遍历嵌套对象
Terraform loop over nested object
假设我有以下本地本地:
locals {
domains = [
"site01.example.com",
"site02.example.com",
]
}
为每个域创建一个证书,returns DNS 验证选项:
resource "aws_acm_certificate" "lps-certificate" {
for_each = local.domains
...
}
到目前为止,我能够创建以下本地:
locals {
records = {
for domain in local.domains : domain => {
for dvo in aws_acm_certificate.lps-certificate[domain].domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
}
}
哪个returns:
{
site01.example.com = {
*.site01.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
site01.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
}
site02.example.com = {
*.site02.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
site02.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
}
}
有没有办法让它看起来像这样:
{
*.site01.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
site01.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
*.site02.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
site02.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
}
除非我误解了这个问题,否则您希望获得的表格可以使用 values
、merge
和 argument expanding:
生成
merge(values(local.records)...)
我没有你的数据结构,所以测试如下:
variable "records" {
default = {
"site01.example.com" = {
"*.site01.example.com" = {
name = "(known after apply)"
record = "(known after apply)"
type = "(known after apply)"
}
"site01.example.com" = {
name = "(known after apply)"
record = "(known after apply)"
type = "(known after apply)"
}
}
"site02.example.com" = {
"*.site02.example.com" = {
name = "(known after apply)"
record = "(known after apply)"
type = "(known after apply)"
}
"site02.example.com" = {
name = "(known after apply)"
record = "(known after apply)"
type = "(known after apply)"
}
}
}
}
output "test" {
value = merge(values(var.records)...)
}
结果是(顺序不同,但希望没关系):
test = {
"*.site01.example.com" = {
"name" = "(known after apply)"
"record" = "(known after apply)"
"type" = "(known after apply)"
}
"*.site02.example.com" = {
"name" = "(known after apply)"
"record" = "(known after apply)"
"type" = "(known after apply)"
}
"site01.example.com" = {
"name" = "(known after apply)"
"record" = "(known after apply)"
"type" = "(known after apply)"
}
"site02.example.com" = {
"name" = "(known after apply)"
"record" = "(known after apply)"
"type" = "(known after apply)"
}
}
假设我有以下本地本地:
locals {
domains = [
"site01.example.com",
"site02.example.com",
]
}
为每个域创建一个证书,returns DNS 验证选项:
resource "aws_acm_certificate" "lps-certificate" {
for_each = local.domains
...
}
到目前为止,我能够创建以下本地:
locals {
records = {
for domain in local.domains : domain => {
for dvo in aws_acm_certificate.lps-certificate[domain].domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
}
}
哪个returns:
{
site01.example.com = {
*.site01.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
site01.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
}
site02.example.com = {
*.site02.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
site02.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
}
}
有没有办法让它看起来像这样:
{
*.site01.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
site01.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
*.site02.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
site02.example.com = {
name = (known after apply)
record = (known after apply)
type = (known after apply)
}
}
除非我误解了这个问题,否则您希望获得的表格可以使用 values
、merge
和 argument expanding:
merge(values(local.records)...)
我没有你的数据结构,所以测试如下:
variable "records" {
default = {
"site01.example.com" = {
"*.site01.example.com" = {
name = "(known after apply)"
record = "(known after apply)"
type = "(known after apply)"
}
"site01.example.com" = {
name = "(known after apply)"
record = "(known after apply)"
type = "(known after apply)"
}
}
"site02.example.com" = {
"*.site02.example.com" = {
name = "(known after apply)"
record = "(known after apply)"
type = "(known after apply)"
}
"site02.example.com" = {
name = "(known after apply)"
record = "(known after apply)"
type = "(known after apply)"
}
}
}
}
output "test" {
value = merge(values(var.records)...)
}
结果是(顺序不同,但希望没关系):
test = {
"*.site01.example.com" = {
"name" = "(known after apply)"
"record" = "(known after apply)"
"type" = "(known after apply)"
}
"*.site02.example.com" = {
"name" = "(known after apply)"
"record" = "(known after apply)"
"type" = "(known after apply)"
}
"site01.example.com" = {
"name" = "(known after apply)"
"record" = "(known after apply)"
"type" = "(known after apply)"
}
"site02.example.com" = {
"name" = "(known after apply)"
"record" = "(known after apply)"
"type" = "(known after apply)"
}
}