Terraform - 循环数据源中的复杂数据

问题描述 投票:0回答:1

我正在尝试通过循环数据源中的指令来配置 aws 上的记录。

数据源如下所示:

data "dns_instructions" "example-dns-instructions" {
    id      = "123"
    instructions {
        name   = "www.example.com"
        type   = "A"
        value  = "1.2.3.4"
    }

    instructions {
        name   = "a.example.com"
        type   = "TXT"
        value  = "value 1"
    }
}

我尝试了以下方法:

resource "aws_route53_record" "my-records" {
  for_each = {
      for inst in data.dns_instructions.example-dns-instructions.instructions : 
         "${inst.id}" => inst
    }
  zone_id = "ABCSDSD....."
  name    = each.value.name
  type    = each.value.type
  ttl     = 300
  records = each.value.value
}

我确实看到了 TF 状态下包含所有详细信息的数据源,但在尝试申请时,得到:

data.dns_instructions.example-dns-instructions.instructions is null
│
│ A null value cannot be used as the collection in a 'for' expression.

我可能缺少 terraform 中的一些基本内容,有人可以帮忙吗?

amazon-web-services terraform terraform-provider-aws aws-route53
1个回答
0
投票

如果指令始终具有固定值,我建议在这种情况下使用局部变量:

locals {
  instructions = {
    www = {
      name   = "www.example.com"
      type   = "A"
      value  = "1.2.3.4"
     }
    a = {
      name   = "a.example.com"
      type   = "TXT"
      value  = "value 1"
    }
  }
}

resource "aws_route53_record" "my_records" {
  for_each = local.instructions
  zone_id  = "ABCSDSD....."
  name     = each.value.name
  type     = each.value.type
  ttl      = 300
  records  = each.value.value
}
© www.soinside.com 2019 - 2024. All rights reserved.