Deploy Node/Express backend to AWS EC2
In this guide, I’ll walk you through the process of deploying a Node.js backend with Express on an AWS EC2 instance. This backend allows CRUD operations on several models and facilitates uploading images and files to S3, hence the presence of AWS configurations in the .env
file.
Create an EC2 Instance
EC2 (Elastic Compute Cloud): EC2 is a web service provided by Amazon Web Services (AWS) that offers resizable compute capacity in the cloud.
Navigate to the AWS console and create an EC2 instance. I typically opt for an Ubuntu Server in the free tier, but you can choose any suitable option.
Connect to the Instance
Once your instance is up and running, connect to it using your preferred method. I find it convenient to save the configuration in my ~/.ssh/config
file. Here's an example:
Host my-ec2
HostName ec2-3-23-23-23.compute-1.amazonaws.com
User ubuntu
IdentityFile ~/.ssh/my-key.pem
Replace HostName
with your instance's public DNS and IdentityFile
with the path to your key. Then, connect using:
ssh my-ec2
Set Up RDS Database
RDS (Relational Database Service): RDS is a managed database service provided by AWS that makes it easy to set up, operate, and scale relational databases in the cloud. RDS supports several popular database engines such as MySQL, PostgreSQL, Oracle, SQL Server, and MariaDB.
I utilize an RDS database to store my app’s data. You can create one in the AWS console, and for this project, I use PostgreSQL. Once created, grant appropriate permissions and obtain the connection string containing the user, password, host, port, and database name.
Install Dependencies
Once connected to the instance, install necessary dependencies. For my setup, I require Node.js, NPM, nvm, nginx, certbot, Git, and PM2. Here’s how to install Node.js and NPM:
sudo apt update
sudo apt install nodejs nginx git certbot python3-certbot-nginx
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
nvm install lts
Generate an SSH key for repository access (optional but recommended):
ssh-keygen -t rsa -b 4096 -C "user@mail.com"
cat ~/.ssh/id_rsa.pub # Copy the key and add it to your repo
Clone the Repo and Install the App
Once dependencies are installed, clone your app’s repository and install dependencies:
git clone git@github.com:user/backend.git backend
cd backend
npm install
Configure the App
Configure your app by creating a .env
file. Here's an example:
NODE_ENV=production
HOST=http://domain
PORT=3000
DATABASE_URL=postgres://user:pass@host_database:port/database_name
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_S3_REGION=
AWS_S3_BUCKET_NAME=
JWT_SECRET=
Configure Nginx
- Configure Nginx to serve your app. Create a configuration file:
sudo nano /etc/nginx/sites-available/my-config
Add server configurations for your domain and SSL certificate using certbot. Then, restart Nginx:
sudo systemctl restart nginx
sudo certbot --nginx -d custom-domain.com
sudo ln -s /etc/nginx/sites-available/my-config /etc/nginx/sites-enabled/
sudo systemctl restart nginx
Route 53
Route 53: Route 53 is a scalable and highly available Domain Name System (DNS) web service provided by AWS. It allows users to register domain names and route internet traffic to resources like EC2 instances, load balancers, and S3 buckets.
If you’re using a custom domain, configure Route 53 to point the domain to the EC2 instance. Create a record in the AWS console and update the nameservers with your domain provider.
Run the App
Finally, start your app with PM2:
PM2, or Process Manager 2, is a popular process manager for Node.js applications. It allows you to manage, monitor, and deploy Node.js applications
cd backend
pm2 start app.js --name my-app
For updates, fetch and pull changes from your repo and restart the app:
cd backend
git fetch && git pull origin main
pm2 restart my-app
With these steps, you can successfully deploy your Node.js backend on an AWS EC2 instance. Happy coding!