Deploying a Node.js application on Amazon Elastic Container Service (ECS)
Tags: aws, node, javascript, beginners
Published: 2023-01-09 17:09:48


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:

1. Set up an ECS cluster.
2. Build a Docker image for the Node.js application.
3. Push the Docker image to Amazon Elastic Container Registry 
   (ECR)
4. Create a task definition for the Node.js application
5. 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:

1. Sign in to the AWS Management Console and navigate to the 
   Amazon ECS dashboard.
2. Click the "Clusters" link in the left navigation menu.
3. Click the "Create Cluster" button.
4. Select the "EC2 Linux + Networking" cluster template, and 
   then click the "Next Step" button.
5. 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:

1. Create a new directory for your Node.js application and 
   navigate to it.
2. Create a package.json file for your application by running 
   the following command: 
    ```
    npm init -y
    ```
3. 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
    ```
4. 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.

    
   
5. 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

    ```
6. Copy the source code for your application into the Docker 
   image by adding the following line to your Dockerfile:
    

    ```
    COPY . .
    ```
7. Expose the port that your application is running on by 
   adding the following line to your Dockerfile:
    ```
    EXPOSE 3000
    ```
8. 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"]
    ```
9. 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:
   
   1. Sign in to the AWS Management Console and navigate to 
      the Amazon ECR dashboard.

   2. Click the "Create repository" button.

   3. 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:

1.  Sign in to the AWS Management Console and navigate to the 
    Amazon ECS dashboard.
2.  Click the "Task Definitions" link in the left navigation 
    menu.
3. Click the "Create new Task Definition" button.

4. Select the "EC2" launch type, and then click the "Next Step" button.
5. Give your task definition a name, and then select the ECS cluster that you created in Step 1 from the "Cluster" dropdown menu.
6. In the "Container Definitions" section, click the "Add Container" button.
7. In the "Container Name" field, enter a name for your container (e.g. "nodejs-app").
8. In the "Image" field, enter the URL of your ECR repository (e.g."account_id.dkr.ecr.region.amazonaws.com/repository_name:latest").
9. 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.
10. Click the "Add" button to add the container to the task definition.
11. 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:

1. Sign in to the AWS Management Console and navigate to the Amazon ECS dashboard.
2. Click the "Services" link in the left navigation menu.
3. Click the "Create Service" button.
4. Select the ECS cluster that you created in Step 1 from the "Cluster" dropdown menu.
5. Select the task definition that you created in Step 4 from the "Task Definition" dropdown menu.
6. In the "Number of tasks" field, enter the number of tasks that you want to run (e.g. 1).
7. In the "Service Name" field, enter a name for your service (e.g. "nodejs-app-service").
8. In the "Load Balancer Type" section, select the "Application Load Balancer" option.
9. Click the "Next Step" button.
10. In the "Configure Network" section, select the "Create new" option for the "VPC" field.
11. 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:

1. Set up an ECS cluster
2. Build a Docker image for the Node.js application
3. Push the Docker image to Amazon ECR
4. Create a task definition for the Node.js application
5. Create a service to run the task definition