Search code examples
amazon-web-servicesamazon-ecsamazon-cloudwatch

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


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.


Solution

  • 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