AWS

AWS - Create Aurora Cluster using Ruby SDK


Below is the script which can used to create Aurora Cluster using Ruby SDK


ec2-user@ip-172-31-46-131 .aws]$ cat create.rb
require 'aws-sdk-rds'  # v2: require 'aws-sdk'

rds = Aws::RDS::Client.new(region: 'us-east-1')
     
dbcluster = rds.create_db_cluster({
db_cluster_identifier: "apg-cluster3",
vpc_security_group_ids: ["sg-52630c23"],
db_subnet_group_name: "newsubnetgroup",
engine: "aurora-postgresql",
master_username: "master",
master_user_password: "password",
})

Below is the command which is used to run the ruby script.


[ec2-user@ip-172-31-46-131 .aws]$ ruby reate.rb




AWS - Create Aurora PostgreSQL Cluster and 2 Instance ( Random Identifier) Using CloudFormation


Below is the CloudFormation Template for spinning up Aurora Cluster and 2 instance(One write and one reader node) along with Random Cluster and node identifier name -


AWSTemplateFormatVersion: 2010-09-09
Description: A basic CloudFormation template for an RDS Aurora PostgreSQL cluster.
Parameters:
    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: password
        AllowedPattern: "[a-zA-Z0-9]+"
        ConstraintDescription: must contain only alphanumeric characters.
        Description: The database admin account password.
        MaxLength: '41'
        MinLength: '8'
        NoEcho: 'true'
        Type: String
    DatabaseUsername:
        Default: aurorapg
        AllowedPattern: "[a-zA-Z0-9]+"
        ConstraintDescription: must contain only alphanumeric characters.
        Description: The database admin account user name.
        MaxLength: '16'
        MinLength: '1'
        Type: String
    DatabaseClusterParameterGroupName:
        Type: String
        Default: "default.aurora-postgresql9.6"
    DatabaseBackupRetentionPeriod:
        Type: String
        Default: 35
        Description: The database backup retention period in days.
    DatabaseSubnets:
        Default: "subnet-6749a72f,subnet-c3033698"
        Description: The subnets to place database instances in.
        Type: List<AWS::EC2::Subnet::Id>
    DatabaseSecurityGroups:
        Default: sg-0ee3d27f
        Type: List<AWS::EC2::SecurityGroup::Id>
        Description: Security groups to apply to the RDS cluster.
Metadata:
    AWS::CloudFormation::Interface:
        ParameterGroups:
            - Label:
                default: default.aurora-postgresql9.6
              Parameters:
                - DatabaseInstanceType
                - DatabaseName
                - DatabaseUsername
                - DatabasePassword
                - DatabaseSubnets
                - DatabaseSecurityGroups
                - DatabaseBackupRetentionPeriod
                - DatabaseClusterParameterGroupName
        ParameterLabels:
            DatabaseInstanceType:
                default: Database Instance Type
            DatabaseClusterParameterGroupName:
                default: DB cluster parameter group
            DatabasePassword:
                default: Database Password
            DatabaseUsername:
                default: Database Username
            DatabaseBackupRetentionPeriod:
                default: Database Backup Retention Period
            DatabaseSubnets:
                default: Database Subnets
            DatabaseSecurityGroups:
                default: Database Security Groups
Resources:
    DatabaseSubnetGroup:
        Type: AWS::RDS::DBSubnetGroup
        Properties:
            DBSubnetGroupDescription: CloudFormation managed DB subnet group.
            SubnetIds:
                Ref: DatabaseSubnets
    DatabaseCluster:
        Type: AWS::RDS::DBCluster
        Properties:
            Engine: aurora-postgresql
            MasterUsername:
                Ref: DatabaseUsername
            MasterUserPassword:
                Ref: DatabasePassword
            BackupRetentionPeriod:
                Ref: DatabaseBackupRetentionPeriod
            DBClusterParameterGroupName:
                Ref: DatabaseClusterParameterGroupName
            PreferredBackupWindow: 02:00-03:00
            PreferredMaintenanceWindow: mon:03:00-mon:04:00
            DBSubnetGroupName:
                Ref: DatabaseSubnetGroup
            VpcSecurityGroupIds:
                Ref: DatabaseSecurityGroups
    DatabasePrimaryInstance:
        Type: AWS::RDS::DBInstance
        Properties:
            Engine: aurora-postgresql
            DBClusterIdentifier:
                Ref: DatabaseCluster
            DBInstanceClass:
                Ref: DatabaseInstanceType
            DBSubnetGroupName:
                Ref: DatabaseSubnetGroup
    DatabaseReplicaInstance:
        Type: AWS::RDS::DBInstance
        Properties:
            Engine: aurora-postgresql
            DBClusterIdentifier:
                Ref: DatabaseCluster
            DBInstanceClass:
                Ref: DatabaseInstanceType
            DBSubnetGroupName:
                Ref: DatabaseSubnetGroup


AWS - Launch Aurora PostgreSQL using CloudFormation With Custom Instance Identifier


