Home > Blockchain >  Scale AWS ECS Fargate up and down on specific time each day
Scale AWS ECS Fargate up and down on specific time each day

Time:10-01

I have a ECS Fargate service setup, which need to scale-up to 2 extra tasks on working hours i.e 9am to 5pm, otherwise I only need 1 task running.

I have tried Scheduled tasks but then I wont get to use my original ECS service with I use with "Application Load balancer".

I also tried ECS Service Auto scaling-'Step scaling' using a custom CloudWatch alarm. But I don't know any easy way to create a alarm to trigger on specific time, as ECS Step scaling does not accept EventBridge rule.

CodePudding user response:

You can use scheduled scaling:

aws application-autoscaling put-scheduled-action --service-namespace ecs \
    --scalable-dimension ecs:service:DesiredCount \
    --resource-id service/${CLUSTER_NAME}/${SERVICE_NAME} \
    --scheduled-action-name ${DESIRED_ACTION_NAME} \
    --schedule "cron(cron-expression-to-add-here)" \ 
    --scalable-target-action MinCapacity=${MIN_CAPACITY_NEEDED_AT_DESIRED_TIME},MaxCapacity=${MAX_CAPACITY_NEEDED_AT_DESIRED_TIME}

You can have more info about put-scheduled-action here

CodePudding user response:

Scheduled tasks will not work, as those tasks are not part of your service. ECS Scheduled tasks are for scheduled background tasks that run and exit on their own, not for a service that will be running indefinitely.

ECS Service Auto Scaling is the correct feature to use for this. Unfortunately the web interface lags behind the actual capabilities of the service, and doesn't allow you to configure scheduled scaling events at this time.

You currently have to use something like the AWS CLI tool to configure this via the API. You will need to create an AWS Application Autoscaling Target which references your ECS service's DesiredCount setting. Then you will need to create multiple AWS Application Autoscaling Scheduled Actions to change the min/max capacity values on a cron schedule.

I've done this before using Terraform so I know it is possible. And I highly recommend using Terraform to configure this, as well as the rest of your infrastructure. When ECS scheduled auto scaling is setup the configuration doesn't appear anywhere in the AWS web interface as far as I can find. The feature seems to be totally missing from both the ECS and Application Autoscaling web interfaces at this time.

  • Related