Scheduled Scaling of Amazon Aurora Serverless with Amazon EventBridge Scheduler

Listen to this Post

In the world of cloud computing, optimizing resource allocation is crucial for cost efficiency. Many applications experience fluctuating demand based on the time of day or specific days, leading to wasted resources if not managed properly. Amazon Aurora Serverless v2 offers a solution by allowing databases to scale down when not in use, but scaling to zero may not always be feasible due to startup times. However, scaling to a minimal capacity during low-demand periods can still save significant costs. This article explores how to implement scheduled scaling for Amazon Aurora Serverless using the Amazon EventBridge Scheduler.

You Should Know:

  1. Amazon Aurora Serverless v2: This version of Aurora automatically adjusts database capacity based on application demand. It supports scaling to zero, but this feature may not be ideal for all use cases due to the time required to restart the database.

  2. Amazon EventBridge Scheduler: This service allows you to create, run, and manage scheduled tasks at scale. It integrates seamlessly with other AWS services, making it an excellent tool for automating tasks like database scaling.

  3. Cloud Development Kit (CDK): AWS CDK is an open-source software development framework to define cloud infrastructure in code and provision it through AWS CloudFormation.

Steps to Implement Scheduled Scaling:

1. Install AWS CDK:

npm install -g aws-cdk

2. Initialize a New CDK Project:

mkdir aurora-scheduled-scaling
cd aurora-scheduled-scaling
cdk init app --language=typescript

3. Add Dependencies:

npm install @aws-cdk/aws-rds @aws-cdk/aws-events @aws-cdk/aws-events-targets @aws-cdk/aws-iam

4. Define the Stack:

import * as cdk from 'aws-cdk-lib';
import * as rds from 'aws-cdk-lib/aws-rds';
import * as events from 'aws-cdk-lib/aws-events';
import * as targets from 'aws-cdk-lib/aws-events-targets';
import * as iam from 'aws-cdk-lib/aws-iam';

export class AuroraScheduledScalingStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

// Create an Aurora Serverless Cluster
const cluster = new rds.ServerlessCluster(this, 'AuroraCluster', {
engine: rds.DatabaseClusterEngine.AURORA_MYSQL,
scaling: {
autoPause: cdk.Duration.minutes(10), // Pause after 10 minutes of inactivity
minCapacity: rds.AuroraCapacityUnit.ACU_2, // Minimum capacity
maxCapacity: rds.AuroraCapacityUnit.ACU_16, // Maximum capacity
},
});

// Create an EventBridge Rule to Scale Up
const scaleUpRule = new events.Rule(this, 'ScaleUpRule', {
schedule: events.Schedule.cron({ minute: '0', hour: '8' }), // Scale up at 8 AM
});

scaleUpRule.addTarget(new targets.AwsApi({
service: 'RDS',
action: 'modifyCurrentDBClusterCapacity',
parameters: {
DBClusterIdentifier: cluster.clusterIdentifier,
Capacity: 16, // Scale to 16 ACUs
},
policyStatement: new iam.PolicyStatement({
actions: ['rds:ModifyCurrentDBClusterCapacity'],
resources: [cluster.clusterArn],
}),
}));

// Create an EventBridge Rule to Scale Down
const scaleDownRule = new events.Rule(this, 'ScaleDownRule', {
schedule: events.Schedule.cron({ minute: '0', hour: '20' }), // Scale down at 8 PM
});

scaleDownRule.addTarget(new targets.AwsApi({
service: 'RDS',
action: 'modifyCurrentDBClusterCapacity',
parameters: {
DBClusterIdentifier: cluster.clusterIdentifier,
Capacity: 2, // Scale down to 2 ACUs
},
policyStatement: new iam.PolicyStatement({
actions: ['rds:ModifyCurrentDBClusterCapacity'],
resources: [cluster.clusterArn],
}),
}));
}
}

5. Deploy the Stack:

cdk deploy

What Undercode Say:

Scheduled scaling with Amazon Aurora Serverless and EventBridge Scheduler is a powerful way to optimize costs while maintaining performance. By automating the scaling process, you can ensure that your database resources are aligned with demand, reducing unnecessary expenses. This approach is particularly useful for applications with predictable usage patterns, such as those used during business hours.

Expected Output:

  • Cost Savings: By scaling down during off-peak hours, you can significantly reduce your AWS bill.
  • Improved Performance: Scaling up during peak hours ensures that your application remains responsive.
  • Automation: The use of EventBridge Scheduler and CDK simplifies the management of scheduled tasks, reducing the need for manual intervention.

For more details, refer to the AWS documentation on Aurora Serverless and EventBridge Scheduler.

References:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image