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
To install aws-cli, use the link: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html
To configure it, use the link: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html
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.