Implementing Stop-and-Start Policies on Amazon RDS During Non-Working Hours

Listen to this Post

Unused resources cost money! Paying AWS/GCP/Azure money to run hardware doing nothing is a waste of $$$s. There are tools available to easily scale these down when not needed. The example below shows how to suspend RDS Databases on AWS when not in use.

In many environments, resources are only needed during certain hours of the day or days of the week. With many resources you provision in your account, you can suspend them to save all or most of the costs.

One of the most useful tools on AWS for this kind of use case is EventBridge. EventBridge includes multiple components, but the feature useful here is the EventBridge Scheduler. With this, we can create one-time or recurring schedules to run almost any action in AWS.

The example below from Basanagouda Patil provides code to set up IAM policies and AWS Lambda functions that start and stop RDS databases at night and start up again in the morning. These are executed based on schedules set up in EventBridge.

The same kind of approach could be used for any kind of scheduled actions.

Code Example:

1. IAM Policy for Lambda Function:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"rds:StartDBInstance",
"rds:StopDBInstance"
],
"Resource": "arn:aws:rds:region:account-id:db:your-db-instance-id"
}
]
}

2. Lambda Function to Stop RDS Instance:

import boto3

def lambda_handler(event, context):
rds = boto3.client('rds')
response = rds.stop_db_instance(
DBInstanceIdentifier='your-db-instance-id'
)
return response

3. Lambda Function to Start RDS Instance:

import boto3

def lambda_handler(event, context):
rds = boto3.client('rds')
response = rds.start_db_instance(
DBInstanceIdentifier='your-db-instance-id'
)
return response

4. EventBridge Scheduler:

  • Create a new schedule in EventBridge.
  • Set the schedule to trigger the Lambda function to stop the RDS instance at a specific time (e.g., 10 PM).
  • Create another schedule to trigger the Lambda function to start the RDS instance at a specific time (e.g., 6 AM).

What Undercode Say:

In the realm of cloud computing, cost optimization is a critical aspect that cannot be overlooked. Unused resources, such as idle RDS instances, can significantly inflate your AWS bill. By leveraging AWS EventBridge and Lambda functions, you can automate the process of starting and stopping RDS instances during non-working hours, thereby reducing costs without compromising on availability during business hours.

The provided code snippets demonstrate how to set up IAM policies and Lambda functions to manage RDS instances efficiently. The IAM policy ensures that the Lambda function has the necessary permissions to start and stop the RDS instance. The Lambda functions themselves are straightforward, utilizing the Boto3 library to interact with the RDS service.

EventBridge Scheduler plays a pivotal role in this setup, allowing you to define precise schedules for when these actions should occur. This approach not only saves money but also ensures that your resources are available when needed.

In addition to RDS, this methodology can be extended to other AWS services such as EC2 instances, where you can use similar Lambda functions to start and stop instances based on a schedule. For example, you can use the following commands to manage EC2 instances:

  • Stop EC2 Instance:
    aws ec2 stop-instances --instance-ids your-instance-id
    

  • Start EC2 Instance:

    aws ec2 start-instances --instance-ids your-instance-id
    

Furthermore, you can use AWS CLI to create cron jobs on an EC2 instance to automate tasks such as backups, log rotations, and more. For example, to schedule a daily backup script, you can add the following line to your crontab:

0 2 * * * /path/to/backup-script.sh

This command will run the backup script every day at 2 AM.

In conclusion, automating the management of cloud resources not only reduces costs but also enhances operational efficiency. By using AWS services like EventBridge, Lambda, and IAM, you can create a robust system that ensures your resources are used optimally. This approach is not limited to AWS; similar strategies can be applied to other cloud providers like GCP and Azure, using their respective tools and services.

For more detailed instructions and examples, you can refer to the original article by Basanagouda Patil: Implementing Stop-and-Start Policies on Amazon RDS during Non-Working Hours.

By adopting these practices, you can ensure that your cloud infrastructure is both cost-effective and efficient, allowing you to focus on delivering value to your customers rather than worrying about unnecessary expenses.

References:

Hackers Feeds, Undercode AIFeatured Image