Deploying a nodejs application on AWS Fargate

45 min

Description

You’ve been recently hired as a Junior DevOps Engineer for the Company FlyTotalControl; They recently developed a nodejs application and deployed it on an AWS ECS Cluster (EC2). The Cluster required constant updates, meaning they have to manage the fleet of EC2 instances. At the last AWS Summit they heard about the serverless solution Fargate; solution where you only have to deploy your containers and AWS manages the underlying infrastructure for you. You’ve been asked to deploy a demo version of an application on Fargate.

Learning Objectives

Step 1 : Create IAM Role

First things first, let’s create an IAM role, EcsTaskExecutionRole

This role will be used by Fargate .

1. Open the IAM console, and select Create role

2. Select AWS service > EC2 > Next Permissions

3. Select AmazonECSTaskExecutionRolePolicy and click on “Next

4. Name your role “EcsTaskExecutionRole” , then click on “Create role”

Step 2 : Create an Application Load Balancer

ECS Fargate can be configured with an Load Balancer to distribute traffic evenly across the tasks in your service.
We will create one ALB (Application Type Load Balancer)

1. Open the EC2 console, and select Load Balancers, then Create Load Balancer

2. Select Application Load Balancer and Create

3. Name your Load Balancer
Name :
fargate-alb

Then, select the VPC where you want to run your tasks, and the subnets (it’s important to select more than one subnet)

4. Create a new security group, allowing incoming request on HTTP on your load balancer

Name your security group fargate-alb-SG

Create Add Rule

5. Now, let’s create a new target group, this is a group of ip where the load balancer will send the traffic

Name :  fargate-target-group

Target type : IP

Leave the other options by default, then click Next

6. Leave the options by default and click Next

Step 3 : Create the ECS Fargate Cluster

Now that we have our load balancer, let’s create the ECS Fargate Cluster

1. Open the ECS console, and select Create Cluster

2. Select Networking only

3. Name your Cluster
Cluster name :
DemoFargate

Then, click Create

Step 4 : Create a Fargate Task Definition

Let’s create a new task definition

1. Open the ECS console > Task Definitions > Create new Task Definition

2. Select Fargate

3. Name the task definition

Task Definition Name : nodejs-demo-app-fargate

Select the IAM Role we create in Step 1 (EcsTaskExecutionRole)

4. Define the limit of memory and cpu that your task should use

Then select Add container

5. Name your container and image

container name : nodejs-demo-app-fargate
image : ikubelabs/nodejs-demo-app-fargate:1.0

Port mappings : 80

6. Scroll down and add these environment variables

ENVAPP = STAGING
PORT = 80

 

Then click Add

Step 5 : Create a service

Let’s create a  service for this new task definition

Select Actions > Create Service

2.
Select Fargate

Cluster : DemoFargate
Service name : nodejs-demo-fargate-service

Number of tasks (this is the desired count of task you want at any given time, for instance, if one container crashes, the service will provision a new one to match the desired count) : we set it to 4

Minimum healthy percent :
The service is consired healthy if at least half of your containers are up and running in an healthy state (meaning during a rolling update, it will check that there is at least 50% of the containers healthy before a replacement) : we set it to 50

 

Maximum percent :
This the maximum of tasks allowed, typically during a rolling update, the service will provision 4 new tasks, meaning we’re gonna have a total of 8 tasks running, 4 tasks with the old version, 4 tasks with the new version, then the service will wait for the new tasks to be healthy, before removing the old task version: leave it at 200

3. Select the deployment type : Rolling update and click Next step

4. Select the VPC where your service will run the tasks and click on Edit

Then Select the Security Group we created for the load balancer fargate-alb-SG, then click on Next

5. Select Application Load Balancer, choose the Load Balancer name (fargate-alb) and click on Add to load balancer

6. Choose the listener port (80:HTTP), then the target group name (fargate-target-group)

7. Click on Next , then the service will be created

8. Back to the ECS Console, select Tasks, and see how the service is provisionning the new tasks (4 as defined ), wait until it changes the status to RUNNING

Step 6 :Test the application access

Now, let’s access the application

1. Open the Load Balancer console, and copy the DNS name on your browser

2.You should see this webpage

Step 7 : Update the task definition and the service with a new version

Right now, we have the service running 4 tasks (containers) of the current image ikubelabs/nodejs-demo-app-fargate:1.0

Let’s update the service and task with a new version ikubelabs/nodejs-demo-app-fargate:secret

1.Open the ECS Console, select Task Definitions, choose nodejs-demo-app-fargate, then click on Create new revision

2. Select the container nodejs-demo-app-fargate

3. Replace the image with

ikubelabs/nodejs-demo-app-fargate:secret

Then click Update

4. Back to the Task Definition, select Actions > Update service

5. Select the cluster DemoFargate, make sure the service name is nodejs-demo-fargate-service

6. Click on Next

7. Click on Next

8. Then select Update service

8. Note that there 4 new tasks in PENDING, wait until the status is RUNNING

9. You should see the webpage, with the secret key to download the certificate of completion!

Happy Tutorial

Congratulations

Enter the key to download the certificate

** To find the key you have to deploy the application first **

Protected: Certificate of completion AWS Fargate
Scroll to Top