Introduction
Amazon Elastic Container Service (ECS) is a fully managed container orchestration service that makes it easy to run and scale containerized applications on AWS. In this tutorial, we will learn how to deploy a Node.js application on ECS using the following steps:
- Set up an ECS cluster.
- Build a Docker image for the Node.js application.
- Push the Docker image to Amazon Elastic Container Registry (ECR)
- Create a task definition for the Node.js application
- Create a service to run the task definition.
Prerequisites
Before we begin, you will need to have the following prerequisites:
- An AWS account
- The AWS CLI installed and configured on your local machine. Node.js and npm installed on your local machine.
- Docker installed on your local machine.
Step 1: Set up an ECS cluster
The first step in deploying our Node.js application on ECS is to set up an ECS cluster. An ECS cluster is a logical grouping of tasks or services that you can place on one or more EC2 instances.
To create an ECS cluster, follow these steps:
- Sign in to the AWS Management Console and navigate to the Amazon ECS dashboard.
- Click the "Clusters" link in the left navigation menu.
- Click the "Create Cluster" button.
- Select the "EC2 Linux + Networking" cluster template, and then click the "Next Step" button.
- Give your cluster a name, and then click the "Create" button.
It will take a few minutes for the cluster to be created. Once it is created, you will see it listed on the Clusters page.
Step 2: Build a Docker image for the Node.js application
The next step is to build a Docker image for our Node.js application. Docker is a containerization platform that packages our application and its dependencies into a single, portable image that can be run on any platform.
To build a Docker image for our Node.js application, follow these steps:
- Create a new directory for your Node.js application and navigate to it.
-
Create a package.json file for your application by running
the following command:
npm init -y
-
install the dependencies for your application. For this
tutorial, we will be using the Express web framework. Run
the following command to install it:
npm install express --save
-
Create a file called "app.js" in the root directory of your
project and add the following code to it:
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello, World!'); }); app.listen(3000, () => { console.log('Listening on port 3000'); });
This code creates a simple Express server that listens on
port 3000 and returns a "Hello, World!" message when you
visit the root URL. -
Create a file called "Dockerfile" in the root directory of
your project and add the following code to it:
FROM node:12 WORKDIR /app COPY package*.json ./ RUN npm install
-
Copy the source code for your application into the Docker
image by adding the following line to your Dockerfile:
COPY . .
-
Expose the port that your application is running on by
adding the following line to your Dockerfile:
EXPOSE 3000
-
Set the command to run when the Docker container starts by
adding the following line to your Dockerfile:
CMD ["node", "app.js"]
Your Dockerfile should now look like this:
FROM node:12 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "app.js"]
-
Build the Docker image by running the following command:
docker build -t nodejs-app .
This will create a Docker image with the tag "nodejs-app".
Step 3: Push the Docker image to Amazon Elastic
Container Registry (ECR)
Now that we have built our Docker image, we need to push it
to Amazon Elastic Container Registry (ECR) so that it can
be used by our ECS cluster. ECR is a fully-managed Docker
container registry that makes it easy to store, manage, and
deploy Docker container images.
To push the Docker image to ECR, follow these steps:
- Create a new ECR repository by following these steps:
Sign in to the AWS Management Console and navigate to
the Amazon ECR dashboard.Click the "Create repository" button.
Give your repository a name, and then click the
"Create a repository" button.
-
Run the following command to log in to your ECR registry:
aws ecr get-login-password --region region | docker login --username AWS --password-stdin account_id.dkr.ecr.region.amazonaws.com
Replace "region" with the region where you created your
ECR repository (e.g. "us-east-1"), and "account_id" with
your AWS account ID. -
Tag the Docker image with the URL of your ECR repository
by running the following command:
docker tag nodejs-app:latest account_id.dkr.ecr.region.amazonaws.com/repository_name: latest
Replace "account_id" with your AWS account ID, "region"
with the region where you created your ECR repository (e.g.
"us-east-1"), and "repository_name" with the name of your
ECR repository.
-
Push the Docker image to your ECR repository by running
the following command:
docker push account_id.dkr.ecr.region.amazonaws.com/repository_name: latest
Step 4: Create a task definition for the Node.js application
Now that our Docker image is stored in ECR, we can create a
task definition for our Node.js application. A task
definition is a blueprint that describes how to run a
containerized application.
To create a task definition for our Node.js application, follow these steps:
- Sign in to the AWS Management Console and navigate to the Amazon ECS dashboard.
- Click the "Task Definitions" link in the left navigation menu.
Click the "Create new Task Definition" button.
Select the "EC2" launch type, and then click the "Next Step" button.
Give your task definition a name, and then select the ECS cluster that you created in Step 1 from the "Cluster" dropdown menu.
In the "Container Definitions" section, click the "Add Container" button.
In the "Container Name" field, enter a name for your container (e.g. "nodejs-app").
In the "Image" field, enter the URL of your ECR repository (e.g."account_id.dkr.ecr.region.amazonaws.com/repository_name:latest").
In the "Port Mappings" section, map the container port to the host port. For our Node.js application, we will map port 3000 in the container to port 80 on the host.
Click the "Add" button to add the container to the task definition.
Click the "Create" button to create the task definition.
Step 5: Create a service to run the task definition
The final step is to create a service that runs our task
definition. A service is a long-running task that is
typically used to run one or more replicas of a task
definition.
To create a service for our task definition, follow these steps:
- Sign in to the AWS Management Console and navigate to the Amazon ECS dashboard.
- Click the "Services" link in the left navigation menu.
- Click the "Create Service" button.
- Select the ECS cluster that you created in Step 1 from the "Cluster" dropdown menu.
- Select the task definition that you created in Step 4 from the "Task Definition" dropdown menu.
- In the "Number of tasks" field, enter the number of tasks that you want to run (e.g. 1).
- In the "Service Name" field, enter a name for your service (e.g. "nodejs-app-service").
- In the "Load Balancer Type" section, select the "Application Load Balancer" option.
- Click the "Next Step" button.
- In the "Configure Network" section, select the "Create new" option for the "VPC" field.
- Click the "Create Service" button to create the service. It will take a few minutes for the service to be created. Once it is created, you will see it listed on the Services page.
Conclusion
In this tutorial, we learned how to deploy a Node.js application on Amazon ECS using the following steps:
- Set up an ECS cluster
- Build a Docker image for the Node.js application
- Push the Docker image to Amazon ECR
- Create a task definition for the Node.js application
- Create a service to run the task definition
Top comments (0)