AWS Fargate ECS Task Example: A Step-by-Step Guide
AWS Fargate, a serverless compute engine for Amazon ECS (Elastic Container Service), allows you to run containers without managing the underlying infrastructure. It simplifies container orchestration by handling provisioning, scaling, and maintenance, making it an excellent choice for modern containerized applications. This article provides a practical example of running a containerized application using AWS Fargate on ECS.
What Is AWS Fargate?
AWS Fargate eliminates the need to manage EC2 instances for containerized workloads. Key benefits include:
- Scalability: Automatically adjusts resources based on workload demands.
- Cost-Effectiveness: Pay only for the compute resources you use.
- Simplified Management: No need to provision or manage EC2 instances.
AWS Fargate ECS Task Example
Objective
We’ll deploy a containerized Node.js application to AWS Fargate using Amazon ECS.
Step 1: Prerequisites
- AWS Account: Ensure you have access to ECS and Fargate.
- Docker Installed: For building and pushing container images.
AWS CLI Installed: Configure it with your credentials:
aws configure
Step 2: Prepare the Application
Create a Node.js Application
Add the required dependency:
npm install --save express
Add the following code to index.js
:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from AWS Fargate!\n');
});
server.listen(3000, () => {
console.log('Server is running on port 3000');
});
Create a new directory and initialize a Node.js project:
mkdir fargate-ecs-example
cd fargate-ecs-example
npm init -y
Step 3: Build and Push the Docker Image
Create a Dockerfile
Create a Dockerfile
in the project directory:
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]
Build and Push the Image
Push the image to ECR:
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <your_account_id>.dkr.ecr.<region>.amazonaws.com
docker push <your_account_id>.dkr.ecr.<region>.amazonaws.com/fargate-ecs-example:latest
Tag the image for your AWS Elastic Container Registry (ECR):
docker tag fargate-ecs-example:latest <your_account_id>.dkr.ecr.<region>.amazonaws.com/fargate-ecs-example:latest
Build the Docker image:
docker build -t fargate-ecs-example .
Step 4: Create an ECS Cluster
- Navigate to the ECS Console.
- Select Clusters → Create Cluster.
- Choose Networking only (AWS Fargate).
- Name the cluster (e.g.,
fargate-example-cluster
) and create it.
Step 5: Define a Task Definition
- In the ECS Console, go to Task Definitions → Create new Task Definition.
- Choose Fargate.
- Configure the task:
- Task Definition Name:
fargate-example-task
. - Task Role: None (for this example).
- Network Mode:
awsvpc
. - Container:
- Container Name:
fargate-example-container
. - Image:
<your_account_id>.dkr.ecr.<region>.amazonaws.com/fargate-ecs-example:latest
. - Memory Limits: 512 MiB.
- Port Mappings: 3000 (container port).
- Container Name:
- Task Definition Name:
- Create the task definition.
Step 6: Run the ECS Task
- Navigate to the Clusters tab and select your cluster.
- Click Run New Task.
- Configure the task:
- Launch Type:
Fargate
. - Task Definition:
fargate-example-task
. - Cluster:
fargate-example-cluster
. - Network Configuration: Use an existing VPC and public subnet. Enable auto-assign public IP.
- Launch Type:
- Run the task.
Step 7: Access the Application
- Go to the Tasks tab in your cluster and select the running task.
- Find the Public IP of the task under the Network section.
- Access the application in your browser using the IP and port 3000 (e.g.,
http://<public-ip>:3000
).
Best Practices for AWS Fargate
- Monitor Tasks: Use CloudWatch for task logs and performance monitoring.
- Optimize Costs: Right-size resource allocations for CPU and memory.
- Enable Security: Use IAM roles to restrict access and ensure secure networking with private subnets and security groups.
Conclusion
AWS Fargate simplifies running containerized applications by eliminating the need to manage servers. By following this example, you can deploy a Node.js application with minimal setup and scale it effortlessly. AWS Fargate is an excellent choice for building serverless, containerized solutions that are secure, cost-effective, and scalable.