Creating an AWS EC2 Node server with SSL

Thursday 12 February 2026

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

  1. Log into AWS Console → EC2 Dashboard
  2. Click "Launch Instance"
  3. Name your instance (e.g., "nodejs-server")
  4. Choose Amazon Linux 2023 or Ubuntu Server 22.04 LTS (recommended)
  5. Instance type: t2.micro (free tier) or larger
  6. Create/select a key pair (download .pem file - keep it safe!)
  7. Network settings:
  8. Create/select a VPC
  9. Enable auto-assign public IP
  10. Create a new security group (we'll configure it later)
  11. Configure storage: 8-30 GB
  12. 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

  1. Go to EC2 → Security Groups
  2. Select your instance's security group
  3. Configure Inbound Rules:
  4. SSH (22) - Source: Your IP only (for security)
  5. Custom TCP (3000) - Source: ALB security group (we'll create this next)
  6. Remove any HTTP/HTTPS rules (ALB will handle this)


Part 5. Request SSL Certificate (AWS Certificate Manager)

  1. Go to AWS Certificate Manager (ACM)
  2. Click "Request certificate"
  3. Choose "Request a public certificate"
  4. Add domain names:
  5. yourdomain.com
  6. www.yourdomain.com
  7. Or use *.yourdomain.com for wildcard
  8. Validation method: DNS validation (recommended)
  9. Click "Request"
  10. Add DNS records for validation:
  11. ACM will show you CNAME records
  12. Add these to your domain's DNS settings
  13. Wait for status to change to "Issued" (5-30 minutes)


Part 6: Create Target Group

  1. Go to EC2 → Target Groups
  2. Click "Create target group"
  3. Configure:
  4. Target type: Instances
  5. Target group name: nodejs-tg
  6. Protocol: HTTP
  7. Port: 3000
  8. VPC: Select your VPC
  9. Protocol version: HTTP1
  10. Health checks:
  11. Health check protocol: HTTP
  12. Health check path: /health
  13. Advanced settings (optional):
  14. Healthy threshold: 2
  15. Unhealthy threshold: 2
  16. Timeout: 5
  17. Interval: 30
  18. Click "Next"
  19. Register targets:
  20. Select your EC2 instance
  21. Port: 3000
  22. Click "Include as pending below"
  23. Click "Create target group"


Part 7: Create Application Load Balancer

  1. Go to EC2 → Load Balancers
  2. Click "Create Load Balancer"
  3. Choose Application Load Balancer
  4. Basic configuration:
  5. Name: nodejs-alb
  6. Scheme: Internet-facing
  7. IP address type: IPv4
  8. Network mapping:
  9. VPC: Select your VPC
  10. Mappings: Select at least 2 availability zones
  11. Security groups:
  12. Create new security group or select existing:
  13. Allow HTTP (80) from 0.0.0.0/0
  14. Allow HTTPS (443) from 0.0.0.0/0
  15. Listeners and routing:
  16. HTTP:80: Forward to nodejs-tg
  17. Click "Add listener"
  18. HTTPS:443: Forward to nodejs-tg
  19. Default SSL/TLS certificate: Select your ACM certificate
  20. Click "Create load balancer"


Part 8: Update EC2 Security Group

Now that ALB is created:

  1. Go to EC2 → Security Groups
  2. Select your EC2 instance security group
  3. Edit Inbound Rules for port 3000:
  4. Custom TCP (3000) - Source: Select the ALB security group
  5. Save rules


Part 9: Configure HTTP to HTTPS Redirect

  1. Go to EC2 → Load Balancers
  2. Select your ALB
  3. Click Listeners tab
  4. Select the HTTP:80 listener
  5. Click Actions → Edit listener
  6. Remove the forward action
  7. Add Redirect action:
  8. Protocol: HTTPS
  9. Port: 443
  10. Status code: 301 (permanent redirect)
  11. Save changes


Part 10: Configure Domain DNS

  1. Get your ALB DNS name:
  2. Go to Load Balancers → Select your ALB
  3. Copy the DNS name (looks like: nodejs-alb-1234567890.us-east-1.elb.amazonaws.com)
  4. Configure DNS Records (in your domain registrar):
  5. Add CNAME Record: www → Your ALB DNS name
  6. Add A Record or ALIAS Record (if supported): @ → Your ALB DNS name
  7. Note: If your registrar supports ALIAS records (like Route 53), use that for the root domain. Otherwise:
  8. Alternative: Use Route 53 (recommended for AWS):
  9. Go to Route 53 → Hosted Zones
  10. Create hosted zone for your domain (if not exists)
  11. Create A Record:
  12. Name: @ (root) or leave blank
  13. Type: A
  14. Alias: Yes
  15. Route traffic to: ALB
  16. Region: Your region
  17. Select your load balancer
  18. Create A Record for www:
  19. Name: www
  20. Type: A
  21. Alias: Yes
  22. Route traffic to: ALB
  23. Select your load balancer
  24. Update nameservers at your domain registrar to Route 53 nameservers


Part 11: Verify Everything Works

  1. Wait 5-30 minutes for DNS propagation
  2. Visit http://yourdomain.com → Should redirect to HTTPS
  3. Visit https://yourdomain.com → Should show your app with valid SSL
  4. Check ALB health:
  5. EC2 → Target Groups → Select nodejs-tg
  6. Targets tab → Should show "healthy"


You now have a Node server running on an EC2 with an AWS load balancer! 🥳