Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Automating RDS Instance Resizing with AWS Lambda

In the ever-evolving landscape of cloud computing, the ability to efficiently manage and adapt database resources is crucial. This blog post details a step-by-step guide on automating the resizing of Amazon RDS instances using Aws Lambda. By leveraging predefined scaling tags and schedules, you can ensure that your RDS instances dynamically adjust to varying workloads.

Prerequisites:

Before delving into the implementation, make sure you have the following prerequisites in place:

  • AWS Account with necessary Lambda and RDS permissions.
  • RDS Instances tagged with specific keys: scale-up, scale-down, scale-up-time, and scale-down-time.

Implementation Steps:

Lambda Function Setup

  • Configure Lambda function with Python 3.10 runtime.
  • Ensure the Lambda role has RDS and DynamoDB permissions.
  • Set up CloudWatch Events for scheduled execution triggers.

Lambda Function Code:

import boto3
import datetime

rds_client = boto3.client('rds')
current_time = datetime.datetime.utcnow()
current_time_minus_30min = (current_time - datetime.timedelta(minutes=30)).strftime('%H:%M')
current_time = current_time.strftime('%H:%M')
print("time:",current_time)
dynamodb = boto3.resource('dynamodb')
table_name ='RDSlogs'
table = dynamodb.Table(table_name)

def modify_rds(new_instance_class,db_instance_identifier):
try:
rds_client.modify_db_instance(
DBInstanceIdentifier=db_instance_identifier,
DBInstanceClass=new_instance_class,
ApplyImmediately=True
)
return {
'statusCode': 200,
'body': 'Operation completed successfully'
}

except Exception as e:
print("reason:", e)
return {
'statusCode': 500,
'body': 'Error occurred'
}
def find_nearest_times(target_time_str, time_list, current_time_str):
"""Find the nearest times in the list to the target time within a 30-minute window."""
current_time = datetime.datetime.strptime(current_time_str, '%H:%M')
target_time = datetime.datetime.strptime(target_time_str, '%H:%M')
window_start_time = current_time - datetime.timedelta(minutes=30)
window_end_time = current_time + datetime.timedelta(minutes=30)

nearest_up_time = None
nearest_down_time = None
min_difference_up = float('inf')
min_difference_down = float('inf')

for time_str in time_list:
time_obj = datetime.datetime.strptime(time_str, '%H.%M')

# Check if the time is within the 30-minute window
if window_start_time difference = abs((time_obj - target_time).total_seconds())

if time_obj min_difference_up = difference
nearest_up_time = time_obj

if time_obj > target_time and difference min_difference_down = difference
nearest_down_time = time_obj

return nearest_up_time.strftime('%H.%M') if nearest_up_time else None, \
nearest_down_time.strftime('%H.%M') if nearest_down_time else None


def update_table(db_instance_identifier, current_time, new_instance_class):
dynamodb_client = boto3.client('dynamodb')
try:
dynamodb_client.put_item(
TableName=table_name,
Item={
'DBidentifier': {'S': db_instance_identifier},
'InstanceClass': {'S': new_instance_class},
'Timestamp': {'S': current_time}
}
)
print(f"Successfully wrote to DynamoDB for DB Identifier: {db_instance_identifier}")
except Exception as e:
print("Failed to write to DynamoDB:", str(e))

def lambda_handler(event, context):
print("event:",event)
response = rds_client.describe_db_instances()
print(response)
for i in response['DBInstances']:
db_instance_name = i['DBInstanceIdentifier']
print(db_instance_name)
each_response = rds_client.describe_db_instances(DBInstanceIdentifier=db_instance_name)
print(response)
db_instance_class = each_response['DBInstances'][0]['DBInstanceClass']
up_value = None
down_value = None
scale_down_time_value = None

for tag in response['DBInstances'][0]['TagList']:
if tag['Key'] == 'scale-up':
up_value = tag['Value']
elif tag['Key'] == 'scale-down':
down_value = tag['Value']
elif tag['Key'] == 'scale-up-time':
scale_up_time_value = tag['Value']
elif tag['Key'] == 'scale-down-time':
scale_down_time_value = tag['Value']

# Print the obtained scale values
print("Scale Up Value:", up_value)
print("Scale Down Value:", down_value)
up_time_part = scale_up_time_value.split('/')
down_time_part= scale_down_time_value.split('/')

nearest_up_time, nearest_down_time = find_nearest_times(current_time, up_time_part + down_time_part, current_time)

print("Nearest Scale Up Time within 30 minutes:", nearest_up_time)
print("Nearest Scale Down Time within 30 minutes:", nearest_down_time)

for up_time, down_time in zip(up_time_part, down_time_part):
print(up_time)
if nearest_up_time and nearest_up_time == up_time and db_instance_class == down_value:
new_instance_class = up_value
modify_rds(new_instance_class,db_instance_name)
update_table(db_instance_name, current_time, new_instance_class)


elif nearest_up_time and nearest_up_time == down_time and db_instance_class == up_value:
new_instance_class = down_value
modify_rds(new_instance_class,db_instance_name)
update_table(db_instance_name, current_time, new_instance_class)
  • Utilize the provided Python code with Boto3 for RDS and DynamoDB interactions.
  • The code includes functions for modifying RDS instance class, finding nearest times within a 30-minute window, and updating DynamoDB logs.

Testing:

  • Manually trigger the Lambda function and observe logs.
  • Verify the correct identification of RDS instance class and successful execution of resizing operations.

Scheduling:

  • Set up a recurring CloudWatch Events rule for periodic Lambda execution.
  • Use a cron expression for desired intervals (e.g., cron(0 0 * * ? *) for daily execution).
  1. RDS Tags:
  • Tag RDS instances with specific keys and values:
  • scale-up: Target instance class for scale-up.
  • scale-down: Target instance class for scale-down.
  • scale-up-time: Time window for scale-up operations (e.g., 17:51/01:10).
  • scale-down-time: Time window for scale-down operations (e.g., 17:00/02:30).

Conclusion:

Automating the resizing of your RDS instances based on workload demands streamlines database management in your AWS environment. The combination of AWS Lambda, RDS, and DynamoDB creates a powerful solution that adapts to changing requirements without manual intervention.

Take control of your RDS costs and ensure optimal performance by automating database scaling! This blog provided a step-by-step guide using AWS Lambda, RDS, and DynamoDB. Implement the Python code we shared to automatically resize your RDS instances based on predefined tags and schedules. Start saving money and improve efficiency today!


Automating RDS Instance Resizing with AWS Lambda was originally published in SupportSages on Medium, where people are continuing the conversation by highlighting and responding to this story.



This post first appeared on Listing AWS Backup Recovery Points With AWS Lambda, please read the originial post: here

Share the post

Automating RDS Instance Resizing with AWS Lambda

×

Subscribe to Listing Aws Backup Recovery Points With Aws Lambda

Get updates delivered right to your inbox!

Thank you for your subscription

×