【发布时间】:2020-07-30 23:27:32
【问题描述】:
我有多个使用 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" {
【问题讨论】:
标签: amazon-web-services foreach dns terraform amazon-route53