Aurora Serverless v2 creation using CloudFormation

TOC

Aurora Serverless v2 creation using CloudFormation

In the following pages, we have shown you how to create Aurora Serverless v1 using CloudFormation.

あわせて読みたい
Aurora Serverless v1 with CFN 【Aurora Serverless v1 with CFN】 Aurora Serverless is a managed database service provided by AWS. Amazon Aurora Serverless is an on-demand, autoscaling conf...

It was announced that Aurora Serverless v2 will also support CloudFormation on 2022/10/05.

https://aws.amazon.com/about-aws/whats-new/2022/10/amazon-aurora-serverless-v2-supports-aws-cloudformation/?nc1=h_ls

This page uses CloudFormation to create Aurora Serverless v2.

Environment

Diagram of Aurora Serverless v2 creation using CloudFormation.

The basic structure is the same as the page introduced at the beginning of this document.

Create Aurora Serverless v2 and associate it with two subnets with different AZs.
For DB engine, specify the latest version of MySQL type.

Create an EC2 instance.
Use it as a client to connect to Aurora Serverless.
The OS will be the latest version of Amazon Linux 2023.

CloudFormation template files

The above configuration is built with CloudFormation.
The CloudFormation templates are placed at the following URL

https://github.com/awstut-an-r/awstut-fa/tree/main/138

Explanation of key points of template files

Aurora Serverless v2

When creating Aurora Serverless v2 using CloudFormation, three resources are created.
Many parameters for each resource are the same as when creating a regular Aurora.
In this case, we will focus on parameters specific to Aurora Serverless v2.

DB Subnet Group

Resources:
  DBSubnetGroup:
    Type: AWS::RDS::DBSubnetGroup
    Properties:
      DBSubnetGroupName: dbsubnetgroup
      DBSubnetGroupDescription: test subnet group
      SubnetIds:
        - !Ref DBSubnet1
        - !Ref DBSubnet2
Code language: YAML (yaml)

No special configuration is required here.
Specify two or more subnets on which to place the DB clusters described below.

The point is the AZ where each subnet is created.
Note that each must be created in a different AZ.

Aurora Cluster

Resources:
  DBCluster:
    Type: AWS::RDS::DBCluster
    Properties:
      DatabaseName: !Ref DBName
      DBClusterIdentifier: !Sub "${Prefix}-dbcluster"
      DBSubnetGroupName: !Ref DBSubnetGroup
      Engine: !Ref DBEngine
      EngineVersion: !Ref DBEngineVersion
      MasterUsername: !Ref DBMasterUsername
      MasterUserPassword: !Ref DBMasterUserPassword
      ServerlessV2ScalingConfiguration:
        MaxCapacity: 1.0
        MinCapacity: 0.5
      StorageEncrypted: true
      VpcSecurityGroupIds:
        - !Ref DBSecurityGroup
Code language: YAML (yaml)

There are three points.

The first point is the Engine property.
In Aurora Serverless v2, you can choose MySQL or PostgreSQL as the DB engine.
Since we will create a MySQL type Aurora Serverless v2, we will specify “aurora-mysql” for this property.

The second point is the EngineVersion property.
This property allows you to specify the DB engine version.

Refer to the following page to check the versions that can be specified using the AWS CLI.

https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-serverless-v2.requirements.html#aurora-serverless-v2-Availability

The version of the MySQL type that can be selected in the ap-northeast-1 region can be checked with the following command.

$ aws rds describe-orderable-db-instance-options \
--engine aurora-mysql \
--db-instance-class db.serverless \
--region ap-northeast-1 \
--query 'OrderableDBInstanceOptions[].[EngineVersion]' \
--output text
8.0.mysql_aurora.3.02.0
8.0.mysql_aurora.3.02.1
8.0.mysql_aurora.3.02.2
8.0.mysql_aurora.3.02.3
8.0.mysql_aurora.3.03.0
8.0.mysql_aurora.3.03.1
8.0.mysql_aurora.3.03.1
Code language: Bash (bash)

In this case, specify “8.0.mysql_aurora.3.03.1” which is the latest version as of July 2023.

The third is the ServerlessV2ScalingConfiguration property.
This property defines the range of performance (ACU) that can be taken by the DB instances created in the cluster.

The unit of measure for Aurora Serverless v2 is the Aurora capacity unit (ACU).

Each ACU is a combination of approximately 2 gibibytes (GiB) of memory, corresponding CPU, and networking. You specify the database capacity range using this unit of measure.

Aurora Serverless v2 capacity

ACU values can be specified from 0.5 to 128 in increments of 0.5.
For this verification, we specify 0.5 as the minimum possible ACU value and 1.0 as the maximum (MinCapacity and MaxCapacity properties).

DB Instance

Resources:
  DBInstance:
    Type: AWS::RDS::DBInstance
    Properties:
      DBClusterIdentifier: !Ref DBCluster
      DBSubnetGroupName: !Ref DBSubnetGroup
      DBInstanceIdentifier: !Sub "${Prefix}-dbinstance"
      DBInstanceClass: !Ref DBInstanceClass
      Engine: !Ref DBEngine
      AvailabilityZone: !Sub "${AWS::Region}${AvailabilityZone}"
      PubliclyAccessible: false
