AWS Route 53 is Amazon Web Services' exceptionally adaptable and available Domain Name System DNS web service. It gives developers and administrators the ability to manage domain names and route internet traffic to different AWS resources and different endpoints. Terraform is an open-source infrastructure as-code instrument created by HashiCorp. It permits clients to characterize and arrange foundation resources using definitive setup records. Terraform automates the creation, modification, and deletion of resources across different cloud suppliers and on-premises conditions.
Each DNS zone compares to a space name e.g., geeksforgeeks.com and contains records that characterize how area names are set out to IP addresses. Route 53 allows users to register and manage domain names directly through the service. By using Route 53 users can create their own domain name easily. Users can create and manage DNS records, for example, A, AAAA, CNAME, MX, TXT, and so on to map domain names to specific IP addresses or other DNS records. Route 53 backings different routing arrangements, including simple routing, weighted routing, dormancy based routing, latency based routing, geolocation-based routing, and failover routing.
Terraform provides the aws_route53_record resource to manage an Route53 Record resource. A minimal configuration to get started requires a name argument. The resource can be extended with zone_id, type, ttl, records, alias blocks, routing policy blocks, and overwrite controls.
Core Resource awsroute53record
The aws_route53_record resource is the primary Terraform construct for creating and managing DNS records in Route 53.
A minimal configuration to get started is:
hcl
resource "aws_route53_record" "example" {
# Required arguments
name = "my-route53-record"
}
Provider configuration establishes the AWS connection before any Route 53 resources are defined.
hcl
provider "aws" {
region = "us-east-1"
}
This section specifies the AWS provider and sets the region to "us-east-1". The provider block configures the authentication details and default settings for interacting with AWS.
AWS Route Creation defines an AWS Route 53 record resources named "example_record". It indicates the setup for a DNS record inside the Route 53 zone recently made. The characteristics of the record include:
- zoneid: This characteristic references the ID of the Highway 53 zone where the record will be made. It utilizes the introduction sentence structure to get to the zone ID of the "examplezone" asset.
- name: This indicates the domain name for the record. For this situation, it's example "www.geeksforgeeks.com".
- type: This property characterizes the kind of DNS record
A common A record example using zone_id reference is:
hcl
resource "aws_route53_record" "example_record" {
zone_id = aws_route53_zone.example_zone.zone_id
name = "www.geeksforgeeks.com"
type = "A"
ttl = 300
records = ["203.0.113.10"]
}
Module Approach for Route53 Records
A Terraform module creates a Route53 record in AWS. It provides a flexible way to configure various types of routing policies for your DNS records.
The module creates a Route53 record with the specified zone ID, name, type, TTL, and records. It supports optional attributes. It supports different routing policies including geolocation, failover, latency, weighted, and CIDR-based routing. It provides the option for alias records that can point to AWS resources using their AWS resource name. It allows only one routing policy block to be supplied. It adheres to security best practices by leveraging automated scanning with Checkov.
Minimum example usage:
hcl
module "minimum_example" {
source = "boldlink/route53-records/aws"
version = "insert_latest_version"
zone_id = local.zone_id
name = var.name
type = var.type
ttl = var.ttl
records = var.records
}
Module requirements and providers:
| Name | Version |
|---|---|
| terraform | >= 0.14.11 |
| aws | >= 4.65.0 |
| Name | Version |
|---|---|
| aws | 5.23.1 |
No modules.
Resources created by the module:
| Name | Type |
|---|---|
| awsroute53record.main | resource |
Key input attributes:
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| alias | (Optional) An alias block. Conflicts with ttl & records. | any | {} | no |
| allow_overwrite | Allow creation of this record in Terraform to overwrite an existing record, if any |
The module allows creation of this record in Terraform to overwrite an existing record, if any via the allow_overwrite flag.
Routing Policies and Advanced Configurations
Route 53 supports multiple routing policies that can be expressed through aws_route53_record.
Weighted Routing distributes traffic based on weights - useful for blue-green deployments:
hcl
resource "aws_route53_record" "primary" {
zone_id = aws_route53_zone.primary.zone_id
name = "app.example.com"
type = "A"
set_identifier = "primary"
weighted_routing_policy {
weight = 80
}
alias {
name = aws_lb.primary.dns_name
zone_id = aws_lb.primary.zone_id
evaluate_target_health = true
}
}
hcl
resource "aws_route53_record" "canary" {
zone_id = aws_route53_zone.primary.zone_id
name = "app.example.com"
type = "A"
set_identifier = "canary"
weighted_routing_policy {
weight = 20
}
alias {
name = aws_lb.canary.dns_name
zone_id = aws_lb.canary.zone_id
evaluate_target_health = true
}
}
Failover Routing provides automatic failover between primary and secondary:
hcl
resource "aws_route53_record" "primary_failover" {
zone_id = aws_route53_zone.primary.zone_id
name = "app.example.com"
type = "A"
set_identifier = "primary"
failover_routing_policy {
type = "PRIMARY"
}
alias {
name = aws_lb.primary.dns_name
zone_id = aws_lb.primary.zone_id
evaluate_target_health = true
}
health_check_id = aws_route53_health_check.primary.id
}
hcl
resource "aws_route53_record" "secondary_failover" {
zone_id = aws_route53_zone.primary.zone_id
name = "app.example.com"
type = "A"
set_identifier = "secondary"
failover_routing_policy {
type = "SECONDARY"
}
alias {
name = aws_lb.secondary.dns_name
zone_id = aws_lb.secondary.zone_id
evaluate_target_health = true
}
}
The module supports different routing policies including geolocation, failover, latency, weighted, and CIDR-based routing. Only one routing policy block can be supplied per record.
Managing Validation Records and for_each Patterns
Certificate validation records are commonly created with for_each to keep configurations DRY.
hcl
resource "aws_route53_record" "cert_validation" {
for_each = {
for dvo in aws_acm_certificate.main.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = aws_route53_zone.primary.zone_id
}
Wait for validation to complete:
hcl
resource "aws_acm_certificate_validation" "main" {
certificate_arn = aws_acm_certificate.main.arn
validation_record_fqdns = [for record in aws_route53_record.cert_validation : record.fqdn]
}
For multiple similar records, use for_each to keep things DRY:
hcl
variable "subdomains" {
default = {
"api" = "10.0.1.10"
"admin" = "10.0.1.11"
"staging"= "10.0.2.10"
}
}
hcl
resource "aws_route53_record" "subdomains" {
for_each = var.subdomains
zone_id = aws_route53_zone.primary.zone_id
name = "${each.key}.example.com"
type = "A"
ttl = 300
records = [each.value]
}
Importing Existing Records and Best Practices
If you have existing Route 53 records and want to bring them under Terraform management:
```bash
Import an existing record
terraform import 'awsroute53record.web' Z1234567890example.comA
Import a weighted record
terraform import 'awsroute53record.primary' Z1234567890app.example.comA_primary
```
The import ID format is {zoneid}{name}{type}, with {setidentifier} appended for routing-policy records.
Best Practices:
Use variables for zone IDs - Don't hardcode zone IDs
The module and resource patterns encourage reuse of zoneid via local or variable references rather than hardcoding. Using allowoverwrite = true enables safe recreation of validation records. Using alias blocks instead of ttl and records points to AWS resources and allows health checks to be evaluated.
Provider Setup and Installation Steps
Step 2: Install Terraform.
Now install terraform packages by using following below commands:
bash
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
Now install terraform by using following commands
sudo yum -y install terraform
Step 3: Create A File And Write Terraform Script for AWS Route 53 Using Terraform
Create a file with .tf extension in that file write a script by using following command
Provider configuration is required before any Route 53 record resources are created.
Conclusion
Terraform AWS Route53 record management combines declarative infrastructure as code with the full routing capabilities of Amazon Route 53. The native aws_route53_record resource provides fine-grained control over name, type, ttl, records, alias, and routing policies. The community module approach adds a flexible wrapper that enforces a single routing policy per record, supports alias records that can point to AWS resources using their AWS resource name, and adheres to security best practices by leveraging automated scanning with Checkov.
Operational patterns include using foreach for validation and subdomain batches, importing existing records with the {zoneid}{name}{type} format with optional {setidentifier}, and applying weighted, failover, latency, geolocation and CIDR-based routing policies for traffic control. Provider setup with region configuration and installation via yum enables execution of the Terraform scripts that define zoneid references, domain names, and record types for AWS Route 53.