How to Map a Custom Domain to an EC2 Instance Using Amazon Route 53


1 views

When you need to associate a custom domain with your EC2 instance, you're dealing with three main AWS services:

  • EC2 Instance: Your virtual server with a public DNS (ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com)
  • Route 53: AWS's DNS service where you create hosted zones
  • Domain Registrar: Where you purchased your domain name

Before proceeding, ensure you have:

1. A running EC2 instance with a public IP
2. A registered domain name
3. Route 53 hosted zone created for your domain
4. Nameservers updated at your registrar

The core of the solution lies in creating proper DNS records in Route 53. You'll need to create either:

Option 1: A Record (Recommended)

This directly points your domain to the EC2 instance's IP:

{
  "Name": "example.com",
  "Type": "A",
  "TTL": 300,
  "ResourceRecords": [
    {
      "Value": "EC2_INSTANCE_IP"
    }
  ]
}

Option 2: CNAME Record

This points your domain to the EC2 public DNS name:

{
  "Name": "www.example.com",
  "Type": "CNAME",
  "TTL": 300,
  "ResourceRecords": [
    {
      "Value": "ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com"
    }
  ]
}

Here's how to create these records through the AWS Console:

  1. Navigate to Route 53 service
  2. Select your hosted zone
  3. Click "Create record"
  4. Choose record type (A or CNAME)
  5. Enter your domain/subdomain
  6. Point to your EC2 instance (IP or DNS)
  7. Set TTL (300 seconds is typical)
  8. Save the record

After DNS propagation (which can take up to 48 hours), verify with:

nslookup example.com
dig example.com
ping example.com

For production environments, consider:

  • Creating an Elastic IP for your EC2 instance
  • Setting up ALIAS records in Route 53
  • Configuring HTTPS with ACM certificates
  • Implementing health checks

If your domain isn't resolving:

  • Double-check nameserver settings at registrar
  • Verify record types and values in Route 53
  • Ensure EC2 security groups allow HTTP/HTTPS traffic
  • Check if your instance has a public IP

When you launch an EC2 instance, AWS assigns it a public DNS name like ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com. To use a custom domain (e.g., example.com) with your instance, you'll need to configure DNS records in Route 53.

  • An active EC2 instance with a public IP or Elastic IP
  • A registered domain name
  • Route 53 hosted zone configured for your domain
  • Updated nameservers at your domain registrar

Here's how to create an A record that points your domain to the EC2 instance:

{
  "Comment": "Point domain to EC2 instance",
  "Changes": [
    {
      "Action": "CREATE",
      "ResourceRecordSet": {
        "Name": "example.com",
        "Type": "A",
        "TTL": 300,
        "ResourceRecords": [
          {
            "Value": "YOUR_EC2_PUBLIC_IP"
          }
        ]
      }
    }
  ]
}

For more stability, consider using an Elastic IP:

  1. Allocate an Elastic IP in the EC2 console
  2. Associate it with your instance
  3. Update your A record with the Elastic IP

After DNS propagation (which can take up to 48 hours), verify with:

dig example.com
ping example.com
nslookup example.com

For subdomains or load balancing:

{
  "Comment": "Subdomain configuration",
  "Changes": [
    {
      "Action": "CREATE",
      "ResourceRecordSet": {
        "Name": "app.example.com",
        "Type": "CNAME",
        "TTL": 300,
        "ResourceRecords": [
          {
            "Value": "ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com"
          }
        ]
      }
    }
  ]
}
  • Check security groups allow HTTP/HTTPS traffic
  • Verify instance has a web server running
  • Confirm nameserver changes propagated (using whois)
  • Check Route 53 health checks if configured