,

Deploy a Node.js App (Express or Nest) to AWS ECR and EC2

Posted by






Deploy Node.js App into AWS ECR and EC2

Deploy Node.js App (Express or Nest) into AWS ECR and EC2

Deploying a Node.js app into AWS ECR (Elastic Container Registry) and EC2 (Elastic Compute Cloud) is a common practice for hosting web applications in a scalable and reliable environment. In this article, we will guide you through the process of deploying a Node.js app, specifically using the Express or Nest framework, into AWS ECR and EC2.

Prerequisites

Before getting started, make sure you have the following prerequisites:

  • An AWS account
  • A Node.js app (built using Express or Nest framework)
  • Docker installed on your local machine

Step 1: Dockerize Your Node.js App

The first step is to Dockerize your Node.js app. Create a Dockerfile in the root directory of your Node.js app with the following content:

FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

Once you have the Dockerfile in place, build the Docker image:

docker build -t my-node-app .

Step 2: Push Docker Image to AWS ECR

Login to your AWS account using the AWS CLI:

aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin YOUR_ECR_URI

Tag the Docker image with your ECR repository:

docker tag my-node-app:latest YOUR_ECR_URI/my-node-app:latest

Push the Docker image to your ECR repository:

docker push YOUR_ECR_URI/my-node-app:latest

Step 3: Create and Configure an EC2 Instance

In the AWS Management Console, navigate to EC2 and launch a new EC2 instance. Choose the Amazon Linux 2 AMI and configure the instance with the appropriate security groups, key pairs, and IAM roles.

Step 4: Run Docker Container on EC2 Instance

SSH into your EC2 instance and pull the Docker image from your ECR repository:

docker pull YOUR_ECR_URI/my-node-app:latest

Run the Docker container on the EC2 instance:

docker run -p 3000:3000 YOUR_ECR_URI/my-node-app:latest

Conclusion

You have successfully deployed your Node.js app (Express or Nest) into AWS ECR and EC2. Your app is now running in a containerized environment within the AWS infrastructure, providing scalability, reliability, and security for your application.


0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
coodementor
1 year ago

Can you make video Deploy Node.js App with NestJS into DigitalOcean??