AWS provides a wide range of services, focusing on key areas such as storage, computing resources, running machine learning models, and so on. Each service focuses on specific needs. AWS Lambda is one of the services that come under popular services provided by AWS under the serverless category and a good choice for performing event-driven tasks effortlessly. Similarly, Elastic Container Service (ECS) with Fargate provides the best solution for running containerized, long-running applications which mainly focuses on scalability, flexibility, and minimal operational overhead.
In this blog post, we’ll explore how we can deploy Node JS applications to ECS with Fargate from Docker Hub.
Why ECS Fargate?
ECS Fargate is one of the serverless compute engines that allows us to run Docker containers without managing servers. ECS hides the complexity of having a larger infrastructure and maintaining it by yourself. It takes care of everything like scaling, patching, and security and allows the developers to focus on the application.
Following are a few pros of ECS:
No server management: No need to provision an EC2 instance or maintain
Granular resource control: For each Task, we can specify the amount of memory and CPU resources
AWS Integration: can access other services and work in collaboration such as IAM, CloudWatch, Secrets Manager & VPC
High Availability: Load balancing capability and task recovery when resource fails
ECS Fargate vs Lambda – When to choose which?
Lambda: Best suited for short-lived, event-driven tasks (eg: S3 uploads, API Gateway requests). No containers are needed and Pay per execution.
ECS Fargate: Perfect for long-running processes (eg: Web Apps & APIs), for handling requests continuously, Supports docker, multi-container apps, and complex architecture applications.
Main concepts in ECS
- Task Definition
- A blueprint for application specification: Similar to Class in Object Oriented Programming
- We can specify the following things while creating Task Definitions
- Container Images: Specify the image to be used when running the instance, an image can be from ECR (Elastic Container Registry), Docker Hub, etc.
- vCPU/memory allocation: we can specify the amount of resources we need
- Networking: Port mapping, environment variables, and secrets
- IAM role:
- Task Execution Role: Pulls images and fetches secrets
- Task Role: Grant permissions to containers running the application (eg: permission for S3)
- Task:
- An instance of Task Definition – Like Object in OO Concept
- A task can run multiple containers (eg: A Node JS application + DB image or Redis or whatever image)
- Service:
- Guarantees a specific number of tasks are always running. Handles scaling when there is a need, restarting, and load balancing
- Cluster
- A logical grouping of tasks & services. Fargate clusters require no infrastructure management.
Deploying a Node JS App on ECS Fargate
Let’s deploy a Node JS application to ECS Fargate along with the Application Load balancer and deploy it through the Docker Hub image.
The following GitHub repository has a simple Node JS Application: https://github.com/Parathantl/my-node-app
This has the Dockerfile as well for your reference.
We can either publish this application to ECR or Docker Hub. https://hub.docker.com/repository/docker/parathantl/node-app-demo/general
Prerequisites:
- AWS Account
- Docker Installed
- A sample Node JS app
Step 1: Create a docker Image & Push to Docker Hub or ECR
Step 2: Create a cLUSTER
- In the ECS console page, click “Create Cluster” create a new cluster, and select the infrastructure option to Fargate
![](https://blog.parathan.com/wp-content/uploads/2025/01/Screenshot-2025-02-04-at-17.36.56.png)
After creating:
![](https://blog.parathan.com/wp-content/uploads/2025/01/Screenshot-2025-02-04-at-17.37.20.png)
Step 3: Set Up Networking
- VPC & Subnets: Use the default VPC or create a new one with public/private subnets.
- Security Group: Allow inbound traffic on port 80 (for the ALB) and port 3000 (for the app).
- Application Load Balancer (ALB):
- Create an ALB in your VPC.
- In the AWS Management Console search bar, enter EC2, and click the EC2 result under Services:
- Navigate to Load Balancing > Load Balancers and click on Create Load Balancer
- Create an ALB in your VPC.
![](https://blog.parathan.com/wp-content/uploads/2025/01/image-20221122094741-12-7a84ba23-e4af-4e9b-a872-630189ad57c8-1024x585.png)
- In the Listeners and Routing section, click Create Target Group:
![](https://blog.parathan.com/wp-content/uploads/2025/01/blobid3-cde05d4a-0c8e-474d-bc4c-69f654cb9f1c-1024x338.png)
A new tab opens where you can create the target group.
8. In the Specify group details step, set the following values before clicking Next (leave all fields not mentioned with default values):
- Choose a target type: Select IP addresses (Fargate requires IP)
- Target group name: Enter fargate-lab-target-group
Once, the target group is created return back to Create the load balancer and complete it.
Step 4: Create a Task Definition
- In the ECS Console, navigate to Task Definitions > Create New.
- Choose Fargate as the launch type.
- Configure:
- Task Role: Allow access to AWS services (e.g., S3, DynamoDB).
- Container Definition:
- Image URL:
<aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-nodejs-app:latest
.- If it docker: docker.io/<user_name>/<image_name>
- Port mapping:
3000:3000
. - Environment variables (if needed).
- Image URL:
- CPU/Memory: Start with 0.5 vCPU and 1 GB RAM.
Step 5: Create an ECS Service
- In your ECS cluster, click Create Service.
- Configure
- Launch Type: Fargate.
- Task Definition: Select the one created earlier.
- Service Name:
nodejs-service
. - Number of Tasks: 2 (for high availability)
- Attach the ALB:
- Select the target group created earlier.
- Ensure health checks are configured (e.g.,
/health
endpoint).
Architecture Overview
The ALB (Application Load Balancer) will route traffic to ECS Fargate tasks, which pull Docker images and run them as containers. The application’s logs can be checked in CloudWatch.
![](https://blog.parathan.com/wp-content/uploads/2025/01/Untitled-Diagram.drawio.png)
Cleanup
To avoid charges, delete:
- ECS services and tasks.
- ALB, target groups, and security groups.
- ECR repository and images.
- ECS cluster.
ECS Fargate is a battle-tested service for:
- Microservices: Orchestrate multi-container apps with ease.
- High-traffic APIs: Scale seamlessly with ALB integration.
- Compliance: Isolate tasks in private subnets with IAM policies.
While Lambda is perfect for lightweight tasks, ECS Fargate provides the control and reliability needed for mission-critical applications.
AWS ECS Fargate combines the simplicity of serverless with the power of containers. By following this guide, you’ve deployed a scalable Node.js app with load balancing, logging, and zero server management. Whether you’re building APIs, web apps, or microservices, ECS Fargate is a robust choice for production workloads.
Leave a Reply