← All posts

Technical Post · Amazon Rds

Implementing Autoscaling for Amazon RDS Instances

To meet connectivity and workload requirements, Aurora Auto Scaling dynamically adjusts the number of replicas…

5 min read1,163 wordsSections: 2Images: 1Code blocks: 11Jul 12, 2023

Keywords

Share
Comment

To meet connectivity and workload requirements, Aurora Auto Scaling dynamically adjusts the number of Aurora Replicas provisioned for an Aurora DB cluster. Aurora Auto Scaling is available for both Aurora MySQL and Aurora PostgreSQL. It lets your Aurora DB cluster handle sudden increases in connectivity or workload. When connectivity or workload drops, Aurora Auto Scaling removes unnecessary Aurora Replicas so you don't pay for provisioned DB instances you aren't using.

You define and apply Aurora scaling policies to a DB cluster. A scaling policy sets the minimum and maximum number of Aurora Replicas that Aurora Auto Scaling can manage. Based on those policies, Aurora Auto Scaling scales the number of Aurora Replicas up or down in response to actual workloads, using Amazon CloudWatch metrics and target values.

That said, you can also configure autoscaling for Amazon RDS instances to handle swings in CPU and memory demand. Unlike Amazon Aurora, Amazon RDS has no built-in native autoscaling feature. In this post, we'll look at how to implement this automation with both AWS CloudFormation and Terraform.

Autoscaling Amazon RDS DB Instances with AWS CloudFormation

Before you start, make sure the AWS Command Line Interface (CLI) is installed and properly configured in your local environment.

Step 1: Create the RDS instance group: Start by creating the RDS instance group. Multi-AZ will be enabled by default to ensure high availability. You can use an AWS CloudFormation template to do this. Create a file named "rds-autoscaling.yaml" and add the following code:

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  RDSInstance:
    Type: 'AWS::RDS::DBInstance'
    Properties:
      DBInstanceIdentifier: MyDBInstance
      Engine: aurora
      EngineVersion: '5.7.mysql_aurora.2.03.2'
      DBInstanceClass: db.t2.medium
      AllocatedStorage: '20'
      MultiAZ: true

Be sure to replace "MyDBInstance" with a unique name for your RDS instance.

Step 2: Create the EC2 scaling group: Now create an Amazon EC2 scaling group so the RDS instances can scale automatically. Create a new file named "ec2-autoscaling.yaml" and add the following code:

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  EC2AutoScalingGroup:
    Type: 'AWS::AutoScaling::AutoScalingGroup'
    Properties:
      AvailabilityZones:
        - 'us-east-1a'
        - 'us-east-1b'
      LaunchConfigurationName: MyLaunchConfiguration
      MinSize: 1
      MaxSize: 5
      DesiredCapacity: 2

  MyLaunchConfiguration:
    Type: 'AWS::AutoScaling::LaunchConfiguration'
    Properties:
      ImageId: ami-12345678  # Substitua pelo ID da imagem EC2 desejada
      InstanceType: t2.micro
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash
          echo "Starting RDS autoscaling..."
          aws rds modify-db-instance \
            --db-instance-identifier MyDBInstance \
            --db-instance-class db.t2.medium

Be sure to replace "us-east-1a" and "us-east-1b" with the Availability Zones you want, and "ami-12345678" with the ID of the EC2 image you want.

Step 3: Create the scaling policy: Next, create a scaling policy that defines the automatic scaling rules for the EC2 scaling group. Add the following code to the "ec2-autoscaling.yaml" file, under the "Resources" section:

MyScalingPolicy:
    Type: 'AWS::AutoScaling::ScalingPolicy'
    Properties:
      AdjustmentType: ChangeInCapacity
      AutoScalingGroupName: !Ref EC2AutoScalingGroup
      Cooldown: '300'
      ScalingAdjustment: '1'

Step 4: Create the alarm action: Now create an alarm action that monitors RDS metrics and triggers automatic scaling of the EC2 scaling group when needed. Add the following code to the "ec2-autoscaling.yaml" file, under the "Resources" section:

MyAlarm:
    Type: 'AWS::CloudWatch::Alarm'
    Properties:
      AlarmDescription: 'RDS CPU Utilization'
      Namespace: AWS/RDS
      MetricName: CPUUtilization
      Dimensions:
        - Name: DBInstanceIdentifier
          Value: MyDBInstance
      Statistic: Average
      Period: '60'
      EvaluationPeriods: '2'
      Threshold: '70'
      ComparisonOperator: GreaterThanOrEqualToThreshold
      AlarmActions:
        - !Ref MyScalingPolicy

Be sure to replace "MyDBInstance" with the unique name of your RDS instance.

