Reduce your database cost with scheduled lambda functions

Tim Rosenblüh
25. October 2022
Reading time: 4 min
Reduce your database cost with scheduled lambda functions

Introduction

Databases are one of the most important components of any IT infrastructure. They store important company-relevant data and thus contribute to the success of every company. However, the costs they can cause should not be neglected. Today, thanks to the numerous offerings from AWS, it is no longer necessary to operate databases in your own on-premises infrastructure. Rather, with services such as Relational Database Service (RDS), DynamoDB or Timestream, nearly every conceivable type of database can be mapped to the AWS Cloud. Both of the latter examples have the additional advantage that there is no Idle time. This means that there are no charges if no queries are made to the corresponding database. RDS does not offer this advantage so far and therefore costs are still generated, even if the database does not process any queries. This can lead to unnecessary charges, which should be avoided if possible.

Scenario

The following example is intended to illustrate the cost savings that may theoretically be possible. We assume 6 databases, which all have the same configurations. These databases are currently running 24/7. The cost saving potential results from the fact that the databases are usually only required during standard working hours, i.e. approx. 8-5pm. Outside these hours there are no requests.

DB-Config

Using the AWS Pricing Calculator, a theoretical cost of nearly $2000/month was estimated.

Pricing-Calculator Before

By adjusting the operating hours of the databases to 9h per day, the cost can be reduced to about $800/month.

Pricing-Calculator After

Since this is a simple example with Singe-AZ instances, the cost savings may vary depending on the environment.

Solution

Since RDS does not have its own mechanism to define the operational times of the instances, a combination of AWS EventBridge and AWS Lambda will be used.

AWS EventBridge is a serverless EventBus, which on one hand offers the possibility to forward messages to specific recipients based on processing rules and on the other hand can cause a time-based triggering of an event via scheduled rules. For the latter a cron expression is needed, which defines the exact time at which an event should be triggered.

(a cron expressione for EventBridge could look like this, triggering every day at 10:00 UTC)

cron(0 10 * * ? *)

AWS Lambda is a serverless computing service that allows developers to write and run code without having to explicitly define or provision server instances. Lambda functions can be triggered by a variety of AWS services/events, including EventBridge rules.

In this example, tags are also used to identify the databases that are started/stopped automatically.

DB-Tags

With the understanding of both services, an architecture could look like this:

Example-Architecture

[/et_pb_text][et_pb_text admin_label=”Code” _builder_version=”4.18.0″ _module_preset=”default” custom_padding=”30px||||false|false” locked=”off” global_colors_info=”{}”]

Code excerpts

(Code written in Python)

With the call describe_db_instances(), all database instances in the account can be listed.

client = boto3.client('rds')
instances = client.describe_db_instances()

To read the tags of the associated database, the list_tags_for_resource(ResourceName=ARN_OF_THE_INSTANCE) function of the AWS SDK can be called.

(continuing from the above excerpt)

for i in instances['DBInstances']:
    arn = i['DBInstanceArn']
    db_tags = client.list_tags_for_resource(ResourceName=arn)

At this point the tags (variable db_tags) could be checked for the presence of specific keys/values.

After the individual checks the database can be started with <code>start_db_instance(DBInstanceIdentifier=DB_NAME)</code> or stopped with `stop_db_instance(DBInstanceIdentifier=DB_NAME).</p>

(continuing from the above excerpt)

    # Checking of keys/values has been omitted at this point 
    client.
    start_db_instance(DBInstanceIdentifier=i["DBInstanceIdentifier"])

    client.
    stop_db_instance(DBInstanceIdentifier=i["DBInstanceIdentifier"])

Summary

Databases are an important part of any enterprise infrastructure. It is therefore all the more important to look for cost-saving opportunities in this area as well. Automated approaches using various AWS services, such as AWS Lambda and Amazon EventBridge, can be used to implement a solution quickly and easily. This can significantly reduce the operational charges of the databases, which can lead to a considerable monthly cost reduction depending on the number of database instances.

In the example given, the cost could thus be reduced by approx. 60%. However, since there were only 6 database instances with little storage (20GB), it can be assumed that an even higher cost reduction could be achieved if the infrastructure had been more complex and contained a wider variety of RDS-Databases.

Final note

The presented solution is limited to single DB instances. Aurora clusters were ignored here (but can also be started/stopped automatically). Furthermore, it is unfortunately not possible to automatically stop databases with read-replicas or read-replicas themselves.

For more information: RDS-Docs