DEV Community

Naami Ahmed
Naami Ahmed

Posted on

Automating AWS RDS Management Using IAM, Lambda, CloudWatch, and EventBridge

WorkFlow

Introduction

In cloud computing, cost and resource optimization are critical for efficient infrastructure management. AWS provides various services that help automate database management, enhance security, and ensure high availability. This article explores how to automate Amazon RDS instance management using AWS IAM, Lambda, CloudWatch, and EventBridge. We will walk through a common scenario, benefits, and implementation steps.

Common Scenario: Managing RDS Instance Availability

Many organizations use Amazon RDS for databases, but keeping instances running 24/7 can be costly. To optimize costs, businesses prefer running their RDS instances only during business hours. This scenario requires:

  • Starting RDS at 6 AM IST (before business hours begin).
  • Stopping RDS at 6 PM IST (after business hours end).
  • Automating the process to avoid manual intervention.
  • Monitoring performance and logs to track execution and troubleshoot issues.

Workflow

1. IAM (Identity and Access Management)IAM is used to define permissions for the Lambda function to start and stop RDS instances securely.

2. Lambda (Serverless Compute Function)AWS Lambda helps execute the function that starts and stops RDS instances based on a predefined schedule.

3. CloudWatch (Monitoring and Logging)CloudWatch is used to capture logs, monitor execution, and track failures.

4. EventBridge (Automated Scheduling)Amazon EventBridge (formerly CloudWatch Events) triggers the Lambda function based on a cron schedule, ensuring automation.

Workflow: Automating RDS Start and Stop

  • Create an IAM Role for Lambda with permissions to start and stop RDS instances.
  • Write a Lambda Function to check the time and perform the necessary action (start/stop RDS).
  • Set Up EventBridge Rules to schedule the function execution (6 AM IST & 6 PM IST).
  • Monitor Execution Logs using CloudWatch to ensure proper functioning.

Step-by-Step Implementation

Step 1: Create an IAM Role for Lambda

1. Go to the AWS IAM Console.
2. Create a new role with the following policies
3. AmazonRDSFullAccess (or custom policy with StartDBInstance & StopDBInstance permissions).
4. AWSLambdaBasicExecutionRole (for CloudWatch logging)
5. Attach this role to the Lambda function.

Step 2: Write the Lambda Function
This function will:

  • Fetch the current time in IST.
  • Determine if it’s a weekday and within working hours.
  • Start or stop the RDS instance accordingly
import boto3
import datetime
from dateutil import tz
import logging
import os
# Configure logging
logging.basicConfig(level=logging.INFO)
# AWS RDS Client
region = os.getenv("AWS_REGION", "us-east-1")  # Default to 'us-east-1' if not set
rds = boto3.client('rds', region_name=region)
# Configurable Tags (Default: Empty Dict)
REQUIRED_TAGS = {
    os.getenv("TAG_KEY", "Environment"): os.getenv("TAG_VALUE", "Production")
}
# Configurable Timezone (Default: UTC)
TIMEZONE = tz.gettz(os.getenv("TIMEZONE", "UTC"))
def get_tagged_rds_instances():
    """Fetches RDS instances with required tags."""
    try:
        instances = rds.describe_db_instances()['DBInstances']
        tagged_instances = []
for db in instances:
            tags = rds.list_tags_for_resource(ResourceName=db['DBInstanceArn'])['TagList']
            tag_dict = {tag['Key']: tag['Value'] for tag in tags}
# Check if all required tags match
            if all(REQUIRED_TAGS[key] == tag_dict.get(key, "") for key in REQUIRED_TAGS):
                tagged_instances.append(db['DBInstanceIdentifier'])
return tagged_instances
    except Exception as e:
        logging.error(f"Error fetching RDS instances: {str(e)}")
        return []
def lambda_handler(event, context):
    """Lambda function to start/stop RDS based on time and day."""
    now = datetime.datetime.now(tz=TIMEZONE)
    current_hour = now.hour
    current_day = now.weekday()
tagged_rds_instances = get_tagged_rds_instances()
for db_identifier in tagged_rds_instances:
        if 0 <= current_day <= 4:  # Weekdays (Monday-Friday)
            if 6 <= current_hour < 18:
                logging.info(f"Starting RDS {db_identifier}...")
                rds.start_db_instance(DBInstanceIdentifier=db_identifier)
            else:
                logging.info(f"Stopping RDS {db_identifier}...")
                rds.stop_db_instance(DBInstanceIdentifier=db_identifier)
        else:  # Weekends (Saturday-Sunday)
            logging.info(f"Stopping RDS {db_identifier} for the weekend...")
            rds.stop_db_instance(DBInstanceIdentifier=db_identifier)
return "Lambda execution completed."
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure EventBridge Rules

  • Go to Amazon EventBridge.
  • Create two rules:
  • Start RDS at 6 AM IST: cron(30 0 * * ? *) (UTC → 6:30 AM IST)
  • Stop RDS at 6 PM IST: cron(30 12 * * ? *) (UTC → 6:30 PM IST)
  • Set the target as the Lambda function.

Step 4: Monitor Execution Using CloudWatch

  • Go to CloudWatch Logs.
  • Navigate to the log group for your Lambda function.
  • Check execution details, errors, or any unexpected behaviors.

Benefits of This Approach
✅ Cost Optimization — RDS instances run only when needed, reducing AWS costs.
✅ Automation — Eliminates manual intervention and improves efficiency.
✅ Security & Access Control — IAM roles restrict unnecessary access.
✅ Monitoring & Logging — CloudWatch ensures visibility into function execution.
✅ Scalability — Easily extend this setup for multiple RDS instances.

Conclusion
Automating Amazon RDS using IAM, Lambda, CloudWatch, and EventBridge is a powerful way to optimize cloud resources efficiently. By implementing this workflow, businesses can reduce costs, improve reliability, and maintain secure access control.

This setup can be further enhanced by integrating SNS notifications for alerts or using AWS Systems Manager for more complex automation tasks.

Thank You

Top comments (0)