How to Deploy a NestJS Application to AWS: A Complete Step-by-Step Guide

How to Deploy a NestJS Application to AWS: A Complete Step-by-Step Guide
How to Deploy a NestJS Application to AWS: A Complete Step-by-Step Guide


Deploying a NestJS application to AWS ensures scalability, reliability, and global availability. AWS offers multiple services that you can leverage for deployment, such as EC2, Elastic Beanstalk, and AWS Lambda. This guide walks you through deploying a NestJS app using an EC2 instance for maximum control over the deployment process.

Prerequisites

Before you begin, ensure you have:

  • A NestJS application ready for deployment.
  • An AWS account.
  • Basic knowledge of AWS services like EC2 and Route 53.
  • Installed AWS CLI on your local machine.

1. Setting Up Your NestJS Application

  • Build Your NestJS App
Ensure your application is production-ready by building it:

npm run build

  • Configure the Environment
Create a .env file for environment-specific variables:

PORT=3000
DATABASE_URL=your-database-url

  • Package Your App
Compress your application for easy transfer:

zip -r nestjs-app.zip .


2. Preparing the AWS Environment

Launching an EC2 Instance

  1. Log in to the AWS Management Console.
  2. Navigate to EC2 and click on Launch Instance.
  3. Configure the following:
    • AMI: Choose Amazon Linux 2 or Ubuntu.
    • Instance Type: Select t2.micro (free-tier eligible).
    • Key Pair: Generate a new key pair or utilize an existing one to securely enable SSH access to your instance.
    • Storage: Allocate sufficient storage (e.g., 8GB).

Configuring Security Groups

  1. Modify your instance's security group to allow inbound traffic for necessary services and protocols:
    • Add rules to enable HTTP traffic on port 80 and HTTPS traffic on port 443 for public access to your application.
    • Configure an inbound rule to permit SSH (port 22) connections exclusively from your current IP address to maintain secure remote access.

3. Deploying the NestJS Application

Installing Dependencies on EC2

  • SSH into your EC2 instance:

ssh -i your-key.pem ec2-user@your-ec2-public-ip

  • Update the instance and install Node.js:

sudo yum update -y
sudo curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash - sudo yum install -y nodejs

  • Install pm2 to manage the NestJS application:

sudo npm install -g pm2


Uploading Your Application Code

  • Use SCP to transfer your zipped application to the EC2 instance:

scp -i your-key.pem nestjs-app.zip ec2-user@your-ec2-public-ip:~

  • SSH into the instance and extract the code:

unzip nestjs-app.zip
cd nestjs-app

  • Install the dependencies:

npm install


4. Configuring and Running the Application

  • Set Up Environment Variables
Create a .env file on the EC2 instance with your variables.

  • Run the Application with PM2

pm2 start dist/main.js --name "nestjs-app"
pm2 save pm2 startup

  • Configure NGINX as a Reverse Proxy
Install and configure NGINX to route traffic to your application:

sudo yum install nginx -y
sudo systemctl start nginx sudo systemctl enable nginx


Edit the NGINX configuration:

sudo nano /etc/nginx/conf.d/nestjs.conf


Add the following configuration:

server {
listen 80; server_name your-domain.com; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }


Restart NGINX:

sudo systemctl restart nginx


5. Setting Up a Domain and SSL

Domain Configuration
  • Use AWS Route 53 or another domain registrar to point your domain to your EC2 instance.
Install SSL Certificates


Use Certbot to obtain free SSL certificates:

sudo yum install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your-domain.com -d www.your-domain.com


Set up automatic certificate renewal:

sudo crontab -e


Add the following line:

0 0 * * * certbot renew --quiet


Congratulations! Your NestJS application is now deployed to AWS and accessible via your domain. This setup ensures scalability, reliability, and security for your application. For further optimizations, consider using AWS services like RDS for databases and CloudWatch for monitoring.

Post a Comment

Previous Post Next Post