Use CloudFormation to set up an AWS RDS MySQL database instance

You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose

This article can help users create MYSQL database instances using CloudFormation automation services. RDS stands for Relational Database Service. It is a managed AWS (Amazon Web Services) service that simplifies the setup and management of relational databases.

RDS supports various database engines. They include:

  • MySQL database
  • Amazon Aurora
  • PostgreSQL
  • Maria DB
  • Oracle
  • SQL server

The benefits of using AWS RDS include:

RDS makes it easier to configure and manage the RDS database. No need to worry about software patches, and no need to go through the complicated process of configuring an instance and installing software on the instance.

If needed, RDS makes it easier to expand the database (read-only copy). In addition, the service also provides users with the option to ensure high availability settings (multi-az).

Requirements/prerequisites

Before starting the installation, please review the installation prerequisites listed below.

Users will need to have:

  • An AWS account.
  • Created a user authorized to create resources on the AWS account.
  • An IDE similar to visual studio code can write and edit CloudFormation templates.
  • Create a VPC with a subnet and Internet connection.
  • Created a parameter group for the database instance.

N/B: For RDS MYSQL database instances, we cannot use CloudFormation templates to create parameter groups. It must be created in advance and used as an input variable (parameter on the template).

Step 1: Create a database instance parameter group

Through the parameter group, you can manage the database engine configuration. To manually create the RDS database parameter group, follow the steps below.

On the AWS RDS console, select the parameter group, and then click Create parameter group.

Create parameter group

Next, enter the parameter group details. For our case, we are creating a MySQL version 8 db instance, so we filled in the details as shown below.

Use CloudFormation to set up an AWS RDS MySQL database instanceEnter parameter group details

When finished, click Create. It will create the database parameter group.

Step 2: Create CloudFormation template

Use the template below to create your RDS MYSQL database instance.

AWSTemplateFormatVersion: "2010-09-09"
Description: "Create a DB subnet group and MYSQL Database"

Parameters:
  VPC:
    Type: String
    Description: The VPC to create the cluster
    Default: vpc-ID

  PrivateSubnet01:
    Type: String
    Description: The subnet for the DB cluster
    Default: subnet-ID

  PrivateSubnet02:
    Type: String
    Description: The subnet for the DB cluster
    Default: subnet-ID

  MasterUsername:
    Type: String
    Description: The username for our database.
  
  MasterUserPassword:
    Type: String
    Description: The password for the database.
    "NoEcho": true

  ParameterGroup:
    Type: String
    Description: The name of the database parameter group created.

Resources:
    EC2SecurityGroup:
        Type: "AWS::EC2::SecurityGroup"
        Properties:
            GroupDescription: "Database instances security group"
            VpcId: !Ref VPC
            SecurityGroupIngress: 
              - 
                CidrIp: "*.*.*.*/32"
                FromPort: 3306
                IpProtocol: "tcp"
                ToPort: 3306
            SecurityGroupEgress: 
              - 
                CidrIp: "0.0.0.0/0"
                IpProtocol: "-1"

    RDSDBSubnetGroup:
        Type: "AWS::RDS::DBSubnetGroup"
        Properties:
            DBSubnetGroupDescription: "Subnet Group for mySQL database"
            DBSubnetGroupName: !Sub "${AWS::Region}-aws-dxl-database-subnet-group"
            SubnetIds: 
              - !Ref PrivateSubnet01
              - !Ref PrivateSubnet02
            Tags: 
              - Key: Name
                Value: eu-central-1-test-db-cluster
              - Key: createdBy
                Value: Maureen Barasa
              - Key: Project
                Value: test-blog
              - Key: Environment
                Value: test

    RDSDBInstance:
        Type: AWS::RDS::DBInstance
        Properties:
            DBInstanceIdentifier: aws-dxl-database-1
            AllocatedStorage: 100
            DBInstanceClass: db.m5.large
            Engine: "MYSQL"
            MasterUsername: !Ref MasterUsername
            MasterUserPassword: !Ref MasterUserPassword
            BackupRetentionPeriod: 7
            MultiAZ: true
            EngineVersion: 8.0.20
            AutoMinorVersionUpgrade: true
            Iops: 1000
            PubliclyAccessible: false
            StorageType: io1
            Port: 3306
            StorageEncrypted: true
            CopyTagsToSnapshot: true
            MonitoringInterval: 60
            EnableIAMDatabaseAuthentication: false
            EnablePerformanceInsights: true
            PerformanceInsightsRetentionPeriod: 7
            DeletionProtection: true
            DBSubnetGroupName: !Ref RDSDBSubnetGroup
            VPCSecurityGroups: 
              - !Ref EC2SecurityGroup
            MaxAllocatedStorage: 1000
            DBParameterGroupName: !Ref ParameterGroup
            MonitoringRoleArn: !Sub "arn:aws:iam::${AWS::AccountId}:role/rds-monitoring-role"
            Tags: 
              - Key: Name
                Value: aws-dxl-database-1
              - Key: createdBy
                Value: Maureen Barasa
              - Key: Project
                Value: test-blog
              - Key: Environment
                Value: test

Outputs:
   Cluster:
    Description: The DB Cluster Name
    Value: !Ref RDSDBInstance
    
   SubnetGroup:
    Description: The db subnet group name 
    Value: !Ref RDSDBSubnetGroup

We can deploy CloudFormation templates using the CloudFormation stack.

Use CloudFormation to set up an AWS RDS MySQL database instanceDeploy CloudFormation template

Resources: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-whatis-howdoesitwork.html

Introduction to CloudFormation templates

The template consists of 3 parts. Parameters, resources and output parts.

parameter:

In the resource section, we ask users to enter the dynamic variables of their template. In our case, users should replace the VPC and subnet ID with their respective VPC and subnet ID. Next, it will prompt the user to enter their database master username and password. Finally, the user will be asked to enter the name of the parameter group created earlier.

Resources:

The user defines the AWS resources to be created here. For our case, we first create a database instance security group. Users should change the security group entry to reflect the CIDR IP block they wish to allow access to the database instance.

Next, it creates the database subnet group. The subnet group defines the subnet in which the database cluster and instance are created. In addition, users should pay attention to the name and label to customize as needed.

Finally, create a database instance. However, users should browse the template and change the instance attributes to match their specific needs. In addition, the database instance identifier and label should be customized to meet user requirements.

Output:

The output part of the template indicates that CloudFormation outputs the name of the resource created. For example, in our case, we instruct the template to output the names of the database instance and subnet group.

Similar guides:

Use CloudFormation to create an Amazon DocumentDB (MongoDB) database on AWS

Use CloudFormation to create and configure AWS Application Load Balancer

How to create AWS Network Architecture using CloudFormation

Happy Building! ! !

You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose

Sidebar