How to Setup Node.JS Web Server to AWS with ECS and Fargate

2022/10/117 min read
bookmark this

Table of Contents

  1. Introduction
  2. Requirements
  3. Create Simple Node.js App
  4. Build Node.js as Docker Image locally
  5. Verify local Node.js Web Server
  6. Create Image at AWS ECR
  7. Create ECS Cluster
  8. Push Docker Image to AWS ECR Elastic Container Registry
  9. Delete Resources
  10. Conclusion

Introduction

This blog will focus on how to create simple Node.js by using Docker, then later push the docker image to AWS ECR and ECS and use AWS Fargate to serve this Node.js Web Server.

Requirements

  • AWS Account, first of all you'll need an AWS account, if you don't have one already you can create one should be get an AWS free tier for a year.
  • Basic knowledge about AWS, Node.js, Docker, create Image and Container, ECR, ECS, and the Fargate.

Create Simple Node.js App

First, we'll create a simple Node.js Web Application, and build image by using Docker with this app. At here, we'll start create the Node.js Applications by running below commands.

npm init -y
npm install express --save
nano app.js

These commands, first line we'll initialize a npm package.json file with npm init -y, then install the only dependencies which is express. Run nano app.js or create a file manully with the name app.js, then put below content to the app.js.

const express = require('express')
const app = express()

app.get('/', (req, res) => {
    res.send('hello world!')
})

app.listen(7777, () => {
    console.log('server is up on 7777')
});

This code will start up web server by using the express and return the hello worlds! when page get request with port 7777.

You can test the page by type the node app.js at the command console.

Build Node.js as Docker Image locally

This section, we'll start build docker image and create container. Create a Dockerfile at the same directory, put below content to the Dockerfile.

FROM node:8-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN npm install
EXPOSE 7777
CMD ["node", "app.js"]

Now, let's run this command to build docker image, this will build the docker image and store locally.

docker build -t sample-docker-nodejs-app .

Once build docker image succeed, you can run this to list the images and check the image we just created.

docker images

list docker images

Now, run the docker with what we just built to create a container, this tells the docker that to use the image 1722c9508798.

docker run -p 3300:7777 1722c9508798

Verify local Node.js Web Server

Type below http://localhost:3300 to see if able to get response from the container Node.js, once if we get the same heelo world!, means the docker image/container works on the local and ready to setup image/container to AWS.

curl http://localhost:3300

Create Image at AWS ECR

So far what we have done are create a Docker image locally, then use the image and create a container, expose the port 7777 to the caller site as 3300, so we'll do the same but in the cloud with AWS. First we need to create a docker image into AWS, that'll be ECR, Amazon Elastic COntainer Registry, which will be a image repository have we can push our image, and you can set the image either publicly or privately.

In this section, we'll create a private repository for our docker images.

First, go to the AWS console, ECR and click Create repository. Here we choose the Private and for the name, for this blog I just create as below, and we choose the immutability as Enabled, there's few more setting at here, for this blog this will be enough.

Create ECR Repository

Once select everything, click the Create repository, if create repository succeed, you should see your image as below screen.

View created ECR Repository

Push Docker Image to AWS ECR Elastic Container Registry

Once create the AWS ECR Repository, click and go inside then you won't see any image as below. Here we'll push our docker image at here. ECR Repository Images

AWS ECR already has list of command can help to push image, click the View push commands you should see the detail of how to use AWS cli to push local image to the ECR.

First, we'll exist below command to authenticate, once you get successful, you should see Login Succeeded.

aws ecr get-login-password --region us-west-1 | docker login --username AWS --password-stdin {your account id}.dkr.ecr.us-west-1.amazonaws.com

Second, we'll build our docker image, if already use docker build image built locally then can skip this part.

docker build -t my-sample-docker-nodejs-app .

Next, we'll tag our image with ESR's repository

docker tag my-sample-docker-nodejs-app:latest {account id}.dkr.ecr.us-west-1.amazonaws.com/my-sample-docker-nodejs-app:latest

Lastly, we'll push our image to the AWS ECR

docker push {account id}.dkr.ecr.us-west-1.amazonaws.com/my-sample-docker-nodejs-app:latest

Once push the image successful, should be seen the new record at here. ECR Image

Create ECS Cluster

We'll close to finish, so far we have our docker image at ECR, which is expose port as 7777, so start from here we'll create clusters, task to run the docker image.

Click Create Cluster and choose Networking Only. create Cluster

During the configure cluster, just put any name for the cluster, if you have VPC already you can skip that, or create new VPC as blow. create Cluster part 2

Once create cluster is done, should seen this screen that ECS cluster and CloudFormation Stack are created. create Cluster part 3

Create Task Definitions

After created an ECS Cluster, we'll create new task definition, choose the Fargate. create Task definition

When create a new task definition, most of the field at here we'll keep as default, once thing important is that we'll add the container and choose the image we push from our local to the ECR repository. create Task definition - part2

create Task definition - part2

Create Task at Cluster

When finish creating the cluster with our container from ECR, we'll create a task so our app will run. Go to the cluster and choose the one we just created, and click create task. create task

Once create a task for the cluster, AWS should start build container and create public IP address, for testing, you can use copy from below Public IP address and test it out at the browser. create task

One thing to remember is that, need to add the port 7777 to the security policy, once add the inbound port 7777 to the security policy then type the below URL at the browser.

If everything works out, verify the browser should return the same response as from our local.

http://54.183.190.94:7777

That's all we need to cover for this blog, next is delete the resource to avoid unexpected charge.

Verify from the browser

Delete Resources

If you are just create ECR, Farget, and cluster for testing, we need to delete these resource once your testing is done.

DeRegister Task Definition

Delete Task Definition

Delete Clusters

delete Cluster

Conclusion

Above are how we can deploy a simple node.js app to AWS ECS Fargate, this blog cover how to setup node.js and build and run image/container locally and how we can build and run image/container at AWS. Next we'll try use different language or use different compute, or try to automate this.