【问题标题】:is there a way to create route53_zone and apply its zone_id to records at the same time in terraform?有没有办法在 terraform 中创建 route53_zone 并将其 zone_id 同时应用于记录?
【发布时间】:2021-08-12 08:48:11
【问题描述】:

我对编码和 terraform 非常陌生。

我试图将我需要的每个资源都写入 .tf 文件

在这样做的同时,我

Terraform plan

得到一个错误

│ Error: no matching Route53Zone found
│
│   with data.aws_route53_zone.<name>,
│   on <filename>.tf line 70, in data "aws_route53_zone" "<name>":
│   70: data "aws_route53_zone" "<name>" {

这就是我写的

resource "aws_route53_zone" "example" {
    name       = "example.com"
    comment    = "eg-example"

    tags= {
        Name = "example.com"
    }
}

resource "aws_acm_certificate" "eg-example-acm-domaincert" {
    domain_name               = "*.example.com"
    validation_method         = "DNS"
    tags = {
      name = "eg-example-acm-domaincert"
      }
}

resource "aws_route53_record" "cname-example" {
    zone_id = data.aws_route53_zone.example.zone_id
    name    = "cname.example.com"
    type    = "CNAME"
    records = ["cname-example-alb-ext-1111111111.region.elb.amazonaws.com"]
    ttl     = "300"

}

data "aws_route53_zone" "example" {
  name         = "example.com."
  private_zone = true
}

resource "aws_acm_certificate_validation" "eg-example-acm-domaincert" {
  certificate_arn         = "arn:aws:acm:**********************"
  validation_record_fqdns = ["*****************************.example.com"]
}

代码与此差别不大。

除了在 AWS 控制台上创建一个区域之外,我真的没有任何想法。

还有其他方法吗?

希望我的解释对你有意义。

【问题讨论】:

    标签: javascript amazon-web-services terraform amazon-route53


    【解决方案1】:

    由于您是在 TF 脚本中创建 HZ,因此您也可以参考它,而无需使用数据源:

    resource "aws_route53_record" "cname-example" {
        zone_id = aws_route53_zone.example.zone_id
        name    = "cname.example.com"
        type    = "CNAME"
        records = ["cname-example-alb-ext-1111111111.region.elb.amazonaws.com"]
        ttl     = "300"
    
    }
    

    【讨论】:

      【解决方案2】:

      摆脱对data 的调用,直接使用资源:

      aws_route53_zone.example.zone_id

      在检索之前创建的资源时使用data(例如,来自另一个 Terraform 项目)。

          
      resource "aws_route53_record" "cname-example" {
          zone_id = aws_route53_zone.example.zone_id // no need for data.
          name    = "cname.example.com"
          type    = "CNAME"
          records = ["cname-example-alb-ext-1111111111.region.elb.amazonaws.com"]
          ttl     = "300"
      
      }

      运行terraform validateterraform plan

      祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-20
        • 1970-01-01
        • 2020-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-08
        • 1970-01-01
        相关资源
        最近更新 更多