Below is the CloudFormation Template for spinning up Aurora PostgreSQL Cluster and 2 instance( Read and Write Node) with custom Instance Identifier using CloudFormation With Custom Instance Identifier:



AWSTemplateFormatVersion: 2010-09-09
Description: A basic CloudFormation template for an RDS Aurora PostgreSQL cluster.
Parameters:
    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: password
        AllowedPattern: "[a-zA-Z0-9]+"
        ConstraintDescription: must contain only alphanumeric characters.
        Description: The database admin account password.
        MaxLength: '41'
        MinLength: '8'
        NoEcho: 'true'
        Type: String
    DatabaseUsername:
        Default: aurorapg
        AllowedPattern: "[a-zA-Z0-9]+"
        ConstraintDescription: must contain only alphanumeric characters.
        Description: The database admin account user name.
        MaxLength: '16'
        MinLength: '1'
        Type: String
    DatabaseClusterParameterGroupName:
        Type: String
        Default: "default.aurora-postgresql9.6"
    DatabaseBackupRetentionPeriod:
        Type: String
        Default: 35
        Description: The database backup retention period in days.
    DatabaseSubnets:
        Default: "subnet-6749a72f,subnet-c3033698"
        Description: The subnets to place database instances in.
        Type: List<AWS::EC2::Subnet::Id>
    DatabaseSecurityGroups:
        Default: "sg-0ee3d27f"
        Type: List<AWS::EC2::SecurityGroup::Id>
        Description: Security groups to apply to the RDS cluster.
    DBIdentifierNameMaster:
        Default: "aur1"
        Description: "The string that will be prefixed to each instance name"
        Type: "String"
        MinLength: "3"
        MaxLength: "10"
        AllowedPattern: "[a-z0-9]*"
        ConstraintDescription: "Identifier names must be 3-6 characters and contain only a-z and 0-9."
    DBIdentifierNameReplica:
        Default: "aur2rr"
        Description: "The string that will be prefixed to each instance name"
        Type: "String"
        MinLength: "3"
        MaxLength: "10"
        AllowedPattern: "[a-z0-9]*"
        ConstraintDescription: "Identifier names must be 3-10 characters and contain only a-z and 0-9."
Metadata:
    AWS::CloudFormation::Interface:
        ParameterGroups:
            - Label:
                default: default.aurora-postgresql9.6
              Parameters:
                - DatabaseInstanceType
                - DatabaseName
                - DatabaseUsername
                - DatabasePassword
                - DatabaseSubnets
                - DatabaseSecurityGroups
                - DatabaseBackupRetentionPeriod
                - DatabaseClusterParameterGroupName
        ParameterLabels:
            DatabaseInstanceType:
                default: Database Instance Type
            DatabaseClusterParameterGroupName:
                default: DB cluster parameter group
            DatabasePassword:
                default: Database Password
            DatabaseUsername:
                default: Database Username
            DatabaseBackupRetentionPeriod:
                default: Database Backup Retention Period
            DatabaseSubnets:
                default: Database Subnets
            DatabaseSecurityGroups:
                default: Database Security Groups
Resources:
    DatabaseSubnetGroup:
        Type: AWS::RDS::DBSubnetGroup
        Properties:
            DBSubnetGroupDescription: CloudFormation managed DB subnet group.
            SubnetIds:
                Ref: DatabaseSubnets
    DatabaseCluster:
        Type: AWS::RDS::DBCluster
        Properties:
            Engine: aurora-postgresql
            MasterUsername:
                Ref: DatabaseUsername
            MasterUserPassword:
                Ref: DatabasePassword
            BackupRetentionPeriod:
                Ref: DatabaseBackupRetentionPeriod
            DBClusterParameterGroupName:
                Ref: DatabaseClusterParameterGroupName
            PreferredBackupWindow: 02:00-03:00
            PreferredMaintenanceWindow: mon:03:00-mon:04:00
            DBSubnetGroupName:
                Ref: DatabaseSubnetGroup
            VpcSecurityGroupIds:
                Ref: DatabaseSecurityGroups
    DatabasePrimaryInstance:
        Type: AWS::RDS::DBInstance
        Properties:
            Engine: aurora-postgresql
            DBClusterIdentifier:
                Ref: DatabaseCluster
            DBInstanceClass:
                Ref: DatabaseInstanceType
            DBSubnetGroupName:
                Ref: DatabaseSubnetGroup
            DBInstanceIdentifier:
                Ref: DBIdentifierNameMaster
    DatabaseReplicaInstance:
        Type: AWS::RDS::DBInstance
        Properties:
            Engine: aurora-postgresql
            DBClusterIdentifier:
                Ref: DatabaseCluster
            DBInstanceClass:
                Ref: DatabaseInstanceType
            DBSubnetGroupName:
                Ref: DatabaseSubnetGroup
            DBInstanceIdentifier:
                Ref: DBIdentifierNameReplica

4 comments: