🐶
Terraform

Terraform Route 53 Record to ALB Configuration

By Filip on 10/05/2024

Learn how to use Terraform to effortlessly create a Route 53 record that directs traffic to your Application Load Balancer (ALB) in AWS.

Terraform Route 53 Record to ALB Configuration

Table of Contents

Introduction

This guide explains how to connect your domain name to an Application Load Balancer (ALB) in AWS using Route 53. Before you begin, ensure you have a registered domain name in Route 53 and an ALB configured in your AWS account. You'll need your domain name and the ALB's DNS name, which you can find in the ALB details within the EC2 console. To create the connection, you'll set up an ALIAS record in Route 53. This involves using Terraform's aws_route53_record resource, specifying 'A' as the record type and configuring the 'alias' block with your ALB's DNS name and your Route 53 hosted zone ID. Once you've saved your Terraform configuration, run 'terraform apply' to create the record. Remember that ALIAS records are the correct choice for ALBs, not CNAME records. Also, ensure your ALB has properly configured listeners and targets for health checks. After the changes are applied, allow time for DNS propagation. If you encounter issues, verify your ALB's DNS name, zone ID, and the ALB's health status. For Terraform errors, review the messages for syntax problems or permission issues.

Step-by-Step Guide

To connect your domain name to an Application Load Balancer (ALB) using Route 53, you'll need to create an ALIAS record. Here's a breakdown:

  1. Prerequisites:

    • You have a registered domain name in Route 53.
    • You have an ALB set up in your AWS account.
  2. Gather Information:

    • Domain Name: The domain you want to use (e.g., example.com).
    • ALB DNS Name: Found in the ALB's details within the EC2 console. It usually looks like your-alb-name-1234567890.us-east-1.elb.amazonaws.com.
  3. Create the ALIAS Record:

    • Terraform: Use the aws_route53_record resource with type = "A" and alias block.
      • Set name to your desired subdomain (e.g., blog for blog.example.com or leave empty for the root domain).
      • Within the alias block:
        • name should point to your ALB's DNS name.
        • zone_id is the Route 53 hosted zone ID where your domain is managed.
        • evaluate_target_health should generally be set to true for ALBs.
  4. Apply Changes:

    • Save your Terraform configuration.
    • Run terraform apply to create the record in Route 53.

Important Considerations:

  • Record Name: The name attribute in aws_route53_record determines the subdomain. An empty string represents the root domain.
  • ALIAS vs CNAME: For ALBs, always use an ALIAS record. CNAME records have limitations and aren't suitable for the root domain.
  • Health Checks: ALIAS records inherently use Route 53 health checks directed at the ALB. Ensure your ALB has listeners and targets configured correctly.
  • Propagation Time: DNS changes can take some time to propagate globally.

Troubleshooting:

  • Record Not Working: Double-check the ALB's DNS name, zone ID, and that the ALB is healthy.
  • Terraform Errors: Carefully review error messages for hints. Common issues include incorrect syntax or missing permissions.

Code Example

This Terraform code configures an AWS Route 53 ALIAS record to point your root domain name to an Application Load Balancer (ALB). It uses variables for flexibility and includes health check configuration. You need to provide your domain name, ALB DNS name, and hosted zone ID.

# Configure the AWS Provider
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0" # Use the latest version compatible with your setup
    }
  }
}

# Variables (Replace with your actual values)
variable "domain_name" {
  default = "example.com"
}

variable "alb_dns_name" {
  default = "your-alb-name-1234567890.us-east-1.elb.amazonaws.com"
}

variable "hosted_zone_id" {
  # Get this from the Route 53 console
  default = "YOUR_HOSTED_ZONE_ID" 
}

# Create an ALIAS record for the root domain
resource "aws_route53_record" "alb_record" {
  zone_id = var.hosted_zone_id
  name    = var.domain_name
  type    = "A"

  alias {
    name                   = var.alb_dns_name
    zone_id                = var.hosted_zone_id # Use the same hosted zone ID
    evaluate_target_health = true
  }
}

