Skip to content

How to dynamically change auto scaling min, max limit for ECS Fargate

0

Hello, is it possible after deploying a ECS Fargate auto scaling stack, using the CDK to later dymanically change the min and max scaling limit for the running Tasks?

I would like to know how I could do this from code, preferrable using TypeScript. More over, I would like to perform this change outside of the CDK re-deployment cycle, later on based on some event to update the scaling limits.

2 Answers
0

Is this something you want to recur on a schedule, or have some other business logic to know when they should happen? You can create a Scheduled Action either for a 1 time action, or recurring on a cron

https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_autoscaling.ScheduledAction.html

EXPERT

answered a year ago

0

Yes, it's possible to dynamically change the auto scaling min and max limits for an ECS Fargate service after deployment. You can do this by accessing the scalable target that was created for your service.

In TypeScript, you can modify the auto scaling configuration of an existing ECS Fargate service like this:

// Assuming you have a reference to your existing service
const loadBalancedFargateService = /* your existing service reference */;

// Get or create the scalable target
const scalableTarget = loadBalancedFargateService.service.autoScaleTaskCount({
minCapacity: 5,  // New minimum capacity
maxCapacity: 30, // New maximum capacity
});

// You can also update or add scaling policies
scalableTarget.scaleOnCpuUtilization('CpuScaling', {
targetUtilizationPercent: 60, // Adjust target utilization as needed
});

scalableTarget.scaleOnMemoryUtilization('MemoryScaling', {
targetUtilizationPercent: 60, // Adjust target utilization as needed
});

When you call autoScaleTaskCount() on an existing service, it will either create a new scalable target if one doesn't exist, or update the existing one with the new capacity values.

If you need to update the scaling configuration outside of a CDK deployment (e.g., in response to an event or on a schedule), you would need to use the AWS SDK for TypeScript (AWS SDK for JavaScript v3) to call the Application Auto Scaling API directly. This would involve using the RegisterScalableTarget API to update the min and max capacity values for your ECS service.
Sources
interface EnableScalingProps · AWS CDK
aws-cdk-lib.aws_ecs_patterns module · AWS CDK

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.