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.
Log in to the AWS Management Console and open the EC2 Console.
In the left navigation pane under Network & Security, click Elastic IPs.
Click Allocate Elastic IP address.
Choose Amazon's pool of IPv4 addresses and click Allocate.
Select the newly allocated Elastic IP from the list, click Actions, and choose Associate Elastic IP address.
Set Resource type to Instance.
Select your Instance ID and its Private IP address.
Click Associate.
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.
In the EC2 Console left menu under Network & Security, click Security Groups.
Select the Security Group attached to your EC2 instance.
Click the Inbound rules tab and select Edit inbound rules.
Add the following two rules if they do not already exist:
| Type | Protocol | Port Range | Source | Description |
|---|
| HTTP | TCP | 80 | 0.0.0.0/0 | Allow web traffic from anywhere |
| HTTPS | TCP | 443 | 0.0.0.0/0 | Allow secure web traffic from anywhere |
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 Type | Host / Name | Value / Destination | TTL |
|---|
| A | @ | YOUR_ELASTIC_IP | 600s / 1 Hour |
Add a CNAME Record for www:
| Record Type | Host / Name | Value / Destination | TTL |
|---|
| CNAME | www | yourdomain.com | 600s / 1 Hour |
Scenario B: Mapping a Subdomain (api.yourdomain.com)
Add an A Record:
| Record Type | Host / Name | Value / Destination | TTL |
|---|
| A | api | YOUR_ELASTIC_IP | 600s / 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.
Test DNS Resolution:
Bash
nslookup api.yourdomain.com
Test HTTPS Response:
Bash
curl -I https://api.yourdomain.com