In this blog, we will see the configuration of the AWS RDS schedule start and stop using lambda functions.
Steps to configure it.
- IAM role for lambda with RDS start and stop permissions.
- Two lambda functions in python for start and stop.
- RDS tagging.
Create IAM policy:-
Go to IAM and create an IAM policy with RDS start and stop actions. Policy as given below:-
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"rds:StopDBInstance",
"rds:StartDBInstance",
"rds:DescribeDBInstances"
],
"Resource": "*"
}
]
}

Create lambda function for stop:
Go to lambda page and click on Create Function.

Select Author from scratch. Provide Function name and select Python 3.7 or Python 3.8 as Runtime. And click on Create Function.

Copy the Python code as given below and replace it with the existing code. Replace TAG_KEY and TAG_VALUE highlighted in different colors as you wish. You can see in the screenshot I have used tag_key as StartStop and tag_value as yes.
import boto3
def lambda_handler(event, context):
tag_key = 'TAG_KEY'
tag_value = 'TAG_VALUE'
rds = boto3.client('rds')
instances = rds.describe_db_instances()
instances_to_stop = [i for i in instances['DBInstances'] if any(t['Key'] == tag_key and t['Value'] == tag_value for t in i.get('TagList', [])) and i['DBInstanceStatus'] == 'available']
for instance in instances_to_stop:
rds.stop_db_instance(DBInstanceIdentifier=instance['DBInstanceIdentifier'])
return {
'statusCode': 200,
'body': f'Stopped {len(instances_to_stop)} RDS instances'
}
Then click on Deploy.

Go to Configuration tab and then click on Permissions. Under execution role click on Role name.

IAM page will open in a new tab. Attach that IAM policy that we created before.
Create lambda function to start RDS:
In the same way, you can create another lambda function to start RDS.
Replace existing python code with the below one:
import boto3
def lambda_handler(event, context):
tag_key = 'TAG_KEY'
tag_value = 'TAG_VALUE'
rds = boto3.client('rds')
instances = rds.describe_db_instances()
instances_to_start = [i for i in instances['DBInstances'] if any(t['Key'] == tag_key and t['Value'] == tag_value for t in i.get('TagList', [])) and i['DBInstanceStatus'] == 'stopped']
for instance in instances_to_start:
rds.start_db_instance(DBInstanceIdentifier=instance['DBInstanceIdentifier'])
return {
'statusCode': 200,
'body': f'Started {len(instances_to_start)} RDS instances'
}
Follow the same steps as we have done in the stop function to attach the newly created IAM policy.
RDS tagging:
Go to RDS page and click on RDS which you want to configure scheduled start and stop.
Go to Tags tab and click on Add.

Provide tag key and tag value that you have given the lambda function. Click on Add.

Testing:
Go to the lambda page again and click on the RDS stop function. Then click on Test.

Give Event Name as per your wish and click on Save.
Now again click on Test and wait for a few moments to complete lambda execution. Once lambda is executed successfully then go to the RDS page and refresh the page. After a few moments, you can see RDS is stopping.

In the same way, you can test RDS start lambda function as well.
Now lambda function is tested and ready. You can configure it with Amazon EventBridge to schedule start and stop as per your requirement.
I think this is one of the most vital info for me. And i am glad reading your article.
But wanna remark on some general things, The web site style is wonderful, the articles is
really excellent : D. Good job, cheers