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 Instance1. Launch EC2 Instance
2. Connect to Your Instancessh -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 -ysudo dnf install nodejs -ysudo dnf install npm -y
For Ubuntu:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bashsource ~/.bashrcnvm install --lts
Part 3: Setup Your Node.js Application
1. Create application directorymkdir ~/myappcd ~/myappnpm init -ynpm 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 pm2pm2 start server.jspm2 startuppm2 save
Part 4: Configure EC2 Security Group
Part 5. Request SSL Certificate (AWS Certificate Manager)
Part 6: Create Target Group
Part 7: Create Application Load Balancer
Part 8: Update EC2 Security GroupNow that ALB is created:
Part 9: Configure HTTP to HTTPS Redirect
Part 10: Configure Domain DNS
Part 11: Verify Everything Works
You now have a Node server running on an EC2 with an AWS load balancer! 🥳