Step 5: Deploy the stacks to AWS CloudFormation Now you can deploy the stacks to AWS CloudFormation using the AWS CLI. Open a terminal and run the following commands:

aws cloudformation create-stack --stack-name RDSAutoscaling --template-body file://rds-autoscaling.yaml

aws cloudformation create-stack --stack-name EC2Autoscaling --template-body file://ec2-autoscaling.yaml

Wait until stack creation completes.

Congratulations! You've successfully set up an autoscaling environment for Amazon RDS instances with Multi-AZ enabled. The EC2 scaling group will monitor RDS metrics and automatically add or remove instances based on the scaling rules you defined.

Autoscaling Amazon RDS DB Instances with Terraform

Step 1: Configure the provider and create the RDS instance group: Start by creating a directory for your project and navigating into it. Then create a file named main.tf and add the following code:

# Configuração do provedor AWS
provider "aws" {
  region = "us-east-1" # Substitua pela região desejada
}

# Recurso de instância RDS
resource "aws_db_instance" "rds_instance" {
  identifier             = "my-db-instance" # Substitua pelo nome exclusivo da instância do RDS
  engine                 = "aurora"
  engine_version         = "5.7.mysql_aurora.2.03.2"
  instance_class         = "db.t2.medium"
  allocated_storage      = 20
  multi_az               = true
}

Be sure to replace “my-db-instance” with the unique name you want to give your RDS instance.

Step 2: Create the EC2 scaling group: Now let's create the Amazon EC2 scaling group. In the same main.tf file, add the following code below the aws_db_instance resource:

# Recurso de grupo de escalonamento do EC2
resource "aws_autoscaling_group" "ec2_autoscaling_group" {
  name                 = "my-autoscaling-group" # Substitua pelo nome exclusivo do grupo de escalonamento
  min_size             = 1
  max_size             = 5
  desired_capacity     = 2
  availability_zones   = ["us-east-1a", "us-east-1b"] # Substitua pelas zonas de disponibilidade desejadas

  launch_configuration = aws_launch_configuration.my_launch_configuration.name
}

# Recurso de configuração de inicialização da instância EC2
resource "aws_launch_configuration" "my_launch_configuration" {
  name_prefix          = "my-launch-config" # Substitua pelo prefixo do nome da configuração de inicialização
  image_id             = "ami-12345678" # Substitua pelo ID da imagem EC2 desejada
  instance_type        = "t2.micro"

  user_data = <<-EOF
    #!/bin/bash
    echo "Starting RDS autoscaling..."
    aws rds modify-db-instance \
      --db-instance-identifier my-db-instance \
      --db-instance-class db.t2.medium
  EOF
}

Be sure to replace “my-autoscaling-group” with the unique name you want to give the scaling group, and “ami-12345678” with the ID of the EC2 image you want.

Step 3: Create the scaling policy: Now let's create a scaling policy that defines the automatic scaling rules for the EC2 scaling group. In the same main.tf file, add the following code below the aws_launch_configuration resource:

# Recurso de política de escalonamento
resource "aws_autoscaling_policy" "my_scaling_policy" {
  name                   = "my-scaling-policy" # Substitua pelo nome exclusivo da política de escalonamento
  autoscaling_group_name = aws_autoscaling_group.ec2_autoscaling_group.name
  adjustment_type        = "ChangeInCapacity"
  scaling_adjustment     = 1
  cooldown               = 300
}

Be sure to replace “my-scaling-policy” with the unique name you want to give the scaling policy.

Step 4: Create the alarm action: Now let's create an alarm action that monitors RDS metrics and triggers automatic scaling of the EC2 scaling group when needed. In the same main.tf file, add the following code below the aws_autoscaling_policy resource:

# Recurso de ação de alarme
resource "aws_cloudwatch_metric_alarm" "my_alarm" {
  alarm_name          = "rds-cpu-utilization"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = 2
  metric_name         = "CPUUtilization"
  namespace           = "AWS/RDS"
  period              = 60
  statistic           = "Average"
  threshold           = 70
  alarm_description   = "RDS CPU Utilization"
  alarm_actions       = [aws_autoscaling_policy.my_scaling_policy.arn]

  dimensions = {
    DBInstanceIdentifier = aws_db_instance.rds_instance.identifier
  }
}

Step 5: Initialize and apply the configuration: Now open a terminal in the project folder and run the following commands:

terraform init

This initializes the project directory and downloads the required plugins.

terraform apply

This creates the infrastructure specified in the Terraform files. Confirm with “yes” when prompted.

Congratulations! You've successfully set up an autoscaling environment for Amazon RDS instances with Multi-AZ enabled using Terraform. The EC2 scaling group will monitor RDS metrics and automatically add or remove instances based on the scaling rules you defined.

Comments

Every comment is moderated before it appears here. Nothing is published automatically.

Loading…