Terraform for_each 对象迭代
Terraform for_each iteration over object
我是 Terraform 的新手,我想简化一些我需要使用 for_each 创建的私有 r53 区域。
我有以下本地文件,我想用它来创建私有区域和在这些区域中关联的 A 记录:
locals {
private_zones = [
{
name = "foo.com"
txt = [
"This is the txt record for foo.com"]
ttl = 300
records = {
"host1" = "192.168.0.1",
"host2" = "192.168.0.2"
}
},
{
name = "bar.com"
txt = [
"This is the txt record for bar.com"]
ttl = 300
records = {
"host1" = "192.168.0.3",
"host2" = "192.168.0.4"
}
}
]
}
我找到了一些代码,可以让我遍历本地以创建区域
resource "aws_route53_zone" "zone" {
for_each = { for name in local.private_zones : name.name => name }
name = each.value.name
vpc {
vpc_id = <vpc_id>
}
}
但我不知道如何使用每个本地的记录列表在各自的区域中迭代和创建 A 记录。
你会使用 aws_route53_record 和 flattened private_zones
:
locals {
private_zones_flat = merge([
for zone in local.private_zones: {
for host, ip in zone.records:
"${zone.name}-${host}" => {
zone_name = zone.name
host = host
ip = ip
}
}
]...)
}
resource "aws_route53_record" "host" {
for_each = local.private_zones_flat
zone_id = aws_route53_zone.zone[each.value.zone_name].zone_id
name = each.value.host
type = "A"
ttl = "300"
records = [each.value.ip]
}
我是 Terraform 的新手,我想简化一些我需要使用 for_each 创建的私有 r53 区域。
我有以下本地文件,我想用它来创建私有区域和在这些区域中关联的 A 记录:
locals {
private_zones = [
{
name = "foo.com"
txt = [
"This is the txt record for foo.com"]
ttl = 300
records = {
"host1" = "192.168.0.1",
"host2" = "192.168.0.2"
}
},
{
name = "bar.com"
txt = [
"This is the txt record for bar.com"]
ttl = 300
records = {
"host1" = "192.168.0.3",
"host2" = "192.168.0.4"
}
}
]
}
我找到了一些代码,可以让我遍历本地以创建区域
resource "aws_route53_zone" "zone" {
for_each = { for name in local.private_zones : name.name => name }
name = each.value.name
vpc {
vpc_id = <vpc_id>
}
}
但我不知道如何使用每个本地的记录列表在各自的区域中迭代和创建 A 记录。
你会使用 aws_route53_record 和 flattened private_zones
:
locals {
private_zones_flat = merge([
for zone in local.private_zones: {
for host, ip in zone.records:
"${zone.name}-${host}" => {
zone_name = zone.name
host = host
ip = ip
}
}
]...)
}
resource "aws_route53_record" "host" {
for_each = local.private_zones_flat
zone_id = aws_route53_zone.zone[each.value.zone_name].zone_id
name = each.value.host
type = "A"
ttl = "300"
records = [each.value.ip]
}