![](/img/trans.png)
[英]Invalid request error in AWS::Route53::RecordSet when creating stack with AWS CloudFormation json
[英]Terraform error building changeset: InvalidChangeBatch when looping through multiple Route53 Records
我有使用 for_each 命令創建的 Route 53 記錄。 我的一條記錄與其條目相關聯的值超過 1 個。 這是聲明記錄的方式:
變量.tf
variable "mx" {
type = map(object({
ttl = string
records = set(string)
}))
}
變量.tfvars
mx = {
"mywebsite.org." = {
ttl = "3600"
records = [
"home.mywebsite.org.",
"faq.mywebsite.org."
]
}
"myotherwebsite.org." = {
ttl = "3600"
records = [
"home.myotherwebsite.org."
]
}
mx.tf文件
locals {
mx_records = flatten([
for mx_key, mx in var.mx : [
for record in mx.records : {
mx_key = mx_key
record = record
ttl = mx.ttl
}]
])
}
resource "aws_route53_record" "mx_records" {
for_each = { for idx, mx in local.mx_records : idx => mx }
zone_id = aws_route53_zone.zone.zone_id
name = each.value.mx_key
type = "MX"
ttl = each.value.ttl
records = [
each.value.record
]
}
執行時一切正常,直到 Terraform 意識到我的記錄有額外的價值。 然后它會生成以下錯誤:
Error building changeset: InvalidChangeBatch: [Tried to create resource record set
[name='mywebsite.org.', type='MX'] but it already exists]
我的問題是,有沒有辦法讓 Terraform 不為此值創建第二個條目? 對於 Route53,所有記錄名稱都必須是唯一的。 Terraform 是否有一種方法可以簡單地將此值添加到此記錄,因為它是在初始執行運行中創建的? 任何幫助將不勝感激,因為這變得具有挑戰性。
更新刪除展平並更新為'records = [each.value.records]'后,這是錯誤:
Error: Unsupported attribute
on mx.tf line 20, in resource "aws_route53_record" "mx_records":
20: each.value.record
|----------------
| each.value is tuple with 2 elements
This value does not have any attributes.
Error: Unsupported attribute
on mx.tf line 20, in resource "aws_route53_record" "mx_records":
20: each.value.record
|----------------
| each.value is tuple with 1 element
This value does not have any attributes.
我認為您可以直接使用您的mx
,而不是將其轉換為mx_records
。
您可以嘗試以下操作:
resource "aws_route53_record" "mx_records" {
for_each = var.mx
zone_id = aws_route53_zone.zone.zone_id
name = each.key
type = "MX"
ttl = each.value.ttl
records = each.value.records
}
上面的for_each
應該只執行兩次。 首先是mywebsite.org.
第二個是myotherwebsite.org.
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.