首页 > 解决方案 > Terraform - 恢复 route53 记录

问题描述

长期潜伏者第一次发帖。

我是使用 terraform 的新手,并尝试浏览文档,但文档的记录不够清楚,无法让我解决我的问题。

设想

我正在尝试模拟最终用户意外从 route53 中删除公共托管区域然后使用 terraform 恢复公共区域及其记录集的事件。

问题

我遇到的问题是,当公共托管区域被删除时,运行 terraform 计划会吓坏并说 route53 记录/区域不存在,但实际上并没有提示我创建新区域,即使它是目前正在管理 terraform。

我的解决方法是在导入的对象上运行 terraform state rm,编辑我的 example.tf 文件以删除任何 route53 资源记录集,只保留 aws_route53_zone 记录完整并重新运行 terraform 计划,这样做 terraform 认识到这是一个新的公共托管区域,并将创建新的公共区域。问题在于,它分两步完成,我想一步完成,理想情况下运行 terraform 计划一次以创建托管区域,然后在新区域中创建记录集。

当前的工作解决方案 - 例如.tf

此方法需要管理员运行两次 terraform plan 并涉及编辑 example.tf 文件。首先删除 terraform 管理的现有记录集,以便运行 terraform plan 以创建意外删除的公共托管区域。

resource "aws_route53_zone" "example" {
  name = "terraform-test.example.com"
}

理想的解决方案 - example.tf

这是我想到的可行的解决方案,但是当我运行 terraform plan 时,它会抱怨找不到或未声明的资源。

# This should create the public hosted zone if it's not existing.
resource "aws_route53_zone" "example" { 
  name         = "terraform-test.example.com"
  private_zone = false
}
# Then it should create the following records under the hosted zone. 
resource "aws_route53_record" "www-terraform-A" {
  name    = "${data.aws_route53_zone.example.name}"
  zone_id = "${data.aws_route53_zone.example.zone_id}"
  type    = "A"
  ttl     = "300"
  records = ["X.X.X.X"]
}

我希望在运行 terraform plan 之后,terraform 识别出该区域不存在,它应该提示我创建它然后记录记录集,但它不是我收到的错误消息

Error: Reference to undeclared resource

  on terraform-test.servallapps.com.tf line 6, in resource "aws_route53_record" "www-terraform-A":
   6:   name    = "${data.aws_route53_zone.example.name}"

A data resource "aws_route53_zone" "example" has not been declared in the
root module.

标签: amazon-route53terraform-provider-aws

解决方案


错误是因为它不是数据源而是资源,所以它不需要data前缀。

更改data.aws_route53_zone.example.nameaws_route53_zone.example.name应该工作


推荐阅读