Explanation:

  • Variables: The code uses variables to store your domain name, ALB DNS name, and hosted zone ID. This makes it easy to reuse the code for different environments or ALBs.
  • aws_route53_record Resource: This resource defines a new Route 53 record.
    • zone_id: Specifies the Route 53 hosted zone where the record will be created.
    • name: Sets the subdomain. In this case, it's an empty string ("") to target the root domain (example.com).
    • type: Set to "A" for an address record.
    • alias: Configures the record as an ALIAS record.
      • name: The ALB's DNS name.
      • zone_id: The hosted zone ID of the ALB (which is the same as your domain's hosted zone ID).
      • evaluate_target_health: Enables Route 53 health checks on the ALB.

How to Use:

  1. Replace Placeholders: Update the variables with your actual domain name, ALB DNS name, and hosted zone ID.
  2. Run Terraform:
    • Initialize Terraform: terraform init
    • Apply the configuration: terraform apply

Additional Notes:

  • Subdomains: To create an ALIAS record for a subdomain (e.g., blog.example.com), set the name attribute to the desired subdomain ("blog" in this case).
  • Multiple Records: You can create multiple aws_route53_record resources to define records for different subdomains or purposes.
  • Destroy Resources: When you're done, you can remove the record using terraform destroy.

Additional Notes

  • Weighted Routing: You can create multiple ALIAS records with the same name but different weights to distribute traffic between multiple ALBs or other endpoints. This is useful for blue/green deployments or canary releases.
  • Latency-Based Routing: Route 53 can automatically route traffic to the ALB closest to the user based on latency. This can improve performance for globally distributed applications.
  • Failover: You can configure Route 53 health checks to monitor your ALB's health. If an ALB becomes unhealthy, Route 53 will automatically route traffic to a healthy ALB.
  • DNSSEC: Consider enabling DNSSEC for your domain to enhance security by digitally signing DNS responses.
  • Import Existing Record: If you already have an A record for your domain, you can import it into Terraform using the aws_route53_record data source and then modify it to be an ALIAS record.
  • Alternative to Terraform: You can also manage Route 53 records using the AWS Management Console, AWS CLI, or other infrastructure-as-code tools.
  • Pricing: Route 53 charges for hosted zones and DNS queries. ALIAS records are a cost-effective way to route traffic to ALBs as they don't incur additional DNS query charges.
  • Documentation: Refer to the official AWS documentation for the most up-to-date information on Route 53, ALBs, and Terraform.

Summary

This guide outlines how to connect your domain name to an Application Load Balancer (ALB) using an ALIAS record in Route 53.

Before You Begin:

  • Ensure you have a registered domain name in Route 53.
  • Have an ALB set up within your AWS account.

Steps:

  1. Gather Information:

    • Domain Name: The domain you want to use (e.g., example.com).
    • ALB DNS Name: Found in the ALB details within the EC2 console (e.g., your-alb-name-1234567890.us-east-1.elb.amazonaws.com).
  2. Create the ALIAS Record (using Terraform):

    • Utilize the aws_route53_record resource with type = "A" and the alias block.
    • Set the name attribute to your desired subdomain (e.g., blog for blog.example.com) or leave it empty for the root domain.
    • Within the alias block:
      • Set name to your ALB's DNS name.
      • Set zone_id to the Route 53 hosted zone ID where your domain is managed.
      • Set evaluate_target_health to true for ALBs.
  3. Apply Changes:

    • Save your Terraform configuration.
    • Run terraform apply to create the record in Route 53.

Key Points:

  • ALIAS Records are Essential: Always use ALIAS records for ALBs, as CNAME records have limitations and are unsuitable for the root domain.
  • Health Checks: ALIAS records automatically utilize Route 53 health checks targeting the ALB. Ensure your ALB's listeners and targets are correctly configured.
  • Propagation Time: DNS changes require time to propagate globally.

Troubleshooting:

  • Record Not Functioning: Verify the ALB's DNS name, zone ID, and health status.
  • Terraform Errors: Review error messages for clues. Common issues include syntax errors or insufficient permissions.

Conclusion

By following these steps, you can effectively connect your domain name to an Application Load Balancer using Route 53, ensuring traffic is routed efficiently and reliably to your applications. Remember to leverage Terraform for streamlined infrastructure management and refer to the provided code example for a practical starting point. Always consult the AWS documentation for the latest features, updates, and best practices.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait