Configuring DNS Records for AWS EC2 A Records, CNAME, and Subdomains
Learn how to configure DNS records to point your domain name to your AWS EC2 instance A records for IPv4, CNAME for subdomains, and best practices.
Configuring DNS Records for AWS EC2
After buying a domain, you need to configure DNS records to point it to your EC2 instance. This guide covers A records, CNAME records, and subdomain setup.
Prerequisites
- A registered domain name
- An EC2 instance with an Elastic IP
- Access to your DNS provider (Cloudflare, Namecheap, Route 53, etc.)
Step 1: Allocate an Elastic IP
Before configuring DNS, ensure your EC2 instance has a static IP:
# Via AWS CLI aws ec2 allocate-address --domain vpc # Associate with your instance aws ec2 associate-address --instance-id i-xxxxx --allocation-id eipalloc-xxxxx
Or via AWS Console:
- Go to EC2 → Elastic IPs
- Click Allocate Elastic IP address
- Select it, Actions → Associate Elastic IP address
- Select your instance and associate
Why Elastic IP? The default public IP changes when you stop/start the instance. An Elastic IP is static it never changes.
Step 2: Create an A Record (Root Domain)
Point yourdomain.com to your EC2 IP:
On Cloudflare:
- DNS → Records → Add Record
- Type:
A - Name:
@(represents root domain) - IPv4 address:
12.34.56.78(your Elastic IP) - Proxy status: DNS only (gray cloud)
- TTL: Auto
- Save
On Namecheap (Advanced DNS):
- Add New Record
- Type:
A Record - Host:
@ - Value:
12.34.56.78 - TTL: 5 min
- Save All Changes
Step 3: Create a CNAME for www
Point www.yourdomain.com to yourdomain.com:
On Cloudflare:
- Add Record
- Type:
CNAME - Name:
www - Target:
yourdomain.com - Proxy status: DNS only
- Save
Step 4: Create A Records for Subdomains
Point api.yourdomain.com to your EC2:
On Cloudflare:
- Add Record
- Type:
A - Name:
api - IPv4 address:
12.34.56.78 - Proxy status: DNS only
- Save
Repeat for other subdomains:
admin→12.34.56.78(admin panel)app→12.34.56.78(app subdomain)
Step 5: Configure Nginx for Subdomains
Update your Nginx server blocks:
# API server block server { listen 80; server_name api.yourdomain.com; location / { proxy_pass http://localhost:3000; # ... proxy headers ... } } # Frontend server block server { listen 80; server_name yourdomain.com www.yourdomain.com; root /home/ubuntu/frontend/dist; location / { try_files $uri $uri/ /index.html; } }
sudo nginx -t && sudo systemctl reload nginx
Step 6: Add SSL for Each Subdomain
# Single domain sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com # API subdomain sudo certbot --nginx -d api.yourdomain.com # All at once sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com -d api.yourdomain.com
Step 7: Verify DNS
# Check A record dig yourdomain.com A +short # Should return: 12.34.56.78 # Check CNAME dig www.yourdomain.com CNAME +short # Should return: yourdomain.com. # Check subdomain dig api.yourdomain.com A +short # Should return: 12.34.56.78 # Check from different DNS servers nslookup yourdomain.com 8.8.8.8 nslookup yourdomain.com 1.1.1.1
Using AWS Route 53
If using Route 53 for DNS:
- Go to Route 53 → Hosted zones
- Create hosted zone for yourdomain.com
- Update nameservers at your registrar
- Create records:
- Type A, Name: (empty for root), Value: your Elastic IP
- Type A, Name: api, Value: your Elastic IP
- Type CNAME, Name: www, Value: yourdomain.com
Common DNS Issues
DNS not propagating:
- Wait 5-60 minutes (TTL-dependent)
- Check with dnschecker.org for global propagation
- Ensure nameservers are correct at the registrar
Wrong IP returned:
- Check the A record value
- Clear local DNS cache:
sudo dscacheutil -flushcache(Mac) oripconfig /flushdns(Windows)
Subdomain not working:
- Ensure the A record for the subdomain exists
- Ensure Nginx has a server block with the correct server_name
- Check that the subdomain resolves:
dig api.yourdomain.com
The Takeaway
Configuring DNS for EC2 involves: allocating an Elastic IP (static IP), creating A records for the root domain and subdomains (api, admin), creating a CNAME for www, configuring Nginx server blocks with matching server_name, and adding SSL with certbot for each domain. Verify with dig or dnschecker.org.
First allocate an Elastic IP and associate it with your instance. Then create an A record in your DNS provider: Type A, Name @ (for root) or api (for subdomain), Value (your Elastic IP). Verify with dig yourdomain.com A +short.
The default public IP changes when you stop and start the EC2 instance. An Elastic IP is a static IP that persists across stop/start cycles. Without it, your DNS A record would point to the wrong IP after a restart.
Create an A record: Type A, Name api, Value (your Elastic IP). Then add an Nginx server block with server_name api.yourdomain.com and the appropriate proxy_pass or root. Finally, get SSL with sudo certbot --nginx -d api.yourdomain.com.
Create a hosted zone for your domain, update nameservers at your registrar, then create records: A record for root (empty name, Elastic IP value), A record for subdomains (api, Elastic IP), and CNAME for www (yourdomain.com).
Typically 5 minutes to 1 hour for new records. Changes to existing records depend on the TTL (5 min for testing, 1 hour for production). Full global propagation can take up to 24 hours. Check with dnschecker.org or whatsmydns.net.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

