How to import your existing AWS Route53 Zone on Terraform

Reading Time: 2 minutes
In this blog post, learn the step-by-step process of importing an AWS Route53 zone into your terraform project. From installing and configuring the AWS command line interface (CLI), through identifying and importing the Route53 zone, to verifying with terraform plan and applying modifications, make your AWS Route53 zone management simpler and more streamlined with terraform.
import aws route53 zone in terraform

Table of Contents

In this post I will explain how to import an AWS Route53 zone in your terraform project if you manage it on AWS.

I am assuming that you already have a terraform project on your workstation. If it is not the case, follow this link to create your first terraform project: https://learn.hashicorp.com/collections/terraform/aws-get-started

1. Install and configure aws cli

2. List your Route53 zones

Using the command below, retrieve the list of your zone managed on AWS Route53 and identifiy the id of the zone you want to import in terraform.

				
					# language: shell
aws route53 list-hosted-zone
				
			

You response should looks like below

				
					{
  "HostedZones": [
    {
      "Id": "/hostedzone/Z3O0J2DXBE1FTB",
      "Name": "garygitton.fr.",
      "CallerReference": "RISWorkflow-d2b689d8c0e95d0eeae33e3b3e3c8f3d",
      "Config": {
        "Comment": "HostedZone created by Route53 Registrar",
        "PrivateZone": false
      },
      "ResourceRecordSetCount": 10
    }
  ]
}
				
			

From this response you can get the ID of your zone that we will need to use below. 
In this example the ID is Z3O0J2DXBE1FTB

3. Define the terraform providers needed

To work with the resource aws_route53_zone in terraform, your first need to defined a provider which can manage this resource. You should get a file providers.tf in your project containing the following configuration:

				
					# path: ~/myproject/providers.tf
# language: terraform
terraform {
  required_providers {
    aws = {
      version = "~> 4.0"
      source  = "hashicorp/aws"
    }
  }
}

provider "aws" {
  alias      = "eu_west_1"
  region     = "eu-west-1"
  access_key = "AKIAVX7X7J7Q7Q7X7X7X"
  secret_key = "/FBpfsDSDSg/Agfdgfd05485438+-fdsfdsFDOFD"
}

				
			

4. Import the Route53 zone in your terraform

Using the ID retrieve below we can import our zone in terraform. To do so we need to create and attribute a name to our aws_route53_zone resource, in my case “garygitton_fr”

				
					# path: ~/myproject/
# language: shell
terraform import aws_route_53.garygitton_fr Z3O0J2DXBE1FTB

				
			

The response should be like:

				
					
aws_route53_zone.garygitton_fr: Importing from ID "Z09569350VZU6BZU6V4"...
aws_route53_zone.garygitton_fr: Import prepared!
  Prepared aws_route53_zone for import
aws_route53_zone.garygitton_fr: Refreshing state... [id=Z09569350VZU6BZU6V4]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
				
			

5. Create the terraform resource for the zone

In your terraform file main.tf, add the base configuration for the aws_route53_zone resource

				
					# path: ~/myproject/main.tf
# language: terraform
terraform import aws_route_53.garygitton_fr Z3O0J2DXBE1FTB

				
			

6. Check the terraform plan

Using the command plan form terraform, verify the modification that will be bring

				
					# path: ~/myproject
# language: shell
terraform plan

				
			

Output should be like:

				
					aws_route53_zone.garygitton_fr: Refreshing state... [id=Z09569350VZU6BZU6V4]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # aws_route53_zone.garygitton_fr will be updated in-place
  ~ resource "aws_route53_zone" "garygitton_fr" {
      ~ comment       = "HostedZone created by Route53 Registrar" -> "Managed by Terraform"
      + force_destroy = false
        id            = "Z09569350VZU6BZU6V4"
        name          = "garygitton.fr"
        tags          = {}
        # (4 unchanged attributes hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.
				
			

7. Apply the modification

If the plan, is good for you, apply the modification as follow:

				
					# path: ~/myproject
# language: shell
terraform plan

				
			

You should get a response like

				
					Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_route53_zone.garygitton_fr: Modifying... [id=Z09569350VZU6BZU6V4]
aws_route53_zone.garygitton_fr: Modifications complete after 1s [id=Z09569350VZU6BZU6V4]
				
			

You are all done

You just imported your AWS Route53 zone in your Terraform configuration.

Share it on:

Twitter
LinkedIn
Facebook
WhatsApp

About the Author

Gary Gitton
Hello, I'm Gary Gitton - an accomplished Software Engineer, Tech Lead, specializing in PHP, API Engineering, DevOps, and Cloud Management. Throughout my career, I've had the privilege to enhance multiple software solutions with my expertise in multiple languages and platforms. I bring my unique blend of technical and business acumen to every project, ensuring efficient, scalable, and innovative outcomes. I'm also passionate about fostering a culture of continuous learning, mentoring developers, and leading projects with agile methodologies. Whether concocting a bespoke API or orchestrating cloud environments, I'm committed to delivering quality and excellence. Let's connect and transform your vision into a digital reality.

You might also like