Guide to using AWS ECS Fargate
Introduction
AWS Elastic Container Service (ECS) Fargate is a "serverless" technology for containers, helping you run Docker applications without having to manage physical servers or EC2 virtual machine clusters.
Normally, when running containers, you must choose the instance type, manage the operating system, and scale the server cluster. With Fargate, you simply package the application into a container, define CPU and memory requirements, and Fargate will automatically initialize and manage the underlying infrastructure.
Key Advantages
- No infrastructure management: Eliminates operational burdens such as OS patching, server maintenance, or EC2 cluster management.
- High security: Each task runs in a separate execution environment, completely isolated from other tasks, helping to enhance security.
- Flexible scalability: Fargate automatically adjusts resources based on the actual needs of the application without worrying about cluster capacity shortages.
- Deep integration: Easily connect with other AWS services such as IAM, CloudWatch, and VPC.
Pricing
Fargate applies a Pay-as-you-go model (pay for what you use). Costs are calculated based on:
- The amount of CPU and Memory (RAM) that your container configures.
- Running time: Calculated from the start of downloading the container image until the task terminates, accurate to the second.
- Additional costs: May include data storage fees (EBS), data transfer fees (Data Transfer), or fees for using other advanced features.
Prerequisites
In this article, I will still use the Docker Image built from the NestJS project; you can follow my previous articles to continue using it or use your own docker image, then push this docker image to AWS ECR.
Detail
In the AWS CDK project, create a .env file with the following information; please update it accordingly to suit your needs.
Create file lib/ecs-fargate-cloudfront-stack.ts
Explaination:
- vpc: still creates a VPC (Virtual Private Cloud) without a NAT Gateway (as this resource is quite expensive)
- fargateService: created along with a Load Balancer; you pass in the cpu, memoryLimitMiB, and the vpc created above
- capacityProvider: using FARGATE_SPOT can save up to 70% in costs for applications that can tolerate interruptions; since this is just an example, I use it to save costs, but in actual cases, you should consider this option
- taskImageOptions: pass in docker image information such as image url, port, and environment variables
- fargateService.taskDefinition: To grant ECR login permissions to the Execution Role
- fargateService.targetGroup.configureHealthCheck: in case you have an API for health checks, add it; otherwise, it can be skipped
- fargateService.taskDefinition.addToTaskRolePolicy: this part is to grant permissions to the S3 Bucket
- cloudfront.Distribution: creates Cloudfront pointing to the Load Balancer address, along with customHeaders; note that the NestJS project must also have middleware to handle customHeaders (I have provided instructions in previous articles)
After deploying, the results are as follows:
The corresponding resources have been created on the AWS Console
Use Postman to use the API
Happy coding!
Comments
Post a Comment