AWS  

Complete Guide: Mapping a Custom Domain to an AWS EC2 Instance

Mapping a custom domain (e.g., yourdomain.com or api.yourdomain.com) to an Amazon EC2 instance involves reserving an Elastic IP (Static IPv4), opening Security Group ports, updating your DNS registrar records, and binding your web server with an SSL/TLS certificate.

Technical Overview & Architecture

User Request ──> https://api.yourdomain.com
                        │
                        ▼ (DNS Registrar - A Record)
                 AWS Elastic IP (Static IPv4)
                        │
                        ▼ (EC2 Security Group)
                   Ports 80/443 Open
                        │
                        ▼ (AWS EC2 Web Server)
            Nginx / Apache / IIS / Reverse Proxy
  • AWS Infrastructure: Linux or Windows EC2 Instance.

  • Network Requirement: AWS Elastic IP Address (Static IPv4).

  • DNS Registrar Access: Ability to edit DNS zone records (GoDaddy, Route 53, Cloudflare, Namecheap, etc.).

Step 1: Allocate and Associate an AWS Elastic IP

By default, EC2 public IP addresses are dynamic and change every time the instance is stopped or restarted. An Elastic IP provides a persistent public IPv4 address.

  1. Log in to the AWS Management Console and open the EC2 Console.

  2. In the left navigation pane under Network & Security, click Elastic IPs.

  3. Click Allocate Elastic IP address.

  4. Choose Amazon's pool of IPv4 addresses and click Allocate.

  5. Select the newly allocated Elastic IP from the list, click Actions, and choose Associate Elastic IP address.

  6. Set Resource type to Instance.

  7. Select your Instance ID and its Private IP address.

  8. Click Associate.

  9. Copy your Elastic IP address for use in Step 3.

Step 2: Configure EC2 Security Group Inbound Rules

Ensure your EC2 instance permits incoming HTTP (Port 80) and HTTPS (Port 443) traffic.

  1. In the EC2 Console left menu under Network & Security, click Security Groups.

  2. Select the Security Group attached to your EC2 instance.

  3. Click the Inbound rules tab and select Edit inbound rules.

  4. Add the following two rules if they do not already exist:

TypeProtocolPort RangeSourceDescription
HTTPTCP800.0.0.0/0Allow web traffic from anywhere
HTTPSTCP4430.0.0.0/0Allow secure web traffic from anywhere
  1. Click Save rules.

Step 3: Configure DNS Records at Your Registrar

Log in to your DNS management provider (GoDaddy, Route 53, Cloudflare, Namecheap, etc.) and create the necessary DNS records.

Scenario A: Mapping a Root Domain (yourdomain.com)

Add an A Record:

Record TypeHost / NameValue / DestinationTTL
A@YOUR_ELASTIC_IP600s / 1 Hour

Add a CNAME Record for www:

Record TypeHost / NameValue / DestinationTTL
CNAMEwwwyourdomain.com600s / 1 Hour

Scenario B: Mapping a Subdomain (api.yourdomain.com)

Add an A Record:

Record TypeHost / NameValue / DestinationTTL
AapiYOUR_ELASTIC_IP600s / 1 Hour

Step 4: Configure the Web Server on EC2

Connect to your EC2 instance via SSH (Linux) or RDP (Windows) and configure your web server/reverse proxy.

Nginx Configuration (Linux EC2)

Create or edit your site configuration file in /etc/nginx/sites-available/yourdomain.conf:

Nginx

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com api.yourdomain.com;

    location / {
        proxy_pass http://localhost:5000; # Forward requests to your application port
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'keep-alive';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Enable the configuration and reload Nginx:

Bash

sudo ln -s /etc/nginx/sites-available/yourdomain.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 5: Provision Free SSL/TLS Certificates (HTTPS)

Encrypt traffic to your EC2 instance using Let's Encrypt and Certbot.

On Ubuntu / Debian EC2:

Bash

sudo apt update
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com -d api.yourdomain.com

On Amazon Linux 2023 EC2:

Bash

sudo dnf install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com -d api.yourdomain.com

Certbot will automatically verify your domain, issue the SSL certificate, and update your Nginx configuration to enforce HTTPS traffic.

Step 6: Verification & Testing

Verify that DNS resolution, network connectivity, and SSL certificate bindings are functioning properly.

  1. Test DNS Resolution:

    Bash

    nslookup api.yourdomain.com
    
  2. Test HTTPS Response:

    Bash

    curl -I https://api.yourdomain.com