0
0
TerraformHow-ToBeginner · 4 min read

How to Create Route53 Record with Terraform

Use the aws_route53_record resource in Terraform to create a Route53 DNS record. Define the hosted zone ID, record name, type, and value inside the resource block to manage DNS entries.
📐

Syntax

The aws_route53_record resource manages DNS records in AWS Route53. You specify the hosted zone ID, record name, record type, and the value(s) for the DNS entry.

  • zone_id: The ID of the Route53 hosted zone.
  • name: The DNS record name (e.g., example.com or www.example.com).
  • type: The DNS record type (e.g., A, CNAME, TXT).
  • ttl: Time to live in seconds (optional, default is 300).
  • records: List of values for the DNS record.
terraform
resource "aws_route53_record" "example" {
  zone_id = "ZONEID123456"
  name    = "www.example.com"
  type    = "A"
  ttl     = 300
  records = ["192.0.2.44"]
}
💻

Example

This example creates an A record for www.example.com pointing to the IP address 192.0.2.44 in the specified hosted zone.

terraform
provider "aws" {
  region = "us-east-1"
}

resource "aws_route53_zone" "example_zone" {
  name = "example.com"
}

resource "aws_route53_record" "www" {
  zone_id = aws_route53_zone.example_zone.zone_id
  name    = "www.example.com"
  type    = "A"
  ttl     = 300
  records = ["192.0.2.44"]
}
Output
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
⚠️

Common Pitfalls

  • Using the wrong zone_id will cause Terraform to fail or create records in the wrong hosted zone.
  • For CNAME records, the records value must be a domain name, not an IP.
  • Not specifying ttl defaults to 300 seconds, which might not suit all use cases.
  • For multiple values, ensure records is a list of strings.
terraform
/* Wrong: Using IP in CNAME record */
resource "aws_route53_record" "bad_cname" {
  zone_id = "ZONEID123456"
  name    = "alias.example.com"
  type    = "CNAME"
  ttl     = 300
  records = ["192.0.2.44"]  # Incorrect, should be domain name
}

/* Correct: Using domain name in CNAME record */
resource "aws_route53_record" "good_cname" {
  zone_id = "ZONEID123456"
  name    = "alias.example.com"
  type    = "CNAME"
  ttl     = 300
  records = ["www.example.com."]
}
📊

Quick Reference

Remember these key points when creating Route53 records with Terraform:

  • Always use the correct zone_id from your hosted zone.
  • Match type with appropriate records values.
  • Use ttl to control DNS caching time.
  • Use lists for multiple record values.

Key Takeaways

Use the aws_route53_record resource with zone_id, name, type, ttl, and records to create DNS records.
Ensure the zone_id matches your Route53 hosted zone to avoid misconfiguration.
Match record type with correct record values, e.g., IPs for A records, domain names for CNAME.
Specify ttl to control how long DNS responses are cached.
Use lists for records when you have multiple values for the same DNS record.