Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DamianoP/4ab355d03c8db45ffee79c2f3779a6cf to your computer and use it in GitHub Desktop.
Save DamianoP/4ab355d03c8db45ffee79c2f3779a6cf to your computer and use it in GitHub Desktop.
Example: Time-based Auto Scaling on Amazon ECS + AWS Fargate

Set parameters

$ export ECS_CLUSTER_NAME={YOUR_ECS_CLUSTER_NAME}
$ export ECS_SERVICE_NAME={YOUR_ECS_SERVICE_NAME}

RegisterScalableTarget

$ aws application-autoscaling register-scalable-target --service-namespace ecs \
    --scalable-dimension ecs:service:DesiredCount \
    --resource-id service/${ECS_CLUSTER_NAME}/${ECS_SERVICE_NAME} \
    --min-capacity 1 \
    --max-capacity 3

PutScheduledAction

$ export SCALE_OUT_ACTION_NAME=fargate-time-based-scale-out

# configure scaling out
$ aws application-autoscaling put-scheduled-action --service-namespace ecs \
    --scalable-dimension ecs:service:DesiredCount \
    --resource-id service/${ECS_CLUSTER_NAME}/${ECS_SERVICE_NAME} \
    --scheduled-action-name ${SCALE_OUT_ACTION_NAME} \
    --schedule "cron(50 23 * * ? *)" \ # every day at 8:50am JST
    --scalable-target-action MinCapacity=3,MaxCapacity=10
$ export SCALE_IN_ACTION_NAME=fargate-time-based-scale-in

# configure scaling in
$ aws application-autoscaling put-scheduled-action --service-namespace ecs \
    --scalable-dimension ecs:service:DesiredCount \
    --resource-id service/${ECS_CLUSTER_NAME}/${ECS_SERVICE_NAME} \
    --scheduled-action-name ${SCALE_IN_ACTION_NAME} \
    --schedule "cron(10 9 * * ? *)" \ # every day at 6:10pm JST
    --scalable-target-action MinCapacity=1,MaxCapacity=1

DeleteScheduledAction

$ aws application-autoscaling delete-scheduled-action --service-namespace ecs \
    --scheduled-action-name ${SCALE_OUT_ACTION_NAME} \
    --resource-id service/${ECS_CLUSTER_NAME}/${ECS_SERVICE_NAME} \
    --scalable-dimension ecs:service:DesiredCount
$ aws application-autoscaling delete-scheduled-action --service-namespace ecs \
    --scheduled-action-name ${SCALE_IN_ACTION_NAME} \
    --resource-id service/${ECS_CLUSTER_NAME}/${ECS_SERVICE_NAME} \
    --scalable-dimension ecs:service:DesiredCount

DescribeScheduledActions

$ aws application-autoscaling describe-scheduled-actions --service-namespace ecs \
    --scheduled-action-names ${SCALE_IN_ACTION_NAME} ${SCALE_OUT_ACTION_NAME}

See also

https://docs.aws.amazon.com/autoscaling/application/userguide/application-auto-scaling-scheduled-scaling.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment