So today I had to setup an EC2 server running a Node server on AWS that needed a load balancer and SSL. After looking online I found a lot of articles with information missing or instructions written for old versions of AWS and even sometimes not in the correct order. So I created this article to help anybody in the same position I was in.
Part 1: Create EC2 Instance
1. Launch EC2 Instance
- Log into AWS Console → EC2 Dashboard
- Click "Launch Instance"
- Name your instance (e.g., "nodejs-server")
- Choose Amazon Linux 2023 or Ubuntu Server 22.04 LTS (recommended)
- Instance type: t2.micro (free tier) or larger
- Create/select a key pair (download .pem file - keep it safe!)
- Network settings:
- Create/select a VPC
- Enable auto-assign public IP
- Create a new security group (we'll configure it later)
- Configure storage: 8-30 GB
- Click "Launch Instance"
2. Connect to Your Instance
ssh -i "your-key.pem" ec2-user@your-ec2-public-ip
(Use ubuntu@ instead of ec2-user@ for Ubuntu)
Part 2. Install Node.js
For Amazon Linux 2023:
sudo dnf update -y
sudo dnf install nodejs -y
sudo dnf install npm -y
For Ubuntu:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install --lts
Part 3: Setup Your Node.js Application
1. Create application directory
mkdir ~/myapp
cd ~/myapp
npm init -y
npm install express
2. Create server (server.js):
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.send('Hello from EC2 with ALB!');
});
app.get('/health', (req, res) => {
res.status(200).send('OK');
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
3. Install PM2 (process manager)
sudo npm install -g pm2
pm2 start server.js
pm2 startup
pm2 save
Part 4: Configure EC2 Security Group
- Go to EC2 → Security Groups
- Select your instance's security group
- Configure Inbound Rules:
- SSH (22) - Source: Your IP only (for security)
- Custom TCP (3000) - Source: ALB security group (we'll create this next)
- Remove any HTTP/HTTPS rules (ALB will handle this)
Part 5. Request SSL Certificate (AWS Certificate Manager)
- Go to AWS Certificate Manager (ACM)
- Click "Request certificate"
- Choose "Request a public certificate"
- Add domain names:
- yourdomain.com
- www.yourdomain.com
- Or use *.yourdomain.com for wildcard
- Validation method: DNS validation (recommended)
- Click "Request"
- Add DNS records for validation:
- ACM will show you CNAME records
- Add these to your domain's DNS settings
- Wait for status to change to "Issued" (5-30 minutes)
Part 6: Create Target Group
- Go to EC2 → Target Groups
- Click "Create target group"
- Configure:
- Target type: Instances
- Target group name: nodejs-tg
- Protocol: HTTP
- Port: 3000
- VPC: Select your VPC
- Protocol version: HTTP1
- Health checks:
- Health check protocol: HTTP
- Health check path: /health
- Advanced settings (optional):
- Healthy threshold: 2
- Unhealthy threshold: 2
- Timeout: 5
- Interval: 30
- Click "Next"
- Register targets:
- Select your EC2 instance
- Port: 3000
- Click "Include as pending below"
- Click "Create target group"
Part 7: Create Application Load Balancer
- Go to EC2 → Load Balancers
- Click "Create Load Balancer"
- Choose Application Load Balancer
- Basic configuration:
- Name: nodejs-alb
- Scheme: Internet-facing
- IP address type: IPv4
- Network mapping:
- VPC: Select your VPC
- Mappings: Select at least 2 availability zones
- Security groups:
- Create new security group or select existing:
- Allow HTTP (80) from 0.0.0.0/0
- Allow HTTPS (443) from 0.0.0.0/0
- Listeners and routing:
- HTTP:80: Forward to nodejs-tg
- Click "Add listener"
- HTTPS:443: Forward to nodejs-tg
- Default SSL/TLS certificate: Select your ACM certificate
- Click "Create load balancer"
Part 8: Update EC2 Security Group
Now that ALB is created:
- Go to EC2 → Security Groups
- Select your EC2 instance security group
- Edit Inbound Rules for port 3000:
- Custom TCP (3000) - Source: Select the ALB security group
- Save rules
Part 9: Configure HTTP to HTTPS Redirect
- Go to EC2 → Load Balancers
- Select your ALB
- Click Listeners tab
- Select the HTTP:80 listener
- Click Actions → Edit listener
- Remove the forward action
- Add Redirect action:
- Protocol: HTTPS
- Port: 443
- Status code: 301 (permanent redirect)
- Save changes
Part 10: Configure Domain DNS
- Get your ALB DNS name:
- Go to Load Balancers → Select your ALB
- Copy the DNS name (looks like: nodejs-alb-1234567890.us-east-1.elb.amazonaws.com)
- Configure DNS Records (in your domain registrar):
- Add CNAME Record: www → Your ALB DNS name
- Add A Record or ALIAS Record (if supported): @ → Your ALB DNS name
- Note: If your registrar supports ALIAS records (like Route 53), use that for the root domain. Otherwise:
- Alternative: Use Route 53 (recommended for AWS):
- Go to Route 53 → Hosted Zones
- Create hosted zone for your domain (if not exists)
- Create A Record:
- Name: @ (root) or leave blank
- Type: A
- Alias: Yes
- Route traffic to: ALB
- Region: Your region
- Select your load balancer
- Create A Record for www:
- Name: www
- Type: A
- Alias: Yes
- Route traffic to: ALB
- Select your load balancer
- Update nameservers at your domain registrar to Route 53 nameservers
Part 11: Verify Everything Works
- Wait 5-30 minutes for DNS propagation
- Visit http://yourdomain.com → Should redirect to HTTPS
- Visit https://yourdomain.com → Should show your app with valid SSL
- Check ALB health:
- EC2 → Target Groups → Select nodejs-tg
- Targets tab → Should show "healthy"
You now have a Node server running on an EC2 with an AWS load balancer! 🥳