During periods of high CPU usage, database performance often degrades due to slow-running queries, impacting application performance and user experience. Manually identifying and mitigating these issues is inefficient and time-consuming.
This workflow automates the process of enabling slow query logging only when CPU utilization crosses a defined threshold. Instead of keeping the slow_query_log parameter enabled at all times (which results in large log files), this system dynamically enables logging only when necessary using AWS Lambda, SNS, and CloudWatch Alarms.
- Immediate Response: Automatically enables slow query logging when high CPU usage is detected, allowing rapid analysis.
- Operational Efficiency: Eliminates manual intervention and monitoring, freeing up resources for critical tasks.
- Enhanced Performance: Ensures optimal database performance by dynamically managing parameters.
If you don't have an RDS instance, follow this guide to create one:
🔗 AWS RDS Setup Guide
-
Navigate to AWS CloudWatch
- Log in to the AWS Console and open the CloudWatch service.
-
Create an Alarm
- Click on Alarms → Create Alarm
-
Select Metric
- Click on Select Metric
-
Choose RDS Metrics
- Select RDS → DBInstanceIdentifier
-
Set CPU Utilization Threshold
- Choose CPUUtilization metric and define the threshold (e.g., 80%).
-
Configure SNS Notification
- In Configure Actions, select an SNS Topic (
my-database-high-cpu). - (If SNS is not yet created, you can modify the alarm later to include the topic.)
- In Configure Actions, select an SNS Topic (
-
Name & Create Alarm
- Give a descriptive name and review the settings before creating the alarm.
We will use the Serverless Framework to deploy the Lambda function and trigger it via SNS events.
🔹 Install & Configure AWS CLI
🔹 Install Serverless Framework
-
Initialize Project
- Choose
AWS - Python - Starter
- Choose
-
Name the Project (e.g.,
Auto-SlowQuery) -
Modify handler.py
- Open
handler.pyand replace its content with the Lambda function script. - Code can be found here
- Open
-
Update serverless.yml
service: Auto-slowQuery frameworkVersion: '3' provider: name: aws runtime: python3.9 functions: slow_query_logs: handler: handler.slow_query_logs events: - sns: my-database-high-cpu
Run the following command in the Serverless project directory:
deploy --stage production --region ap-south-1Monitor Deployment Progress
Check the CloudFormation stack in AWS Console.
Once deployed, the Lambda function will be automatically triggered by the SNS event:
Attach the following policy to the Lambda execution role to allow it to modify RDS parameters:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:TagResource"
],
"Resource": [
"arn:aws:logs:ap-south-1:516978611867:log-group:/aws/lambda/Auto-slowQuery-production*:*"
],
"Effect": "Allow"
},
{
"Action": [
"logs:PutLogEvents"
],
"Resource": [
"arn:aws:logs:ap-south-1:516978611867:log-group:/aws/lambda/Auto-slowQuery-production*:*:*"
],
"Effect": "Allow"
},
{
"Sid": "Statement1",
"Effect": "Allow",
"Action": [
"rds:DescribeDBParameterGroups",
"rds:DescribeDBParameters",
"rds:ModifyDBParameterGroup"
],
"Resource": [
"arn:aws:rds:ap-south-1:516978611867:pg:my-database-pg"
]
}
]
}-
Advanced Slow Query Log Analysis
- Use tools like pt-query-digest (Percona Toolkit) or mysqlslowdump to analyze logs.
- Save analyzed logs to Amazon S3 and generate a pre-signed URL for secure sharing.
-
High CPU Usage Process Export
- Extract high CPU usage processes from RDS instances.
- Identify resource-intensive queries and optimize them.
This workflow significantly enhances database performance monitoring while reducing manual intervention, making it a scalable and efficient solution for production environments.













