Blaine
Blaine

Reputation: 667

Get zone id for an exisiting zone or create a new zone with zone name

I am using cdktf to generate terraform code, and want to get the zone_id for an existing zone so I can create new records in it.

"aws_route53_zone": {
      "typescript-aws_get_zone_id_C1732EA4": {
        "name": "mydomain.com",
        "//": {
          "metadata": {
            "path": "custom_stack/typescript-aws/get_zone_id",
            "uniqueId": "typescript-aws_get_zone_id_C1732EA4"
          }
        }
      }
    },
    ...
"aws_route53_record": {
      "typescript-aws_aws_cloudfront_mydomaincom_aws_cloudfront_mydomaincom_record_5E08FD7F": {
        "name": "mydomain.com",
        "type": "A",
        "zone_id": "${aws_route53_zone.typescript-aws_get_zone_id_C1732EA4.zone_id}",
        "alias": [
          {
            "evaluate_target_health": false,
            "name": "${aws_cloudfront_distribution.typescript-aws_aws_cloudfront_mydomaincom_4EF84BC8.domain_name}",
            "zone_id": "${aws_cloudfront_distribution.typescript-aws_aws_cloudfront_mydomaincom_4EF84BC8.hosted_zone_id}"
          }
        ],
        "//": {
          "metadata": {
            "path": "custom_stack/typescript-aws/aws_cloudfront_mydomain.com/aws_cloudfront_mydomain.com_record",
            "uniqueId": "typescript-aws_aws_cloudfront_mydomaincom_aws_cloudfront_mydomaincom_record_5E08FD7F"
          }
        }
      },

However, this always creates a new zone (whose name matches an exisiting zone, but both have separate zone ids.

I referred to this question ( Fetch zone_id of hosted domain on route53 using terraform ) but seems like the flag suggested in the approved answer is no longer available? At least not as per the docs ( https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_zone )

Upvotes: 0

Views: 516

Answers (1)

Daniel Schmidt
Daniel Schmidt

Reputation: 11921

In Terraform resources create resources and data sources load the data. You should be able to use data_route53_zone, which should be aws.route53. DataAwsRoute53Zone.

Upvotes: 1

Related Questions