Deploy Node Service on ECS Fargate

Ashish Ranjan
4 min readJan 19, 2022

First thing first, what is ECS Fargate?

ECS Fargate is the combination of 2 different services, ECS and Fargate.

ECS stands for Elastic Container Service and its Fully AWS-managed Container Orchestration tool and you don’t need to pay for Control Node(Similar to Kubernetes Master Worker nodes).
Fargate is a kind of worker node where you don’t need to provision/maintain the underlying Ec2 machine. you just need to provide specifications of your desired container for example; 2GiB memory with 1core CPUs

Why adopt Fargate:

  • AWS Fargate is a serverless, pay-as-you-go compute engine that lets you focus on building applications without managing servers.
  • Deploy and manage your applications, not infrastructure.
  • Fargate removes the operational overhead of scaling, patching, securing, and managing servers.
  • Monitor your applications via built-in integrations with AWS services like Amazon CloudWatch Container Insights.
  • Supports multiple architectures: Windows, Linux/amd, and Linux/arm
Image source: AWS Fargate docs

Sample Hello world Node application:

  • Below application would throw 200 response code / path and give us Hellow world as output.
  • save the below file with app.js and add required node packages on package.json
  • create Dockerfile for Node Application.
  • Push Docker container on ECR.

Create ECS Infra:

AWS Infra Overview

Using Elastic Container Service (ECS) for Container Management

If you’ve only ever used Elastic Beanstalk to run containerized applications on AWS, you may be wondering how ECS differs from it. The first thing to note is that Elastic Beanstalk uses ECS under the hood and abstracts a lot away. If you want to remove even more management overhead and are only concerned about accelerating application delivery with all the nuts and bolts for performance configured for your containers, then definitely take a look at Elastic Beanstalk. Amazon ECS is a lower-level resource for dealing with Docker container management. I often like to think of it as Amazon’s response to Kubernetes. Here are some important components of ECS:

  • Cluster — This is a group of container instances that act as a single computing resource.
  • Container instances — A container instance is an Amazon EC2 instance that has been registered to be a part of a specific cluster.
  • Container agent — This is an open-source tool that takes care of the plumbing to ensure an Amazon EC2 instance can register to your cluster.
  • Task definition — This piece describes how your applications docker images should be ran. It is a json config file. Think of it as a docker-compose.yml file on steroids.
  • Scheduler — This component determines where a service or one-off task will ran on your cluster by figuring out the most optimal instance to run it on.
  • Services — A service is a long running task such as a web application. It is based off of a task definition. ECS service enables you to run and maintain a specified number of instances of a task definition simultaneously in an Amazon ECS cluster .You can specify how many instances of a service will run and Amazon ECS will ensure those services remain running.
  • Tasks — A task is the end result of running a task definition.
  • VPC with 2public-private subnets along with NAT gateway
  • ALB/NLB ⇒ private or public based on your requirements;
  • Target groups for ECS tasks.
  • ECS cluster
  • Task definition ⇒ While creating a Task definition you would need to take care of port mapping: 3000
# Cluster 
resource "aws_ecs_cluster" "main" {
name = var.nametags = {
Name = var.cluster_tag_name
}
}# Task Definition
resource "aws_ecs_task_definition" "main" {
family = var.name
task_role_arn = aws_iam_role.task_role.arn
execution_role_arn = aws_iam_role.main_ecs_tasks.arn
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.fargate_cpu
memory = var.fargate_memory
container_definitions = jsonencode([
{
name : var.name,
image : var.app_image,
cpu : var.fargate_cpu,
memory : var.fargate_memory,
networkMode : "awsvpc",
portMappings : [
{
containerPort : var.app_port
protocol : "tcp",
hostPort : var.app_port
}
]
}
])
}# Service
resource "aws_ecs_service" "main" {
name = "${var.name}-service"
cluster = var.cluster_id
task_definition = aws_ecs_task_definition.main.family
desired_count = var.app_count
launch_type = "FARGATE"network_configuration {
security_groups = ["${var.aws_security_group_ecs_tasks_id}"]
subnets = var.private_subnet_ids
}load_balancer {
target_group_arn = aws_lb_target_group.nlb_tg.arn
container_name = var.name
container_port = var.app_port
}depends_on = [
aws_ecs_task_definition.main,
]
}
ECS Fargate response

Nothing more to see here….

By,
Ashish Ranjan

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--

Ashish Ranjan

Building Highly scalable and reliable Infrastructure