The Talent500 Blog
Simplify Amazon RDS Stop Scheduling 1

Simplify Amazon RDS Stop Scheduling

Introduction

Imagine you have a database, and you need to stop it temporarily (When you stop RDS, it is stopped temporarily for 7 days only and then it will restart ), but not shut it down completely because you might need it later for your applications. To tackle this, we’re turning to a clever solution involving something called the “EventBridge rule.” This rule is like a watchful guardian. Whenever your database starts, this rule springs into action, setting off a special task called a “lambda function.” This function has a smart piece of code that knows exactly how to pause the database when needed.

The databases are billed for the computing and storage costs. To reduce the overall cost, Amazon RDS allows instances to be stopped temporarily. While the instance is stopped, you’re charged for storage and backups, but not for the DB instance hours. Please note that a stopped instance will automatically be started after 7 days.

While your DB instance is stopped, you are charged for provisioned storage (including Provisioned IOPS). You’re also charged for backup storage, including manual snapshots and automated backups within your specified retention window. However, you’re not charged for DB instance hours.

When you stop a DB instance it retains its ID, Domain Name Server (DNS) endpoint, parameter group, security group, and option group. When you start a DB instance, it has the same configuration as when you stopped it. In addition, if you stop a DB instance, Amazon RDS keeps the Amazon S3 transaction logs. This means that you can do a point-in-time restore if necessary.

When you stop a DB instance, it retains its DNS endpoint. If you stop a DB instance that has a public IP address, Amazon RDS releases its public IP address. When the DB instance is restarted, it has a different public IP address.

Note: You should always connect to a DB instance using the DNS endpoint, not the IP address.

Cost Details:

Here we’re relying on four key services: IAM Role, Policy, EventBridge Rule, and Lambda Function.

Creating IAM roles and policies is usually free, but if you heavily use IAM features like frequent role and policy changes, there might be minor API request charges, which are generally low and not a major concern.

Amazon EventBridge provides a free tier with 1 million monthly events for the first year, and afterward, charges are based on event publishing and custom event delivery, usually at a reasonable cost tied to your usage.

AWS Lambda charges depend on request count and execution duration. The first 1 million requests monthly are free; after that, you pay per request. You’re billed for the precise execution time in 1 ms increments, based on how long your function runs, tied to its usage pattern.

Next up is a  step by step process, covering every single detail along the way.

Simplify Amazon RDS Stop Scheduling 2

Go and click to the database and click on create database  button.

Simplify Amazon RDS Stop Scheduling 3

Click on Tags and On the Tags tab underneath the instance details, choose to Add Tags.  This step is optional

Simplify Amazon RDS Stop Scheduling 4

For the Tag key, enter UAT-TEST . [This is optional]

For Value, enter Auto-Shutdown.  [This is optional]

Choose Add.

Simplify Amazon RDS Stop Scheduling 5

Create an IAM policy and role for Lambda:

We now create an IAM policy and role for Lambda to stop the instances.

1.On the IAM console, under Access Management in the navigation pane, choose Policies.

2.Choose Create policy.

Simplify Amazon RDS Stop Scheduling 6

3. On the JSON tab, enter the following policy code.

Simplify Amazon RDS Stop Scheduling 7

# Policy
{
“Version”: “2012-10-17”,
“Statement”: [
  {
  “Sid”: “VisualEditor0”,
  “Effect”: “Allow”,
  “Action”: [
    “rds:DescribeDBClusterParameters”,
    “rds:StartDBCluster”,
    “rds:StopDBCluster”,
    “rds:DescribeDBEngineVersions”,
    “rds:DescribeGlobalClusters”,
    “rds:DescribePendingMaintenanceActions”,
    “rds:DescribeDBLogFiles”,
    “rds:StopDBInstance”,
    “rds:StartDBInstance”,
    “rds:DescribeReservedDBInstancesOfferings”,
    “rds:DescribeReservedDBInstances”,
    “rds:ListTagsForResource”,
    “rds:DescribeValidDBInstanceModifications”,
    “rds:DescribeDBInstances”,
    “rds:DescribeSourceRegions”,
    “rds:DescribeDBClusterEndpoints”,
    “rds:DescribeDBClusters”,
    “rds:DescribeDBClusterParameterGroups”,
    “rds:DescribeOptionGroups”
  ],
  “Resource”: [
    “arn:aws:rds:ap-south-1:001626390090:db:database-1” #Put your RDS Database ARN
  ]
  }
]
}

