• DevOps
    Case Study

    How we built a resilient multi-account, multi-cloud solution for a Health Tech service provider!

    READ CASESTUDY
    icon

    24/7 DevOps as a Service

    Round-the-clock DevOps for uninterrupted efficiency.

    icon

    Infrastructure as a Code

    Crafting infrastructure with ingenious code.

    icon

    CI/CD Pipeline

    Automated CI/CD pipeline for seamless deployments.

    icon

    DevSecOps

    Integrated security in continuous DevOps practices.

    icon

    Hire DevOps Engineers

    Level up your team with DevOps visionaries.

    icon

    Consulting Services

    Navigate success with expert DevOps consulting.

  • TechOps
    Case Study

    How we built a scalable Odoo solution for a Travel Tech service provider!

    READ CASESTUDY

    WEB HOSTING SUPPORT

    icon

    HelpDesk Support

    Highly skilled 24/7 HelpDesk Support

    icon

    Product Support

    Boost your product support with our expertise.

    MANAGED SERVICES

    icon

    Server Management

    Don’t let server issues slow you down. Let us manage them for you.

    icon

    Server Monitoring

    Safeguard your server health with our comprehensive monitoring solutions.

    STAFF AUGMENTATION

    icon

    Hire an Admin

    Transform your business operations with our expert administrative support.

    icon

    Hire a Team

    Augment your workforce with highly skilled professionals from our diverse talent pool.

  • CloudOps
    Case Study

    How we helped a Private Deemed University in India, save US $3500/m on hosting charges!

    READ CASESTUDY
    icon

    AWS Well Architected Review

    Round-the-clock for uninterrupted efficiency

    icon

    Optimize

    Efficient CloudOps mastery for seamless cloud management

    icon

    Manage

    Automated CI/CD pipeline for seamless deployments

    icon

    Migrate

    Upgrade the journey, Migrate & Modernize seamlessly

    icon

    Modernize

    Simplify compliance complexities with our dedicated services

    icon

    FinOps as a Service

    FinOps as a Service

  • SecOps
    Case Study

    How we built a scalable Odoo solution for TravelTech service provider!

    READ CASESTUDY
    icon

    VAPT

    Vulnerability Assessment and Penetration Testing

    icon

    Source Code Review

    Ensuring source code security ans safe practices to reduce risks

    icon

    Security Consultation

    On demand services for improving server security

    icon

    System Hardening

    Reduced vulnerability and proactive protection

    icon

    Managed SoC

    Monitors and maintains system security. Quick response on incidents.

    icon

    Compliance as a Service

    Regulatory compliance, reduced risk

  • Insights
    Case Study

    How we helped a Private Deemed University in India, save US $3,500/m on hosting charges!

    READ CASESTUDY
    icon

    Blog

    Explore our latest articles and insights

    icon

    Case Studies

    Read about our client success stories

    icon

    Flipbook

    Explore our latest Flipbook

    icon

    Events

    Join us at upcoming events and conferences

    icon

    Webinars

    Watch our educational webinar series

  • Our Story
  • Contact Us

Interested to collaborate?

Get in touch with us!

Ready to elevate your business with certified cloud expertise? Contact us today to learn how our team can help you leverage cloud technology to drive growth, streamline operations, and enhance security.

  • AWSAWS
  • Azure CloudAzure Cloud
  • Google CloudGoogle Cloud
  • Akamai CloudAkamai Cloud
  • OVHOVH
  • Digital OceanDigital Ocean
  • HetznerHetzner
  • Kubernetes Consultancy Services
  • K8s & Cloud native Solutions
  • 24/7 Infrastructure Monitoring
  • DevOps as a Service
  • Cloud CI/CD Solutions
  • White Labeled MSP Support
  • Our story
  • Life@SupportSages
  • Insights
  • Careers
  • Events
  • Contact Us

Connect with us!


LinkedInFacebookXInstagramYouTube

aws partneraws advanced partner
SupportSages

Copyright © 2008 – 2026 SupportSages Pvt Ltd. All Rights Reserved.
Privacy PolicyLegal TermsData ProtectionCookie Policy
Automating RDS Reader Replica Counts Using AWS Lambda and Boto3

Automating RDS Reader Replica Counts Using AWS Lambda and Boto3

Arya P B

  • 3 min read
Automating RDS Reader Replica Counts Using AWS Lambda and Boto3

Generating audio, please wait...

Introduction

Managing RDS read replicas dynamically can improve database performance and reduce costs. This blog will walk you through an AWS Lambda function that adjusts the read replica count for RDS clusters based on custom tags and time-based triggers.

Overview

The Lambda function performs the following actions:

  1. Lists all RDS clusters in the account.
  2. Retrieves tags for each cluster.
  3. Checks for specific tags indicating when to adjust the reader replica count.
  4. Dynamically updates the read replica count using the Application Auto Scaling API.

Prerequisites

  1. IAM Role: Ensure the Lambda function has sufficient permissions to describe RDS clusters, list tags, and update the scaling targets.
  2. Tagged RDS Clusters: Add specific tags to your RDS clusters to indicate when to update reader replicas.
  3. Boto3: AWS SDK for Python to interact with AWS services.

Lambda Function Code

