我有多个使用Terraform创建的DNS记录。我的一条记录具有两个需要读取的值。我在使Terraform能够循环遍历变量以使其成功读取时遇到问题。下面是我的代码。即使我拥有正确的数据类型,我仍然得到一个没有任何意义的错误。我如何遍历这些资源有一些错误,而且在弄清楚哪里出了问题时遇到了一些麻烦。任何建议将不胜感激。
variables.tf
variable "mx" {
type = map(object({
ttl = string
records = set(string)
}))
}
variables.tfvars
mx = {
"mx_record1" = {
ttl = "3600"
records = [
"mx_record1_value"
]
}
"mx_record2" = {
ttl = "3600"
records = [
"mx_record2_value"
"mx_record2_value2"
]
}
mx.tf
locals {
mx_records = flatten([
for mx_key, mx in var.mx : [
for record in mx.records : {
record = record
mx = mx_key
}
]
])
}
resource "aws_route53_record" "mx_records" {
for_each = { for mx in local.mx_records : mx.record => mx }
zone_id = aws_route53_zone.zone.zone_id
name = each.key
type = "MX"
ttl = each.value.ttl
records = [
each.value.record
]
}
低于错误
Error:
Error: Unsupported attribute
on mx.tf line 17, in resource "aws_route53_record" "mx_records":
17: ttl = each.value.ttl
|----------------
| each.value is object with 2 attributes
This object does not have an attribute named "ttl".
更新:
locals {
mx_records = flatten([
for mx_key, mx in var.mx : [
for record in mx.records : {
record = record
mx = mx_key
ttl = mx.ttl
}
]
])
}
resource "aws_route53_record" "mx_records" {
for_each = { for mx in local.mx_records : mx.record => mx }
zone_id = aws_route53_zone.zone.zone_id
name = each.key
type = "MX"
ttl = each.value.ttl
records = [
each.value.record
]
}
错误
Error: [ERR]: Error building changeset: InvalidChangeBatch: [FATAL problem: UnsupportedCharacter (Value contains unsupported characters) encountered with ' ']
status code: 400, request id: a27e6a47-c10f-42ce-be94-10aaa9c276f8
on mx.tf line 13, in resource "aws_route53_record" "mx_records":
13: resource "aws_route53_record" "mx_records" {
答案 0 :(得分:2)
我认为您的mx_records
应该是:
locals {
mx_records = flatten([
for mx_key, mx in var.mx :
[for record in mx.records: {
record = record
mx = mx_key
ttl = mx.ttl
}]
])
}
这将导致以下结构:
mx_records = [
{
"mx" = "mx_record1"
"record" = "mx_record1_value"
"ttl" = "3600"
},
{
"mx" = "mx_record2"
"record" = "mx_record2_value"
"ttl" = "3600"
},
{
"mx" = "mx_record2"
"record" = "mx_record2_value2"
"ttl" = "3600"
},
]