# Automatically Stop Amazon RDS Instances Daily Using AWS Lambda and EventBridge

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**.

## Solution Overview

### 🧱 Components Used:

* **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.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1747500635846/0a00cb5b-87ab-4f52-818c-9b61a3ccf6e4.png align="center")
    

### ⚙️ How It Works:

1. **EventBridge Scheduler** triggers the Lambda function every day.
    
2. **Lambda function** iterates over a list of RDS instances and attempts to stop each one.
    
3. All execution logs are stored in **Amazon CloudWatch Logs** for auditing and debugging.
    

## Step-by-Step Setup Guide

### 1️⃣ . Create the Lambda Function

* **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`
        
    * ![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXePZWBdTv__21FXOyAHc3-Jg0TyfJ13OxAjtkakG-tah3w3B_TFq4P7rr4CUTvo4erHsoxm9FfbWHfCe15BbmPG-o9ccD6oHiMBXcNV2ixCT8cT4OQJsxrw9xMdMxIheQvdcTi74g?key=zhdBqEuEsI0vnjepXvtwiw align="left")
        

### Add Lambda Code

Paste the following code in the function editor:

```python
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
    }
```

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXdhPfrI7pdS5HH59E0zNXafGpu3ulcohqlslMXhDz5j68nq_kC_IUevDKpj_tGyn_-iQbiT_zoTzCDsoQAfw48yq-8-EP-rbfk3rjyXuvFGgrb22rGpL4oUEmhx_eclWGiDU3qUow?key=zhdBqEuEsI0vnjepXvtwiw align="left")

### 3️⃣ . Create EventBridge Schedule

* **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**.
    

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXc1cwgRTSlVBPT-NVhzMJHEozw4HTLefZDexTa4vdUfk2XviMfOIDT79oiSCNefS70cnSBNOvHjYVMDKe0_O4nMB5cYWqpyXbmnCMTGAl-VVMjFpe7r2EDm48HHrSgNqD73B2H3Ag?key=zhdBqEuEsI0vnjepXvtwiw align="left")

## ✅ Testing the Lambda

* 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.
    

## 📌 Summary

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.
