My domain in AWS with Terraform - Part 1 - Import
Currently, I have two domains setup in my AWS environment, one for personal and one for my family. I want the ability to manage these domains programmatically using Terraform. This might be overkill for most people but I like to tinker with things. I’m hoping at the end of this process I can easily change my Amazon Route53 configuration while self documenting all the changes through GIT and CI/CD.
Terraform Import
My AWS infrastructure was already setup using the console, so I need a way to turn that into code. Technically, I could just recreate everything but I thought, there has to be a better way. Then one day I heard about something called Terraform Import. Basically, it’s a way to target your current configurations using IDs with the ability to import it in your code.
There’s a few ways to import your AWS infrastructure into Terraform, you can either use the CLI or write the code inside a tf file. Since I’m importing a handful of configurations, I decided on created a import.tf file and writting all my import code inside there.
This is what my import.tf file looks like:
For the most up to date code, please use reference my Github Repo.
import {
to = aws_route53_zone.zonefm
id = var.zone_id
}
import {
to = aws_route53_record.mx-1
id = "${var.zone_id}_${var.my_domain}_MX"
}
import {
to = aws_route53_record.soa-1
id = "${var.zone_id}_${var.my_domain}_SOA"
}
import {
to = aws_route53_record.txt-1
id = "${var.zone_id}_${var.my_domain}_TXT"
}
import {
to = aws_route53_record.ns-1
id = "${var.zone_id}_${var.my_domain}_NS"
}
import {
to = aws_route53_record.cname-1
id = "${var.zone_id}_fm1${var.cname}_CNAME"
}
import {
to = aws_route53_record.cname-2
id = "${var.zone_id}_fm2${var.cname}_CNAME"
}
import {
to = aws_route53_record.cname-3
id = "${var.zone_id}_fm3${var.cname}_CNAME"
}
After all the import code is written, I run the command:
terraform plan -generate-config-out=generated.tf
This will output my current configurations into a generated.tf file.
Note: I couldn’t figure out how to get one function for the import on CNAMEs. I tried doing a count with
count.index
but for some reason it was throwing up errors. If anyone has suggestions, please reach out.
During my import, I did notice some errors around “multivalue_answer_routing_policy”. These errors do not stop the import of infrastructure but do require a change to the boolean values in your code.
Note: For a easy fix, go into your generated.tf file and remove the “multivalue_answer_routing_policy = false” line. This is assuming you don’t actually need multivalue routing policies.
My code and how to use it
If you like to mirror what I did, you can clone my repo.
Clone the repo to your local machine
git clone "https://github.com/impulsive-sudor/TF-Import-AWS-Route53-Zone-and-Records.git"
Change the tfvar values in the terraform.tfvars file to ones that are relevant to your environment.
aws_region = "us-east-1" zone_id = "" my_domain = "example.com" cname = "._domainkey.example.com"
If you aren’t using Fastmail, adjust the “fm” ID in the CNAME to what’s used by your email provider. (For example, Protonmail is just “protonmail”)
import { to = aws_route53_record.cname-1 id = "${var.zone_id}_fm1${var.cname}_CNAME" }
Run the following command to initialize the directory.
terraform init
Run the following command to output your current infrastructure into terraform code.
terraform plan -generate-config-out=generated.tf
You should now see a generated.tf file in your current directory. If you do, congrats! You have successfully imported your Terraform infrastructure using Terraform import.