使用CloudFormation设置AWS RDS MySQL数据库实例
本文有助于用户使用CloudFormation自动化服务创建MySQL数据库实例。
RDS代表关系数据库服务。
它是一个托管的AWS(亚马逊Web服务)服务,简化了关系数据库的设置和管理。
RDS支持各种数据库引擎。
它们包括:mysqlamazon aurorapostgresqlmaria dboraclesql服务器
使用AWS RD的好处包括:
RDS可以更轻松地配置和管理RDS数据库。
无需担心软件修补程序,也不需要通过配置实例的复杂过程并在实例上安装软件。
RDS使我们的数据库更容易扩展(读取副本)。
此外,该服务为用户提供了选项以确保高可用的设置(多AZ)。
要求/准备工作
在开始之前,安装程序将看一下下面列出的安装前要求。
用户需要提供:AWS帐户。
使用权限创建一个用户,以在AWS帐户上创建资源。
作为Visual Studio代码,如Visual Studio代码编写和编辑CloudFormation模板。
使用子网和Internet连接的VPC。
创建的数据库实例的参数组。
N/B:对于RDS MySQL数据库实例,我们无法使用CloudFormation模板创建参数组。
必须事先创建并用作输入变量(我们的模板上参数)。
第1步:创建数据库实例参数组
参数组允许我们管理数据库引擎配置。
要手动创建RDS数据库参数组,请按照以下步骤操作。
在AWS RDS控制台中选择参数组,然后单击"创建参数"组。
接下来,输入参数组详细信息。
对于我们的案例,我们正在创建一个MySQL版本8 DB实例,因此我们填写了以下详细信息。
完成后单击"创建"。
它将创建数据库参数组。
第2步:创建CloudFormation模板
使用以下模板创建RDS MySQL数据库实例。
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
我们可以使用CloudFormation Stack部署CloudFormation模板。
来源:https://docs.aws.amazon.com/awscloudformation/latest/userguide/cfn-whatis-howdoesitwork.html.
CloudFormation模板解释说明
模板包含3个部分。
参数,资源和输出部分。
参数:
在"资源"部分中,我们要求用户输入其模板的动态变量。
对于我们的情况,用户应使用各自的VPC和子网ID替换VPC和子网ID。
接下来,它将提示用户输入其数据库主用户名和密码。
最后,将需要用户输入前面创建的参数组的名称。
资源:
其中用户定义了要创建的AWS资源。
对于我们的情况,我们首先创建数据库实例安全组。
用户应该更改安全组入口以反映他们希望访问数据库实例的CIDR IP块。
接下来,它会创建DB子网组。
子网组定义了创建数据库群集和实例的子网。
此外,用户应注意根据需要自定义的名称和标记。
最后,创建数据库实例。
但是,用户应该通过模板并更改实例属性以匹配其特定需求。
此外,应自定义数据库实例标识符和标签以满足用户要求。
模板的输出部分指示CloudFormation输出所创建的资源的名称。
例如,在我们的情况下,我们指示模板输出数据库实例和子网组的名称。