4. Choose Next and then Write the Policy name and details as suitable.

Simplify Amazon RDS Stop Scheduling 8

5. Add Tags and then click on Create policy. (Adding tags  is an optional step)

Simplify Amazon RDS Stop Scheduling 9

We will now create an IAM role.

1. In the navigation pane, choose Roles.

2.Choose Create role.

Simplify Amazon RDS Stop Scheduling 10

3.To select the type of trusted entity, choose AWS service.

4.For Common use cases, choose Lambda.

5. Choose Next.

Simplify Amazon RDS Stop Scheduling 11

6.Search for and select the policy you created (RDS_Stop_Policy).

Simplify Amazon RDS Stop Scheduling 12

7. For Role name, enter Rdsstop_Lambda.

Simplify Amazon RDS Stop Scheduling 13

8.In the Tags section, provide your key and value (Optional).

Simplify Amazon RDS Stop Scheduling 14

9.Review the attached policies and choose Create role.

You can now attach this role while creating your Lambda functions.

Create your Lambda function to stop the database.

1. On the Lambda console, choose Functions in the navigation pane.

2. Choose the Create function.

Simplify Amazon RDS Stop Scheduling 15

3 For the Function name, enter stoprds_lambda_function.

4. For Runtime, choose latest Python package available.

5. For the Execution role, select Use an existing role.

6. For the Existing role, choose the role you created (Rdsstop_Lambda).

Simplify Amazon RDS Stop Scheduling 16

7. Choose the Create function.

Simplify Amazon RDS Stop Scheduling 17

8. On the function details page, navigate to the function code.

9. Delete the sample code and enter the below mentioned code:

NOTESubstitute the region in the provided code with the one where your RDS is located, and replace the RDS database identifiers with the actual names of your databases.

import boto3

def lambda_handler(event, context):
    # Replace with your AWS region
    region = ‘ap-south-1’                     # Change this to your Region
   
    # Replace with your RDS instance identifiers
    instance_ids = [‘database-1’]             #Change this to your DB Identifiers     
   
    rds_client = boto3.client(‘rds’, region_name=region)
   
    for instance_id in instance_ids:
        try:
            response = rds_client.stop_db_instance(DBInstanceIdentifier=instance_id)
            print(f”Stopping RDS instance {instance_id}: {response})
        except Exception as e:
            print(f”Error stopping RDS instance {instance_id}: {e})
   
    return {
        ‘statusCode’: 200,
        ‘body’: ‘RDS instances stopping…’
    }

10.Change the name of the file to “lambda_function.py.”

Simplify Amazon RDS Stop Scheduling 18

11. First, click on “File,” and then proceed to click on “Save.”

Scroll down and select the “Edit” button under Runtime Settings.

Simplify Amazon RDS Stop Scheduling 19

In the “handler” section, type the file name along with the function name like this: “lambda_function.lambda_handler,” then click on the “Save” button.

Simplify Amazon RDS Stop Scheduling 20

12.Navigate to the ‘Configuration’ tab and choose ‘Environment Variables’. Click on the EDIT and add the Environment Variables as shown below.

Simplify Amazon RDS Stop Scheduling 21

13.To ensure the code you entered is used during testing, make sure to click the “Deploy” button; otherwise, the default code will be executed.

Simplify Amazon RDS Stop Scheduling 22

14.Select “Test,” and then proceed to choose “Configure Test Event.”

Simplify Amazon RDS Stop Scheduling 23

15.Name the event “StopRDS_Event” and save your changes.

Simplify Amazon RDS Stop Scheduling 24

   16. Click the “Test” button to verify the functionality of the lambda function. If you encounter an error indicating that the database is not in an available state, this suggests that the lambda function is operational. Since you have already stopped the database, it’s a sign that the function is running as expected.

Simplify Amazon RDS Stop Scheduling 25

# Execution Results and Logs

Test Event Name
stopRDS

Response
{
  “statusCode”: 200,
  “body”: “RDS instances stopping…”
}

