Terraform - 无法访问原始类型值(字符串)的属性
Terraform - Can't access attributes on a primitive-typed value (string)
我正在使用 terraform 在 cloudflare 中部署 MX 记录。创建局部变量并将它们展平,过滤掉不同的值
locals {
MXRecordSets = [
{
Name = "example.com.",
Type = "MX",
TTL = 3600,
MXRecords = [
{
Value = "asdsd.l.google.com",
Priority = 1
},
{
Value = "alt1.asdsd.l.google.com",
Priority = 5
},
{
Value = "alt2.asdsd.l.google.com",
Priority = 5
}
]
}
]
}
locals {
FlatMXRecordSets = merge([
for idx, MXRecordSet in local.MXRecordSets:
{
for MXRecord in MXRecordSet.MXRecords:
"${idx}-${MXRecord.Value}" => {
MXRecordSet = MXRecordSet
MXRecord = MXRecord["Value"]
Priority = MXRecord["Priority"]
}
}
]...)
}
resource "cloudflare_record" "com_records_mx" {
for_each = local.FlatMXRecordSets
zone_id = cloudflare_zone.example_com.id
name = each.value["MXRecordSet"].Name
value = each.value["MXRecord"].Value
type = each.value["MXRecordSet"].Type
proxied = false
ttl = 1
priority = each.value["MXRecord"].Priority
}
虽然 运行 terraform apply
,但我收到一条错误消息,提示无法访问原始类型值(字符串)上的属性。
╷
│ Error: Unsupported attribute
│
│ on main.tf line 476, in resource "cloudflare_record" "com_records_mx":
│ 476: priority = each.value["MXRecord"].Priority
│ ├────────────────
│ │ each.value["MXRecord"] is "alt2.asdsd.l.google.com"
│
│ Can't access attributes on a primitive-typed value (string).
对象访问器似乎有几个问题。更正它应该如下所示:
resource "cloudflare_record" "com_records_mx" {
for_each = local.FlatMXRecordSets
zone_id = cloudflare_zone.example_com.id
name = each.value["MXRecordSet"].Name
value = each.value["MXRecord"] # corrected
type = each.value["MXRecordSet"].Type
proxied = false
ttl = 1
priority = each.value["Priority"] # corrected
}
我正在使用 terraform 在 cloudflare 中部署 MX 记录。创建局部变量并将它们展平,过滤掉不同的值
locals {
MXRecordSets = [
{
Name = "example.com.",
Type = "MX",
TTL = 3600,
MXRecords = [
{
Value = "asdsd.l.google.com",
Priority = 1
},
{
Value = "alt1.asdsd.l.google.com",
Priority = 5
},
{
Value = "alt2.asdsd.l.google.com",
Priority = 5
}
]
}
]
}
locals {
FlatMXRecordSets = merge([
for idx, MXRecordSet in local.MXRecordSets:
{
for MXRecord in MXRecordSet.MXRecords:
"${idx}-${MXRecord.Value}" => {
MXRecordSet = MXRecordSet
MXRecord = MXRecord["Value"]
Priority = MXRecord["Priority"]
}
}
]...)
}
resource "cloudflare_record" "com_records_mx" {
for_each = local.FlatMXRecordSets
zone_id = cloudflare_zone.example_com.id
name = each.value["MXRecordSet"].Name
value = each.value["MXRecord"].Value
type = each.value["MXRecordSet"].Type
proxied = false
ttl = 1
priority = each.value["MXRecord"].Priority
}
虽然 运行 terraform apply
,但我收到一条错误消息,提示无法访问原始类型值(字符串)上的属性。
╷
│ Error: Unsupported attribute
│
│ on main.tf line 476, in resource "cloudflare_record" "com_records_mx":
│ 476: priority = each.value["MXRecord"].Priority
│ ├────────────────
│ │ each.value["MXRecord"] is "alt2.asdsd.l.google.com"
│
│ Can't access attributes on a primitive-typed value (string).
对象访问器似乎有几个问题。更正它应该如下所示:
resource "cloudflare_record" "com_records_mx" {
for_each = local.FlatMXRecordSets
zone_id = cloudflare_zone.example_com.id
name = each.value["MXRecordSet"].Name
value = each.value["MXRecord"] # corrected
type = each.value["MXRecordSet"].Type
proxied = false
ttl = 1
priority = each.value["Priority"] # corrected
}