For many applications, traffic and workload follow predictable patterns tied to dates and events. An e-commerce website may see a surge around holidays, whereas a software as a service (SaaS) app may experience an increase after monthly budget refreshes, and these events can be quite demanding in terms of disk IOPS and throughput.
Rather than paying for high Amazon Relational Database Services (Amazon RDS) storage IOPS and throughput for the whole month unnecessarily, you can optimize Amazon RDS costs by auto scaling IOPS and throughput to match these predictable spikes.
In this post, we explain how you can use Amazon RDS IOPS and throughput provisioned settings, automate scaling around monthly and seasonal peaks, and decrease settings during slower weeks. By right-sizing IOPS and throughput levels to your workload’s typical cycles, you can reduce Amazon RDS spend while still getting great performance when you need it most.
Solution overview
Our solution uses an AWS Lambda function to scale storage settings up and down according to a schedule defined in the RDS instance’s tags. The workflow contains the following steps and key components:
Amazon EventBridge Scheduler is responsible for running tasks based on a schedule. For this solution, the scheduler calls a Lambda function one time per day.
A Lambda function written in Python scans RDS resources in the account, looking for specific tags associated with the instances.
When the function finds specific tags, it checks if the current state of IOPS settings matches what is
expected.
The function uses Boto3 calls to Amazon RDS, changing IOPS settings as and when required.
Logs are sent to Amazon CloudWatch logs.
The following diagram illustrates the solution architecture.
Although the examples use an Amazon RDS for MariaDB instance, you can also apply this guidance to RDS for PostgreSQL, RDS for MySQL, RDS for SQL Server, RDS for Oracle or RDS for Db2.
Amazon Aurora uses a completely different storage architecture and is not covered by the solution proposed in this post.
Limitations
The following are some key limitations of Amazon RDS storage operations:
Only one change on IOPS and throughput is allowed for every 6-hours or completion of storage optimization, whichever is longer. For this reason, you should select period and performance parameters wisely.
Consider using Amazon EBS-optimized instances if disk performance is important for the database, and io2 Block Express if you require more performance and durability than what gp3 storage can provide. Io2 Block Express can achieve up to 256,000 IOPS.
For gp3 volumes, provisioned IOPS and throughput change is only allowed for volumes of 200 GB or larger for Oracle, 400 GB or larger for Db2, MariaDB, MySQL, and PostgreSQL, and no storage allocation limit is set for SQL Server. With less than the limit, a fixed baseline of 3,000 IOPS/125MiB/s applies and the setting can’t be changed.
Baseline is included in the price, you only pay extra if you provision more capacity.
Gp3 storage larger than the limit described above can have provisioned IOPS set in the range 12,000-64,000, and throughput in the range 500-4,000 MiB/s, but you must make sure the instance class supports the amount of IOPS and throughput set, otherwise, the instance class will be the bottleneck.
For io2 Block Express, throughput scales proportionally up to 0.256 MiB/s per provisioned IOPS, and up to 4,000 MiB/s.
For more information about these and other limitations, refer to Working with storage for Amazon RDS DB instances.
Prerequisites
For this walkthrough, you should have the following prerequisites:
An AWS account
An RDS database with a volume type that can configure IOPS (gp3, io1, or io2 Block Express)
Basic knowledge of Amazon EventBridge, Lambda functions, and how to navigate AWS resources
Create the Lambda function
Complete the following steps to create your Lambda function. For simplicity, the following example covers how to automate IOPS scaling, but you can adapt the code to scale throughput as well.
Create an AWS Identity and Access Management (IAM) Lambda execution role to give the Lambda function permission to change the RDS instance’s storage parameters. The following privileges are needed, including permissions required to log messages in Amazon CloudWatch:
rds:DescribeDBInstances
rds:ListTagsForResource
rds:ModifyDBInstance
logs:CreateLogGroup
logs:CreateLogStream
logs:PutLogEvents
The following is an example IAM policy that provides the required privileges (please note, account ID in the example has to be changed to match your account ID):
Create a Lambda function in Python and attach the previously created IAM role. Configure the Lambda function to do the following:
Use the Boto3 RDS client to call describe_db_instances() to get all RDS instances.
Loop through the instances and check if they have the following tags:
IOPSHighSetting – The number of IOPS to be set when the instance is in high demand (inside the interval).
IOPSLowSetting – The number of IOPS to be set when the instance is in low demand (outside the interval).
IOPSHighDayInterval – The interval of days in which the database is in high demand.
If set as 5-10, the high setting will be set when the automation runs between the fifth and tenth of each month; outside this period, the low setting will be applied.
If tags exist, parse the IOPSHighDayInterval tag to get the start and end days of the high IOPS period.
Get the current day of the month and check if it falls in the high IOPS period.
If it does, call modify_db_instance() to set the IOPS to the IOPSHighSetting value.
If not, call modify_db_instance() to set IOPS to the IOPSLowSetting value.
The following code works for scaling IOPS:
To invoke the Lambda function daily at midnight, create a EventBridge Scheduler rule with a scheduled expression:
cron(0 0 * * ? *)
This triggers the Lambda function every day at 00:00 UTC, but you can choose a more appropriate time for the change. There won’t be any downtime during the operation.
Make sure the Lambda function has enough timeout and memory allocated to complete the required actions. The default of 3 seconds and 128 MB works in a test environment with one RDS instance, but may not work with multiple instances.
Test the Lambda function automation
Complete the following steps to test the automation:
Create an RDS instance with the three tags set (IOPSHighSetting, IOPSLowSetting, IOPSHighDayInterval). Consult Amazon RDS DB instance storage to make sure the settings are in the boundaries of IOPS settings for your instance and storage type, as discussed before.
On the Lambda console, set up a test environment. For the provided code, no special variable is needed in the test event.
Run the test event and review the output.
In the previous example, our RDS instance was already set with the expected IOPS setting for today.
Let’s change the tag IOPSHighDayInterval to include today in the high demand interval.
If you run the Lambda test event again, you can see the IOPS settings being changed for your instance.
The instance’s status changes to Modifying. The change can take a while to complete.
After Modifying, the status changes to Storage-optimization. No new changes can be made to the storage for at least 6 hours, and while the instance is in any of these statuses, if you change the interval again and run the Lambda function, you get the following error: An error occurred (InvalidParameterCombination) when calling the ModifyDBInstance operation: You can’t currently modify the storage of this DB instance because the previous storage change is being optimized.
For more details about storage optimization, refer to Working with storage for Amazon RDS DB instances.
If this were a real scenario and you had EventBridge set up, the Lambda function would try to run the change again the following day.
Schedule the Lambda function to run daily
To adjust the storage settings according to the scheduler tags, the Lambda function has to be run daily. You can achieve this by creating a scheduler event in EventBridge. Complete the following steps:
On the EventBridge console, under Scheduler in the navigation pane, choose Schedules.
Choose Create schedule.
Provide the rule details, including the rule name, optional description, and event bus.
For Rule type, select Schedule.
Choose Continue in EventBridge Scheduler.
For Occurrence, select Recurring schedule.
For Schedule type, select Cron-based schedule.
For Cron expression, enter the cron parameters. For this post, we schedule the action to run daily at midnight.
Choose a flexible time window of 15 minutes, then choose Next.
For Select target, select Invoke AWS Lambda.
For Lambda function, choose your Lambda function.
Leave Payload blank, then choose Next.
For Action after schedule completion, select NONE.
In the Permissions section, select Create a new role for the schedule and enter a role name, unless you have a specific role created already.
Choose Next.
Confirm everything is as expected and choose Create schedule.
The Lambda function will run as scheduled. To monitor the run, refer to Monitoring Amazon EventBridge.
Clean up
If you followed this guide to create a test environment, you should delete the following resources to avoid ongoing costs:
RDS instance
EventBridge schedule
Lambda function
Lambda IAM role
Conclusion
In this post we outlined a method for automatically adjusting storage performance parameters based on a scheduler set in RDS instance tags. The solution is based on an EventBridge scheduler and a Lambda function written in Python, and can potentially save costs and improve database performance. The solution can scale not only IOPS parameters but also throughput, depending on your requirements.
Leave your thoughts or questions in the comments section.
About the Author
Ivan Schuster is a Senior Database Specialty Architect at AWS. He has over 20 years of experience in the technology industry, mostly with databases. In his role as a professional services consultant, Ivan has supported multiple customers in transitioning their workloads to the AWS Cloud.
Source: Read More