In today's cloud-centric world, managing costs while maintaining scalability and availability is a critical aspect of infrastructure management. AWS offers a plethora of services that enable such management, and among these, Amazon Elastic Container Service (ECS) and Amazon Relational Database Service (RDS) stand out for their versatility and widespread use. However, when these services are left running 24/7, they can accrue significant costs, particularly for development, testing, or staging environments that are not always in use. This article explores a strategy to mitigate these costs by stopping ECS clusters and RDS instances temporarily during off-hours, specifically overnight, using AWS EventBridge and AWS Lambda.
AWS's pricing model for ECS and RDS services is usage-based, meaning you pay for the computing resources and database instances as long as they are running. Many projects, especially those in development or testing phases, do not require these services to run continuously, offering an opportunity for cost savings. The same applies for QA/staging environments. By setting the desired number of tasks in ECS clusters to 0 and stopping RDS instances temporarily when they are not in use, such as overnight, you can significantly reduce your AWS bill.
The automation of starting and stopping AWS services can be elegantly achieved through the combination of AWS EventBridge and AWS Lambda. AWS EventBridge allows you to set rules based on a schedule (e.g., every night at 10 PM) to trigger target actions, such as invoking a Lambda function. AWS Lambda can then run code to adjust the desired task counts in ECS services to 0 or to stop RDS instances temporarily, all without manual intervention.
To begin, you need to create an EventBridge rule that specifies when the Lambda function should be triggered. For example, to stop services at 10 PM every weekday, you would configure a cron expression in EventBridge like so:
This expression directs EventBridge to trigger at 10 PM UTC from Monday to Friday.
Next, you will create a Lambda function that contains the logic to set the desired number of tasks in your ECS services to 0 and to stop your RDS instances. We will use JavaScript (Node.js) for our examples.
Code to Set ECS Service Desired Task Number to 0
The code above sets a specific desired amount of instances for a particular service in the context of a cluster. These three parameters need to be passed to the Lambda function. If we want the cluster to be stopped (to remove any container) you need to set instances = 0.
Code to Stop RDS Instances Temporarily
The code above starts/stops an RDS instance identified by the parameter `clusterIdentifier`. The parameter `action` determines whether the instance needs to be started or stopped.
After setting up your Lambda functions, you integrate them with EventBridge by setting these functions as targets for the EventBridge rules you created. This integration ensures that your ECS services and RDS instances are automatically scaled down or stopped according to the schedule you defined.
By harnessing the power of AWS EventBridge and AWS Lambda, you can automate the scaling down of ECS services and the temporary stopping of RDS instances, achieving significant cost savings. This approach not only optimizes your AWS costs but also encourages a more sustainable use of cloud resources, aligning with best practices in cloud infrastructure management. As you implement this solution, remember to adjust the region, cluster, service, and database identifiers in the provided code snippets to match your specific AWS environment.