Deploying a Node JS Application on AWS ECS Fargate and AWS ECR : A Step-by-Step Guide
In this article, I'll guide you through each step in a simple and easy-to-understand manner. By the end of this guide, you'll be able to deploy your application successfully. Let's get started!
Step 1 : Cloning the Node JS Application from GitHub
The first step is to get a Node JS application from a GitHub repository. If you don't have one, you can use any public repository available on GitHub.
Go to GitHub: Open your web browser and navigate to GitHub.
Search for a Repository: Use the search bar to find a Node JS application repository that you want to clone.
Clone the Repository: Once you've found a suitable repository, click on the green "Code" button and copy the URL. Open your terminal and run the following command to clone the repository to your local machine:
git clone https://github.com/username/repository.git
Replace
https://github.com/username/repository.git
with the URL of your chosen repository.
Step 2 : Building the Dockerfile
A Dockerfile is a script that contains a series of instructions on how to build a Docker image for your application. Most repositories will already have a Dockerfile. If not, you can create one.
Navigate to the Project Directory: Open your terminal and navigate to the directory of the cloned repository.
cd repository
Create/Edit Dockerfile: If there isn't a Dockerfile, create one using a text editor like VSCode or nano. Add the following basic instructions to the Dockerfile:
# Use an official Node.js runtime as a parent image FROM node:14 # Set the working directory WORKDIR /usr/src/app # Copy the package.json and package-lock.json files COPY package*.json ./ # Install dependencies RUN npm install # Copy the rest of the application code COPY . . # Expose the port the app runs on EXPOSE 8080 # Define the command to run the app CMD ["node", "app.js"]
Adjust the file as needed to match your application's requirements.
Build the Docker Image: Run the following command in your terminal to build the Docker image:
docker build -t my-node-app .
Step 3 : Setting Up AWS CLI and Logging In
To interact with AWS services, you'll need to set up the AWS Command Line Interface (CLI) and log in to your AWS account.
Install AWS CLI: Follow the instructions to install AWS CLI from the official documentation.
Configure AWS CLI: Run the following command and enter your AWS credentials (access key, secret key, region, and output format):
aws configure
Login to AWS ECR: Run the following command to 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 : Setting Up an ECS Cluster
An ECS cluster is a logical grouping of tasks or services. It helps in managing the deployment of your containerized applications.
Open AWS Management Console: Go to the AWS Management Console.
Navigate to ECS: In the search bar, type "ECS" and select "Elastic Container Service."
Create a Cluster: Click on "Clusters" in the left-hand menu, then click the "Create Cluster" button. Choose the "Networking only" cluster template and follow the instructions to create your cluster.
Step 5 : Creating a Task Definition
A Task Definition is like a blueprint that describes how your container should be launched.
Create a Repository in ECR: In the AWS Management Console, navigate to the ECR service. Create a new repository to store your Docker image.
Tag and Push Your Docker Image to ECR:
docker tag my-node-app:latest your-account-id.dkr.ecr.your-region.amazonaws.com/my-node-app:latest docker push your-account-id.dkr.ecr.your-region.amazonaws.com/my-node-app:latest
Create Task Definition: In the ECS console, click on "Task Definitions" and then "Create new Task Definition." Select "Fargate" as the launch type and follow the instructions. Use the image URL from ECR in the container definition.
Step 6 : Running the Project
Now that you have everything set up, it's time to run your project.
Run a New Task: Go back to your ECS cluster and click on "Tasks." Then click "Run new Task."
Select Fargate Launch Type: Choose the Fargate launch type and your created Task Definition.
Configure the Task: Set the number of tasks, cluster VPC, and subnets. Click "Run Task."