Function Logs
START RequestId: 86572816-41da-4973-a240-b903dd07fd87 Version: $LATEST
Error stopping RDS instance database-1: An error occurred (InvalidDBInstanceState) when calling the StopDBInstance operation: Instance database-1 is not in available state.
END RequestId: 86572816-41da-4973-a240-b903dd07fd87
REPORT RequestId: 86572816-41da-4973-a240-b903dd07fd87 Duration: 1927.93 ms Billed Duration: 1928 ms Memory Size: 128 MB Max Memory Used: 75 MB Init Duration: 316.53 ms

Request ID
86572816-41da-4973-a240-b903dd07fd87

To further validate the function, you can start the database and then run the lambda function code to see if it successfully stops the database.

  1. You can see on the function detail page that the function was successful.

Simplify Amazon RDS Stop Scheduling 26

After creating and testing functions, we can create EventBridge rules to trigger these functions as needed.

Create your Amazon EventBridge Rule:

Amazon EventBridge rules trigger the functions we created to stop the tagged database. For this post, we configure them to trigger on the Event pattern.

1.On the EventBridge console, under Events in the navigation pane, choose Rules. Under the Create rule section, define a name for the rule as shown below and click Next.

Simplify Amazon RDS Stop Scheduling 27

Simplify Amazon RDS Stop Scheduling 28

2.In the Event Pattern settings, select “AWS Service” as “RDS” and “Event Type” as “RDS DB Instance event.” Click on “Edit Pattern” and then input the pattern provided below.

Simplify Amazon RDS Stop Scheduling 29

#Event Pattern 
{
“source”: [“aws.rds”],
“detail-type”: [“RDS DB Instance Event”],
“resources”: [“arn:aws:rds:ap-south-1:001626202209022090:db:database-1”],    #Database ARN
“detail”: {
“EventID”: [“RDS-EVENT-0088”]                   #Event ID
}
}

Simplify Amazon RDS Stop Scheduling 30

3.Proceed by clicking on the “Next” button.

4.Choose the target Lambda function and pick the function named “StopRDS_Lambda_Function.”

Simplify Amazon RDS Stop Scheduling 31

5.Proceed by clicking on the “Next” button.

6.Add any necessary tags [optional], and then continue by clicking the “Next” button.

Simplify Amazon RDS Stop Scheduling 32

Finalize the process by clicking on the “Create Rule” button.

Simplify Amazon RDS Stop Scheduling 33

Simplify Amazon RDS Stop Scheduling 34

Simplify Amazon RDS Stop Scheduling 35

7. Once you’ve set up the EventBridge Rule, it’s essential to initiate the process by starting the database. This step is vital to ensure that all the services are working as intended. Remember, the database will automatically stop according to the defined rules.

Once you’ve restarted the database, you’ll notice that it eventually stops on its own. Please be patient and allow some time to observe the result.

Simplify Amazon RDS Stop Scheduling 36

8.For further confirmation, you can navigate to CloudTrail and access the event history. In the lookup attributes, choose “Event Name” and input “StopDBInstance.” You’ll notice a successful event trigger, as depicted in the image below.

Simplify Amazon RDS Stop Scheduling 37

9. You can also verify the correct functioning of your Lambda Function by going to the monitoring section of the specific function. Please allow around 1 hour for the data to be displayed, as indicated in the image.


Simplify Amazon RDS Stop Scheduling 38

10.Similar to the EventBridge Rule, you can head to the Monitoring section to view invocations and confirm that the trigger rule is functioning as expected.

Simplify Amazon RDS Stop Scheduling 39

These procedures and steps are applicable to multiple databases as well. To achieve this, it’s necessary to incorporate the appropriate Amazon Resource Names (ARNs) into the policy of the additional RDS instances. Furthermore, make adjustments within the EventBridge Rule, and integrate the database identifier of each respective RDS instance into the Lambda Function.

For enabling the initiation of RDS instances, establish an extra Lambda function. To adapt it for starting RDS instances, simply modify the code by replacing instances of “stop” with “start.” It’s worth noting that when making alterations to the EventBridge rule, a sequence involving stopping and subsequently starting the database must be followed. This sequence ensures that EventBridge accurately identifies the action as a restart.

Conclusion

Automating the start and stop RDS using AWS Lambda and EventBridge is an excellent practice for efficient resource management and cost savings.

 This configuration can be further tuned and adjusted based on desired business needs, helping to optimize your AWS environment.

0
Taniya Pan

Taniya Pan

Add comment