import boto3
import os
from datetime import datetime, timezone
# Initialize AWS clients rds_client and appautoscaling_client
# Constants
SERVICE_NAMESPACE = 'rds'
DIMENSION_NAME = 'replica'
RESOURCE_ID_PREFIX = 'cluster:'
def list_all_rds_clusters():
    """
    Fetch all RDS clusters in the account.
    """
    clusters = []
    response = rds_client.describe_db_clusters()
    for cluster in response['DBClusters']:
        clusters.append({
            'DBClusterIdentifier': cluster['DBClusterIdentifier'],
            'DBClusterArn': cluster['DBClusterArn']
        })
    return clusters
def get_cluster_tags(cluster_arn):
    """
    Retrieve tags for a specific RDS cluster.
    """
    response = rds_client.list_tags_for_resource(ResourceName=cluster_arn)
    tags = {tag['Key']: tag['Value'] for tag in response['TagList']}
    return tags
def update_min_readers(cluster_id, min_count, max_count):
    print(f"Updating min reader count for {cluster_id} to {min_count} and max to {max_count}")
    resource_id = f"{RESOURCE_ID_PREFIX}{cluster_id}"
    response = appautoscaling_client.register_scalable_target(
        ServiceNamespace=SERVICE_NAMESPACE,
        ResourceId=resource_id,
        ScalableDimension='rds:cluster:ReadReplicaCount',
        MinCapacity=int(min_count),
        MaxCapacity=int(max_count)
    )
    print(f"Updated successfully: {response}")
def lambda_handler(event, context):
    """
    Lambda handler function.
    """
    try:
        current_time = datetime.now(timezone.utc).strftime("%H:%M")
        print(f"Current Time (UTC): {current_time}")
        clusters = list_all_rds_clusters()
        print(f"Found {len(clusters)} RDS clusters.")
        for cluster in clusters:
            cluster_id = cluster['DBClusterIdentifier']
            cluster_arn = cluster['DBClusterArn']
            print(f"Checking cluster: {cluster_id}")
            tags = get_cluster_tags(cluster_arn)
            print(f"Tags for {cluster_id}: {tags}")
            if tags.get('update_replica_count', 'no').lower() == 'yes':
                print(f"'update_replica_count' is set to 'yes' for {cluster_id}.")
                morning_time = tags.get('morning_time')
                night_time = tags.get('night_time')
                min_readers_morning = tags.get('min_readers_morning')
                min_readers_night = tags.get('min_readers_night')
                max_count = tags.get('max_count')
                if not (morning_time and night_time and min_readers_morning and min_readers_night):
                    print(f"Missing required tags for cluster {cluster_id}. Skipping...")
                    continue
                if current_time == morning_time:
                    update_min_readers(cluster_id, min_readers_morning, max_count)
                elif current_time == night_time:
                    update_min_readers(cluster_id, min_readers_night, max_count)
                else:
                    print(f"No action needed for {cluster_id} at this time.")
            else:
                print(f"'update_replica_count' is not set to 'yes' for {cluster_id}. Skipping...")
        print("RDS reader replica count check completed successfully.")
        return {
            'statusCode': 200,
            'body': 'RDS reader replica count updated successfully where applicable!'
        }
    except Exception as e:
        print(f"Error occurred: {str(e)}")
        return {
            'statusCode': 500,
            'body': f"Error: {str(e)}"
        }

Key Concepts

  1. Tags for Time-Based Control:
    update_replica_count: Set to yes to enable updates for the RDS cluster.
    morning_time and night_time: Define when to update reader replicas (e.g., 06:00 and 18:00).
    min_readers_morning and min_readers_night: Number of reader replicas for morning and night periods.
    max_count: Maximum reader replica count.
  2. Auto Scaling:
    Uses the register_scalable_target method to set min and max reader counts.
  3. Error Handling:
    The function gracefully handles errors and logs useful debug information.

How to Deploy

  1. Create Lambda Function
    Open AWS Lambda and create a new function.
    Upload the code or use the in-line editor.
  2. Set Environment Variables
  3. Add necessary permissions for rds:DescribeDBClusters, rds:ListTagsForResource, and application-autoscaling:RegisterScalableTarget.
  4. Test the Function
    Trigger the Lambda function manually to verify it lists clusters, reads tags, and updates replica counts as expected.

Conclusion

With this automated approach, you can ensure optimal resource allocation for your RDS reader replicas. By tagging RDS clusters appropriately, you control when and how the scaling happens. This setup reduces manual effort, optimizes performance, and controls costs.

  • DevOps
Promotional banner
Promotional banner

Analyzing AWS IAM Users: Access Key and Password Age

Analyzing AWS IAM Users: Access Key and Password Age
  • DevOps
logo

Analyzing AWS IAM Users: Access Key and Password Age

Analyzing AWS IAM Users: Access Key and Password Age
  • AWS
  • DevOps
logo

Auto-Restart EC2 Instances on Status Check Failure: Quick Setup Guide

Auto-Restart EC2 Instances on Status Check Failure: Quick Setup Guide
  • DevOps
logo

Auto-Restart EC2 Instances on Status Check Failure: Quick Setup Guide

Auto-Restart EC2 Instances on Status Check Failure: Quick Setup Guide
  • AWS
  • DevOps
logo

Posts by Arya P B

Athena