概述
我想要创建一个Route53 DNS记录。取决于变量,它要么是CNAME记录,要么是别名记录。
在第一次运行时,这是可行的,因为这两个记录都不存在。
但是,当更新变量var.route_53_redirection_type
以更改记录类型时,terraform apply
失败,因为它试图在删除旧记录之前创建新记录。
Error: [ERR]: Error building changeset: InvalidChangeBatch: [RRSet of type A with DNS name redirect is not permitted because a conflicting RRSet of type CNAME with the same DNS name already exists in zone.]
当terraform apply
再次运行时,它会工作,因为该记录在上一次运行时已被删除。
以下是代码:
resource "aws_route53_record" "alias" {
count = var.route_53_redirection_type == "ALIAS" ? 1 : 0
zone_id = data.aws_route53_zone.public.zone_id
name = "redirect"
type = "A"
alias {
name = module.alb.alb_dns_name
zone_id = module.alb.alb_zone_id
evaluate_target_health = true
}
}
resource "aws_route53_record" "cname" {
count = var.route_53_redirection_type == "CNAME" ? 1 : 0
zone_id = data.aws_route53_zone.public.zone_id
name = "redirect"
type = "CNAME"
ttl = "5"
records = ["www.google.com"]
}
问题
注意到,我看过生命周期和在……上面,但这两种方法似乎都不适用于这种情况。
提前感谢!
发布于 2022-07-28 10:21:21
更新记录类型的具体原因是什么?
但是,如果这是删除任何特定资源的要求,我们总是可以使用破坏命令来销毁一套完整的云基础设施或目标资源。
terraform destroy --target aws_route53_record.cname
发布于 2022-07-28 12:48:59
我认为很难根据需求实现你想要的东西。我认为更好的方法是使用dynamic
块1和for_each
2。这样,代码就会看起来像:
resource "aws_route53_record" "record" {
zone_id = data.aws_route53_zone.public.zone_id
name = "redirect"
type = var.route_53_redirection_type == "ALIAS" ? "A" : "CNAME"
ttl = var.route_53_redirection_type == "ALIAS" ? null : 5
dynamic "alias" {
for_each = var.route_53_redirection_type == "ALIAS" ? [1] : []
content {
name = module.alb.alb_dns_name
zone_id = module.alb.alb_zone_id
evaluate_target_health = true
}
}
}
https://stackoverflow.com/questions/73150142
复制相似问题