How to Point Your Domain to an AWS EC2 Instance
To point a domain to an AWS EC2 instance, create an
A record in your domain's DNS settings that maps your domain name to the EC2 instance's public IP address. Alternatively, use an Elastic IP for a static IP and update the DNS record accordingly.Syntax
When pointing a domain to an EC2 instance, you mainly work with DNS records. The key syntax is creating an A record that links your domain or subdomain to the EC2 instance's IP address.
Domain Name: The domain or subdomain you want to point (e.g., example.com or www.example.com).Record Type: UseAfor IPv4 addresses.Value: The public IP address of your EC2 instance or an Elastic IP.TTL: Time to live, how long DNS caches the record (usually 300 seconds is fine).
dns
example.com. 300 IN A 203.0.113.25
Example
This example shows how to create an A record in AWS Route 53 to point www.example.com to an EC2 instance with a static Elastic IP 203.0.113.25.
hcl
resource "aws_route53_record" "www" { zone_id = "Z3P5QSUBK4POTI" # Your hosted zone ID name = "www" type = "A" ttl = 300 records = ["203.0.113.25"] }
Output
Creates an A record www.example.com pointing to 203.0.113.25 in Route 53
Common Pitfalls
- Using the EC2 public IP directly without Elastic IP: The public IP can change if the instance stops and starts, breaking your domain link.
- Not waiting for DNS propagation: DNS changes can take minutes to hours to update worldwide.
- Forgetting to open port 80/443 in EC2 security groups: Your website won’t be reachable if HTTP/HTTPS ports are blocked.
- Incorrect DNS record type: Using CNAME for root domain instead of A record can cause issues.
dns
;; Wrong: Using EC2 dynamic IP without Elastic IP example.com. 300 IN A 54.123.45.67 ;; Right: Using Elastic IP example.com. 300 IN A 203.0.113.25
Quick Reference
Summary tips for pointing your domain to EC2:
- Always use an Elastic IP for stable IP address.
- Update your domain's DNS A record to the Elastic IP.
- Open necessary ports (80 for HTTP, 443 for HTTPS) in EC2 security groups.
- Use Route 53 or your DNS provider's console to manage DNS records.
- Allow time for DNS changes to propagate (up to 48 hours, usually faster).
Key Takeaways
Use an Elastic IP for your EC2 instance to keep a stable IP address.
Create an A record in your domain's DNS pointing to the Elastic IP.
Ensure EC2 security groups allow web traffic on ports 80 and 443.
DNS changes can take time to propagate; be patient after updates.
Use Route 53 or your DNS provider's dashboard to manage DNS records easily.