Search code examples
amazon-web-servicesamazon-ecs

How to shutdown EC2 instances backed by ECS to save the cost for staging/QA


We have hosted a docker container on AWS ECS with EC2 instances and would like to terminate/showdown these EC2 instances in the night & weekend for Staging/QA to save the cost.

Thanks in advance :)


Solution

  • If you run the instances in an AutoScaling Group (ASG) , you could use scheduled policy to set a desired capacity of the ASG to zero for the off-peak times. A second policy would start it for work time.

    Alternative would be setup a CloudWatch Event scheduled rule using cron with target of lambda function. The function would do same as the scaling policy. But because this is lambda function, you could also do some other things there. For example, do some pre-shutdown checks or post-shutdown cleanup.

    This will work, because if your tasks run in service, ECS will automatically relaunch your tasks when the instances are back.

    You could also manage the number of tasks using scheduling capability of Amazon ECS.