How to Optimize AWS ECS Cluster Cost Out-Off-Office Hours

Kamol
By DevOps For DevOps
3 min readJul 24, 2022

This story is for the DevOps Engineers who are already familiar with Amazon ECS but wondering how to scale down non-production cluster out-off-office hours to reduce infra cost.

The solution (source code) is based on AWS CloudFormation. If you are unfamiliar with CloudFormation or Infrastructure as Code, please, read here.

Objective

At the end of this story, you should have a CloudWatch and AWS Lambda ready to scale your AWS ECS cluster down/up with time-based scheduled rules. For example, stop all ECS services at 1:00 AM and start at 8:30 AM, as shown in the figure below.

And of course, the infra resources are provisioned and managed by CloudFormation (Infra as Code), which allows you to reduce operating costs.

Steps to Setup

  1. Clone the repository into your system:
git clone git@github.com:microservices-today/ecs-scaling-scheduler.git

2. Set your AWS profile to your non-production AWS account

export AWS_PROFILE=<YOUR NON-PROD ACCOUNT>

3. Run the following script and follow its instruction

bash ./bin/configure.shSpecify the S3 bucket name for storing the templates.
<YOUR S3 BUCKET NAME>
Enter the AWS REGION to deploy the Cloudformation Stack [default: ap-southeast-1]

4. The script above should generate a link to a CloudFormation page, as shown in the figure below:

5. Follow the steps above to complete the creation of the CloudFormation stack. If it succeeds, that means you are done with the setup. That’s all you need. Congratulations!

Architecture

As you can see in the figure below, the architecture is very straightforward: CloudWatch triggers AWS Lambda and Lambda scales ECS Services.

CloudWatch event triggers Lambda, and Lambda scales ECS Services
CloudWatch event triggers Lambda, and Lambda scales ECS Services

If a developer needs/wants to work out of office hours (e.g., weekends), you will need to trigger the scaling manually. In this case, please, follow the Manual invocation for further instruction. For example, start your cluster with the following command:

bash bin/manually.sh start ECS_CLUSTER_NAME

Final Notes

It is not a sophisticated solution and doesn’t track the ECS Service states; it simply scales your services from 1 to 0 and vice versa. Please, use it for non-production clusters only.

Disclaimer: We appreciate and would like to provide credit for this idea to Tomas Tusche, from where we forked our repo and made it workable after a few bug fixes.

--

--