Automatically Stop Amazon RDS Instances Daily Using AWS Lambda and EventBridge

Search for a command to run...

No comments yet. Be the first to comment.
The Ops Fix Hub is a dynamic series designed to tackle real-world challenges in the DevOps and CloudOps domains. "Breaking Down Barriers, One Ops Fix at a Time."
This blog will walk you through a powerful end-to-end log analytics pipeline using AWS WAF, Kinesis Firehose, S3, Logstash, and OpenSearch Dashboards. We aim to analyze and visualize traffic patterns, particularly unwanted requests, filtered by WAF o...
This migration was performed on a production workload where cost reduction was prioritized over zone-level high availability.

1. Overview What I Designed I designed a hybrid infrastructure architecture: Terraform → Foundation Layer Crossplane → Dynamic Lifecycle Layer ArgoCD → GitOps Enforcement This created a continuou

Cross-cloud VM migration is not a disk copy task. It is: An access model transformation A replication lifecycle management exercise A downtime control operation A cost boundary decision We execu

When AWS introduced AWS DevOps Agent, I was less interested in feature lists and more interested in one practical question. Can it actually reduce investigation time during real production-style failu

Migrating object storage across cloud providers is not a copy task.It is a cost, network, and security boundary problem. We migrated 10+ TB of object data from Google Cloud Storage to Amazon S3 under

Managing AWS costs efficiently is crucial, especially when dealing with development, testing, or QA environments. A common and effective approach is to stop RDS instances during off-hours automatically. In this guide, you’ll learn how to set up an automated daily RDS shutdown using AWS Lambda, EventBridge Scheduler, and IAM roles.
AWS Lambda (Python): A serverless function that stops RDS instances.
Amazon EventBridge Scheduler: Triggers the Lambda function once per day.
IAM Role: Grants permissions for the Lambda to stop RDS instances.

EventBridge Scheduler triggers the Lambda function every day.
Lambda function iterates over a list of RDS instances and attempts to stop each one.
All execution logs are stored in Amazon CloudWatch Logs for auditing and debugging.
Go to the AWS Lambda Console.
Click “Create function” → Select Author from scratch.
Function Name: stop-rds-lambda-function
Runtime: Choose Python 3.13 or above.
Permissions: Attach an IAM role with the following permissions:
rds:StopDBInstance
rds:DescribeDBInstances
logs:CreateLogGroup
logs:CreateLogStream
logs:PutLogEvents
Paste the following code in the function editor:
import boto3
import logging
rds = boto3.client('rds')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
DB_INSTANCES = ['testing-db-us-east-1-demo'] # Add your DB instance identifiers here
def lambda_handler(event, context):
stopped_instances = []
failed_instances = []
for db_id in DB_INSTANCES:
try:
logger.info(f"Attempting to stop DB instance: {db_id}")
response = rds.stop_db_instance(DBInstanceIdentifier=db_id)
logger.info(f"Stop initiated for: {db_id}")
stopped_instances.append(db_id)
except Exception as e:
logger.error(f"Failed to stop {db_id}: {str(e)}")
failed_instances.append({'db_id': db_id, 'error': str(e)})
return {
'statusCode': 200,
'stopped_instances': stopped_instances,
'failed_instances': failed_instances
}
Navigate to Amazon EventBridge → Scheduler.
Click Create schedule.
Schedule type: Choose Rate-based schedule.
Set Rate: Every 1 day.
Target:
Choose a Lambda function.
Select the Lambda function you created (stop-rds-lambda-function).
Leave input blank.
Click Next → Create schedule.
Go to your Lambda function.
Click Test to run it manually.
Check the Amazon RDS Console to verify if the specified instances are now stopped.
Review CloudWatch Logs for detailed output and error handling.
By using AWS Lambda with EventBridge Scheduler, you can automate daily shutdowns of RDS instances, reducing unnecessary costs without manual intervention. This is especially helpful for non-production environments.