Code language: YAML (yaml)

The DBInstanceClass property is the key.

db.serverless – A special DB instance class type used by Aurora Serverless v2.

Aurora Serverless v2 instance class type

According to the above, “db.serverless” is specified for this property.

Security Group

Resources:
  DBSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: !Sub "${Prefix}-DBSecurityGroup"
      GroupDescription: DBSecurityGroup.
      VpcId: !Ref VPC
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: !Ref MySQLPort
          ToPort: !Ref MySQLPort
          SourceSecurityGroupId: !Ref InstanceSecurityGroup
Code language: YAML (yaml)

Security group to be applied to the Aurora cluster.
The settings are the same as for a normal MySQL type RDS instance.
Allow inbound communication to the port for MySQL communication (tcp/3306).
For the Source, specify the security group to be applied to the EC2 instance described below.

(Reference) EC2 instance

Resources:
  Instance:
    Type: AWS::EC2::Instance
    Properties:
      IamInstanceProfile: !Ref InstanceProfile
      ImageId: !Ref ImageId
      InstanceType: !Ref InstanceType
      NetworkInterfaces:
        - DeviceIndex: 0
          SubnetId: !Ref InstanceSubnet
          GroupSet:
            - !Ref InstanceSecurityGroup
      UserData: !Base64 |
        #!/bin/bash -xe
        dnf update -y
        dnf install -y mariadb105
Code language: YAML (yaml)

This instance is used to access Aurora Serverless v2.

So we install mariadb using the user data function.
Now prepare the client for MySQL.

For more information on the EC2 instance initialization process, please also see the following page.

あわせて読みたい
Four ways to initialize Linux instance 【Four ways to initialize a Linux instance】 Consider how to perform the initialization process when an EC2 instance is started. We will cover the following ...

Architecting

Use CloudFormation to build this environment and check its actual behavior.

Create CloudFormation stacks and check the resources in the stacks

Create CloudFormation stacks.
For information on how to create stacks and check each stack, please see the following page.

あわせて読みたい
CloudFormation’s nested stack 【How to build an environment with a nested CloudFormation stack】 Examine nested stacks in CloudFormation. CloudFormation allows you to nest stacks. Nested ...

After reviewing the resources in each stack, information on the main resources created in this case is as follows

  • EC2 instance: i-0ea84706be1475b7f
  • Aurora cluster: fa-138-dbcluster
  • Aurora cluster endpoint: fa-138-dbcluster.cluster-cl50iikpthxs.ap-northeast-1.rds.amazonaws.com

Check the Aurora cluster from the AWS Management Console.

Detail of Aurora 1.

The Aurora cluster has been successfully created.
And one DB instance is created in the cluster.

Detail of Aurora 2.

Looking at the DB instance type, we see that it was indeed created as Aurora Serverless v2.
The capacity information shows that the ACU is set between 0.5 and 1.0.

Operation Check

Now that you are ready, access the EC2 instance.
The SSM Session Manager is used to access the instance.

% aws ssm start-session --target i-0ea84706be1475b7f
...
sh-5.2$
Code language: Bash (bash)

For more information on SSM Session Manager, please refer to the following page.

あわせて読みたい
Accessing Linux instance via SSM Session Manager 【Configure Linux instances to be accessed via SSM Session Manager】 We will check a configuration in which an EC2 instance is accessed via SSM Session Manag...

Next, check the installation status of the MySQL client.

sh-5.2$ dnf list installed | grep mariadb
mariadb-connector-c.aarch64            3.1.13-1.amzn2023.0.3              @amazonlinux
mariadb-connector-c-config.noarch      3.1.13-1.amzn2023.0.3              @amazonlinux
mariadb105.aarch64                     3:10.5.18-1.amzn2023.0.1           @amazonlinux
mariadb105-common.aarch64              3:10.5.18-1.amzn2023.0.1           @amazonlinux
Code language: Bash (bash)

You can see that MariaDB is indeed installed.

Check the version.

sh-5.2$ mariadb -V
mariadb  Ver 15.1 Distrib 10.5.18-MariaDB, for Linux (aarch64) using  EditLine wrapper
Code language: Bash (bash)

You will see that the latest version of the client is installed.

Now that the client installation has been verified, connect to Aurora Serverless v2.
Specify the Aurora cluster endpoint and pass the port number, username and password as arguments.

sh-5.2$ mariadb -h fa-138-dbcluster.cluster-cl50iikpthxs.ap-northeast-1.rds.amazonaws.com -P 3306 -u testuser -p testdb
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 97
Server version: 8.0.26 Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [testdb]>
Code language: Bash (bash)

We were able to connect successfully.
Aurora Serverless v2 was successfully created.

Summary

Aurora Serverless v2 was created with CloudFormation.

TOC