Facebook Pixel

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:

  1. Go to EC2 → Elastic IPs
  2. Click Allocate Elastic IP address
  3. Select it, Actions → Associate Elastic IP address
  4. 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:

  1. DNS → Records → Add Record
  2. Type: A
  3. Name: @ (represents root domain)
  4. IPv4 address: 12.34.56.78 (your Elastic IP)
  5. Proxy status: DNS only (gray cloud)
  6. TTL: Auto
  7. Save

On Namecheap (Advanced DNS):

  1. Add New Record
  2. Type: A Record
  3. Host: @
  4. Value: 12.34.56.78
  5. TTL: 5 min
  6. Save All Changes

Step 3: Create a CNAME for www

Point www.yourdomain.com to yourdomain.com:

On Cloudflare:

  1. Add Record
  2. Type: CNAME
  3. Name: www
  4. Target: yourdomain.com
  5. Proxy status: DNS only
  6. Save

Step 4: Create A Records for Subdomains

Point api.yourdomain.com to your EC2:

On Cloudflare:

  1. Add Record
  2. Type: A
  3. Name: api
  4. IPv4 address: 12.34.56.78
  5. Proxy status: DNS only
  6. Save

Repeat for other subdomains:

  • admin12.34.56.78 (admin panel)
  • app12.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:

  1. Go to Route 53 → Hosted zones
  2. Create hosted zone for yourdomain.com
  3. Update nameservers at your registrar
  4. 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) or ipconfig /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.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.