Day 85 of 90 Days of DevOps Challenge: Deploying a Node.js Application on AWS ECS Fargate and ECR
Table of contents
In the world of cloud computing, AWS (Amazon Web Services) offers a powerful suite of services for deploying and managing applications. In this blog post, we will walk through the steps to deploy a Node.js application using AWS Elastic Container Service (ECS) with Fargate and Amazon Elastic Container Registry (ECR). This project will showcase how to build, tag, and run a Dockerized Node.js application in a fully managed environment.
Project Overview
The goal of this project is to deploy a Node.js application sourced from GitHub to AWS ECS Fargate, allowing for seamless scaling and management of containerized applications. We will also utilize AWS ECR to store the Docker images.
Key Components
1. AWS ECS Fargate
AWS Elastic Container Service (ECS) with Fargate is a serverless compute engine for containers that allows you to run containers without managing the underlying servers. You only pay for the compute and storage resources you use.
2. AWS ECR
Amazon Elastic Container Registry (ECR) is a fully managed Docker container registry that makes it easy to store, manage, and deploy Docker container images.
3. Node.js
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, designed for building scalable network applications.
Implementation Steps
Step 1: Get the Node.js Application
Clone the Repository: First, obtain the Node.js application code from GitHub.
git clone <your-nodejs-app-repo-url> cd <your-nodejs-app-directory>
Step 2: Build the Docker Image
Create the Dockerfile: Ensure that your Node.js project has a
Dockerfile
. If not, create one in the root directory. Here’s a sampleDockerfile
for a Node.js application:# Use the official Node.js image FROM node:14 # Set the working directory WORKDIR /app # Copy package.json and package-lock.json COPY package*.json ./ # Install dependencies RUN npm install # Copy application files COPY . . # Expose the application port EXPOSE 3000 # Start the application CMD ["npm", "start"]
Build the Docker Image: Use the following command to build the Docker image:
docker build -t your-dockerhub-username/nodejs-app:latest .
Step 3: Set Up AWS CLI and Log In to ECR
Install AWS CLI: If you haven't already, install the AWS Command Line Interface (CLI) by following the instructions on the official AWS website.
Configure AWS CLI: Run the following command to configure the AWS CLI with your credentials:
aws configure
You will be prompted to enter your AWS Access Key, Secret Key, region, and output format.
Log In to ECR: Authenticate Docker to your Amazon ECR registry:
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <your-account-id>.dkr.ecr.<your-region>.amazonaws.com
Step 4: Push the Docker Image to ECR
Create an ECR Repository: Create an ECR repository to store your Docker image:
aws ecr create-repository --repository-name nodejs-app
Tag the Docker Image: Tag your Docker image to match the ECR repository:
docker tag your-dockerhub-username/nodejs-app:latest <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/nodejs-app:latest
Push the Docker Image to ECR: Push the Docker image to ECR:
docker push <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/nodejs-app:latest
Step 5: Set Up an ECS Cluster
Create an ECS Cluster: Create a new ECS cluster using the following command:
aws ecs create-cluster --cluster-name nodejs-cluster
Step 6: Create a Task Definition
Create a Task Definition: Create a JSON file named
task-definition.json
with the following content, replacing the necessary placeholders:{ "family": "nodejs-app", "containerDefinitions": [ { "name": "nodejs-app", "image": "<your-account-id>.dkr.ecr.<your-region>.amazonaws.com/nodejs-app:latest", "memory": 512, "cpu": 256, "essential": true, "portMappings": [ { "containerPort": 3000, "hostPort": 3000 } ] } ] }
Register the Task Definition: Use the following command to register your task definition:
aws ecs register-task-definition --cli-input-json file://task-definition.json
Step 7: Run the Task
Run the Task on ECS: Run your task in the ECS cluster:
aws ecs run-task --cluster nodejs-cluster --task-definition nodejs-app --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[<your-subnet-id>],securityGroups=[<your-security-group-id>],assignPublicIp=ENABLED}"
Replace
<your-subnet-id>
and<your-security-group-id>
with your specific subnet and security group IDs.
Conclusion
Deploying a Node.js application on AWS ECS Fargate and ECR not only demonstrates your ability to work with cloud services but also highlights your skills in containerization and orchestration. By following the steps outlined in this blog post, you can efficiently deploy and manage your applications in a serverless environment, allowing for automatic scaling and load balancing.
This project serves as a strong addition to your portfolio and a practical example of utilizing AWS services to build scalable applications.
Subscribe to my newsletter
Read articles from Tushar Pant directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by