-->

CloudFormation templates for Global Aurora Databas

2020-08-04 04:44发布

问题:

I am trying to write Cloudformation template to get a aws Global Aurora Database. However I am not able to figure out where and how to add the Global database identifier. Can someone help Cloudformation snippet?

below is my code:

Description: RDS Aurora MySQL cluster.
Parameters:
    DatabaseName:
      Default: "testglobalaurora"
      Description: The database name 
      Type: String

    DatabaseInstanceType:
        Default: db.r4.large
        AllowedValues:
            - db.r4.large
            - db.r4.xlarge
            - db.r4.2xlarge
            - db.r4.4xlarge
            - db.r4.8xlarge
            - db.r4.16xlarge
        Description: "The instance type to use for the database."
        Type: String

    DatabasePassword:
        Default: "testglobalaurora"
        AllowedPattern: "[a-zA-Z0-9]+"
        ConstraintDescription: must contain only alphanumeric characters. Must have length 8-41.
        Description: The database admin account password. 
        MaxLength: '41'
        MinLength: '8'
        NoEcho: 'true'
        Type: String

    DatabaseUsername:
        Default: "testglobalaurora"
        AllowedPattern: "[a-zA-Z0-9]+"
        ConstraintDescription: must contain only alphanumeric characters. Must have length 1-16
        Description: The database admin account user name. 
        MaxLength: '16'
        MinLength: '1'
        Type: String

Metadata:
    AWS::CloudFormation::Interface:
        ParameterGroups:
            - Label:
                default: Database Configuration
              Parameters:
                - DatabaseInstanceType
                - DatabaseName
                - DatabaseUsername
                - DatabasePassword
        ParameterLabels:
            DatabaseName:
              default: Database name
            DatabaseInstanceType:
                default: Database Instance Type
            DatabasePassword:
                default: Database Password
            DatabaseUsername:
                default: Database Username

Resources:
    ParameterGroup:
        Type: "AWS::RDS::DBParameterGroup"
        Properties: 
            Description: testglobalaurora DB parameter group 
            Family: aurora5.6
            Parameters:
                max_connections: 300

    DatabaseCluster:
        Type: AWS::RDS::DBCluster
        Properties:
            Engine: aurora
            EngineMode: global

            MasterUsername:
              Ref: DatabaseUsername
            MasterUserPassword:
              Ref: DatabasePassword
            BackupRetentionPeriod: 35
            PreferredBackupWindow: 02:00-03:00
            PreferredMaintenanceWindow: mon:03:00-mon:04:00
            VpcSecurityGroupIds:
              - Ref: DatabaseSecurityGroup

    DatabaseInstance:
        Type: AWS::RDS::DBInstance
        Properties:
            Engine: aurora
            EngineVersion : 5.6.10a
            DBClusterIdentifier:
                Ref: DatabaseCluster
            DBInstanceClass:
                Ref: DatabaseInstanceType 
            DBParameterGroupName: !Ref ParameterGroup
            PubliclyAccessible: "true"
            DBInstanceIdentifier: !Ref DatabaseName

    DatabaseSecurityGroup:
        Type: AWS::EC2::SecurityGroup
        Properties: 
            VpcId: vpc-55378f2f
            GroupDescription: Access to database
            SecurityGroupIngress:
                - CidrIp: 0.0.0.0/0
                  FromPort: 3306
                  ToPort: 3306
                  IpProtocol: tcp
            Tags: 
                - Key: Name
                  Value: !Sub ${DatabaseName}-security-group

Outputs:
    DatabaseEndpoint: 
        Description: The database endpoint
        Value: !GetAtt DatabaseCluster.Endpoint.Address

    DatabasePort:
        Description: The database port
        Value: !GetAtt DatabaseCluster.Endpoint.Port

My output "
global-database-1-cluster-1 Regional Aurora MySQL 5.6.10a
global-database-1-instance-1 Writer Aurora MySQL 5.6.10a "

Actual ouput "
test-it Global Aurora MySQL 5.6.10a global-database-1-cluster-1 Primary Aurora MySQL 5.6.10a
global-database-1-instance-1 Writer Aurora MySQL 5.6.10a "

回答1:

I recently ran across the need to create a global RDS with Cloudformation. Here is a minimal Cloudformation that got me started.

AWSTemplateFormatVersion: "2010-09-09"
Description: Global RDS database stack
Parameters:
  DatabaseInstanceType:
    Default: db.r4.large
    AllowedValues:
      - db.r4.large
      - db.r4. # add the other r4 instances
    Description: "The instance type to use for the database."
    Type: String
  DatabasePassword:
    Default: SomePassword1
    AllowedPattern: "[a-zA-Z0-9]+"
    ConstraintDescription: must contain only alphanumeric characters. Must have length 8-41.
    Description: The database admin account password.
    MaxLength: '41'
    MinLength: '8'
    NoEcho: 'true'
    Type: String
  DatabaseUsername:
    Default: globaladmin
    ConstraintDescription: must contain only alphanumeric characters. Must have length 1-16
    Description: The database admin account user name.
    MaxLength: '16'
    MinLength: '1'
    Type: String
Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default: Database Configuration
        Parameters:
          - DatabaseInstanceType
          - DatabaseName
          - DatabaseUsername
          - DatabasePassword
    ParameterLabels:
      DatabaseName:
        default: Database name
      DatabaseInstanceType:
        default: Database Instance Type
      DatabasePassword:
        default: Database Password
      DatabaseUsername:
        default: Database Username
Resources:
  GlobalDbCluster:
    Type: AWS::RDS::DBCluster
    Properties:
      Engine: aurora
      EngineMode: global
      EngineVersion: 5.6.10a
      MasterUsername: !Ref DatabaseUsername
      MasterUserPassword: !Ref DatabasePassword
      DBClusterParameterGroupName: !Ref GlobalDbParamGroup
  GlobalDbParamGroup:
    Type: AWS::RDS::DBClusterParameterGroup
    Properties:
      Description: "parameter group for the global database"
      Family: aurora5.6
      Parameters:
        character_set_database: utf32
  InstanceOne:
    Type: AWS::RDS::DBInstance
    Properties:
      DBInstanceClass: !Ref DatabaseInstanceType
      DBClusterIdentifier: !Ref GlobalDbCluster
      Engine: aurora
  InstanceTwo:
    Type: AWS::RDS::DBInstance
    Properties:
      DBInstanceClass: !Ref DatabaseInstanceType
      DBClusterIdentifier: !Ref GlobalDbCluster
      Engine: aurora


回答2:

You need to create a Global Cluster with an identifier, and use that identifier in your DB Cluster. That portion is missing in your CFN template.

Something like:

GlobalCluster:
        Type: AWS::RDS::GlobalCluster
        Properties:
            Engine: aurora
            EngineVersion: 5.6.10a
            Region: us-east-1

and then use it in your DatabaseCluster properties using GlobalClusterIdentifier: <id>

However, looking at the official docs for the CFN types [1] for RDS, it does not list GlobalCluster. So either its just not documented or this Resource type has not been registered with Cloudformation. If the latter is the case, then you may want to open a support case and put in a feature request.

[1] https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/AWS_RDS.html