Using AWS ECS Fargate Horizontal Auto Scaling
Introduction
In previous articles, I have provided instructions on using AWS ECS Fargate to deploy a NestJS Docker image utilizing S3 Service; you can review them to understand the basic concepts before proceeding. In this article, I will guide you through configuring auto scaling to automatically increase or decrease the number of instances based on demand.
Prerequisites
In the NestJS project, to easily test the auto-scaling feature, it is necessary to create an API with a relatively long processing time to drive CPU usage up during execution. I will create a simple API as follows for testing; you can add it to your project or replace it with any equivalent API of your choice. Afterward, build the Docker image and push it to AWS ECR.
Detail
Still using AWS CDK, let's create the file lib/ecs-fargate-cloudfront-autoscale-stack.ts:
- The sections for creating the VPC, fargateService, granting permissions for ECS to pull images and use S3 Service, health checks, and creating Cloudfront are all similar to the previous article I used.
- There is a change here in the capacityProviderStrategies section where I use two strategies: FARGATE and FARGATE_SPOT. Among them, FARGATE is used to ensure there is always one node available, while FARGATE_SPOT offers lower costs but can be reclaimed by Amazon at any time, so it will be used for scaling purposes.
- distribution: In the CloudFront configuration, I added an originRequestPolicy to allow passing parameters in the URL.
- scaling includes the following information:
- minCapacity, maxCapacity: the minimum and maximum number of nodes when scaling.
- scaleOnCpuUtilization: I configured this based on CPU; if CPU utilization increases to 50% or more for 60 seconds, scaling will be performed.
Update the file bin/aws-cdk.ts as follows:
Results after deployment
Resources have been created on the AWS Console.
Testing with Postman shows successful deployment.
When you send a large number of requests, observe the Cloudwatch Metric as CPU usage gradually increases and the system automatically scales accordingly.
Initially, there is only 1 task.
After automatic scaling, it will increase to 2 tasks.
Happy coding!
See more articles here.
Comments
Post a Comment