/
cfn-32-rds.yml
269 lines (242 loc) · 8.89 KB
/
cfn-32-rds.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
Description:
This template creates an Aurora(MySQL) Cluster and instance(s) into the VPC created beforehand by the "cfn-01-basis.yml" template.
You cannot create any type of RDSs except Aurora(MySQL).
This template depends on the following export names.
"${EnvName}-VPC"
"${EnvName}-DatastoreSubnet1"
"${EnvName}-DatastoreSubnet2"
"${EnvName}-SecurityGroupInternal"
"${EnvName}-Route53HostedZoneName"
# ------------------------------------------------------------
# Parameters section
# ------------------------------------------------------------
Parameters:
EnvName:
Description: Please select the environment you want to create. Besides, its name will be prefixed to all resources' name.
Type: String
Default: dev1
AppServiceName:
Description: Please enter the service name will be provided to the customer.
Type: String
Default: YourApp
RevisionNumber:
Description: The revision number of this template consists of 3 parts.
Type: String
Default: 0.1.0
DBMasterUserName:
Description: Please enter the name of the master user on an RDS.
Type: String
Default: master
DBMasterUserPassword:
Description: Please enter the password of the master user on an RDS. That should be greater or equal to 8 characters.
Type: String
NoEcho: true
DBSnapshotArn:
Description: If you want to create an Aurora Cluster from your snapshot, please enter your Snapshot ARN. If you leave it empty, this template creates a brand new Aurora Cluster.
Type: String
EnableMultiAz:
Description: Decide whether the Aurora Cluster has a single instance or 2 instances.
Type: String
Default: false
AllowedValues:
- true
- false
Mappings:
RDS:
dev1:
ClusterName: dev1-yourapp-cluster
Instance1Name: dev1-yourapp-instance1
Instance2Name: dev1-yourapp-instance2
InstanceType: db.t2.small
BackupRetentionPeriod: 3
PreferredBackupWindow: "18:00-19:00" # in UTC
PreferredMaintenanceWindow: "sun:19:30-sun:20:00" # in UTC
dev2:
ClusterName: dev2-yourapp-cluster
Instance1Name: dev2-yourapp-instance1
Instance2Name: dev2-yourapp-instance2
InstanceType: db.t2.small
BackupRetentionPeriod: 3
PreferredBackupWindow: "18:00-19:00" # in UTC
PreferredMaintenanceWindow: "sun:19:30-sun:20:00" # in UTC
prd:
ClusterName: prd-yourapp-cluster
Instance1Name: prd-yourapp-instance1
Instance2Name: prd-yourapp-instance2
InstanceType: db.t2.medium
BackupRetentionPeriod: 15
PreferredBackupWindow: "18:00-19:00" # in UTC
PreferredMaintenanceWindow: "sun:19:30-sun:20:00" # in UTC
# Route53:
# dev1:
# HostedZoneName: dev1.your-app-example.com.
# dev2:
# HostedZoneName: dev2.your-app-example.com.
# prd:
# HostedZoneName: your-app-example.com.
Conditions:
isBrandNewDB: !Equals [ !Ref DBSnapshotArn, "" ]
enableMultiAz: !Equals [ !Ref EnableMultiAz, "true" ]
# ------------------------------------------------------------
# Resources section
# ------------------------------------------------------------
Resources:
#
# define Parameter Groups
#
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-rds-dbparametergroup.html
DBParameterGroup:
Type: "AWS::RDS::DBParameterGroup"
Properties:
Description: "RDS DB parameter group the Aurora Cluster's instance(s)."
Family: "aurora-mysql5.7"
Parameters:
max_connections: "1000"
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-rds-dbclusterparametergroup.html
DBClusterParameterGroup:
Type: "AWS::RDS::DBClusterParameterGroup"
Properties:
Description: "RDS DB cluster parameter group for Hub-Amber"
Family: "aurora-mysql5.7"
Parameters:
character_set_client: "utf8mb4"
character_set_connection: "utf8mb4"
character_set_database: "utf8mb4"
character_set_filesystem: "utf8mb4"
character_set_results: "utf8mb4"
character_set_server: "utf8mb4"
collation_connection: "utf8mb4_bin"
collation_server: "utf8mb4_bin"
time_zone: "UTC"
#
# define RDS
#
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-rds-dbsubnet-group.html
DBSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupDescription: RDS subnet group.
SubnetIds:
- Fn::ImportValue: !Sub ${EnvName}-DatastoreSubnet1
- Fn::ImportValue: !Sub ${EnvName}-DatastoreSubnet2
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-rds-dbcluster.html
DBCluster:
Type: AWS::RDS::DBCluster
DeletionPolicy: Snapshot
Properties:
Engine: aurora-mysql
EngineVersion: 5.7.12
SnapshotIdentifier:
!If [isBrandNewDB, !Ref "AWS::NoValue", !Ref "DBSnapshotArn" ]
DBClusterIdentifier: !FindInMap [RDS, !Ref "EnvName", "ClusterName"]
MasterUsername:
!If [isBrandNewDB, !Ref "DBMasterUserName", !Ref "AWS::NoValue" ]
MasterUserPassword:
!If [isBrandNewDB, !Ref "DBMasterUserPassword", !Ref "AWS::NoValue" ]
BackupRetentionPeriod: !FindInMap [RDS, !Ref "EnvName", "BackupRetentionPeriod"]
PreferredBackupWindow: !FindInMap [RDS, !Ref "EnvName", "PreferredBackupWindow"]
PreferredMaintenanceWindow: !FindInMap [RDS, !Ref "EnvName", "PreferredMaintenanceWindow"]
DBSubnetGroupName: !Ref "DBSubnetGroup"
DBClusterParameterGroupName: !Ref "DBClusterParameterGroup"
VpcSecurityGroupIds:
- Fn::ImportValue: !Sub ${EnvName}-SecurityGroupInternal
Tags:
- Key: Name
Value: !FindInMap [RDS, !Ref "EnvName", "ClusterName"]
- Key: Env
Value: !Ref EnvName
- Key: Service
Value: !Ref AppServiceName
- Key: Role
Value: Datastore
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-rds-database-instance.html
DBInstance1:
Type: AWS::RDS::DBInstance
Properties:
Engine: aurora-mysql
DBClusterIdentifier: !Ref "DBCluster"
DBInstanceClass: !FindInMap [RDS, !Ref "EnvName", "InstanceType"]
DBSubnetGroupName: !Ref "DBSubnetGroup"
DBParameterGroupName: !Ref "DBParameterGroup"
Tags:
- Key: Name
Value: !FindInMap [RDS, !Ref "EnvName", "Instance1Name"]
- Key: Env
Value: !Ref EnvName
- Key: Service
Value: !Ref AppServiceName
- Key: Role
Value: Datastore
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-rds-database-instance.html
DBInstance2:
Type: AWS::RDS::DBInstance
Condition: enableMultiAz
Properties:
Engine: aurora-mysql
DBClusterIdentifier: !Ref "DBCluster"
DBInstanceClass: !FindInMap [RDS, !Ref "EnvName", "InstanceType"]
DBSubnetGroupName: !Ref "DBSubnetGroup"
Tags:
- Key: Name
Value: !FindInMap [RDS, !Ref "EnvName", "Instance2Name"]
- Key: Env
Value: !Ref EnvName
- Key: Service
Value: !Ref AppServiceName
- Key: Role
Value: Datastore
#
# define Route 53 records
#
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-recordset.html
RdsWriterDnsRecord:
Type: "AWS::Route53::RecordSet"
Properties:
HostedZoneName:
Fn::ImportValue:
!Sub "${EnvName}-Route53HostedZoneName"
Name:
Fn::Join:
- ""
- - "rds-writer."
- Fn::ImportValue: !Sub "${EnvName}-Route53HostedZoneName"
Type: CNAME
ResourceRecords:
- !GetAtt DBCluster.Endpoint.Address
TTL: "60"
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-recordset.html
RdsReaderDnsRecord:
Type: "AWS::Route53::RecordSet"
Properties:
HostedZoneName:
Fn::ImportValue:
!Sub "${EnvName}-Route53HostedZoneName"
Name:
# Fn::Join:
# - ""
# - - "rds-reader."
# - Fn::ImportValue: !Sub "${EnvName}-Route53HostedZoneName"
!Join ["", ["rds-reader.", Fn::ImportValue !Sub "${EnvName}-Route53HostedZoneName"]]
Type: CNAME
ResourceRecords:
- !GetAtt DBCluster.ReadEndpoint.Address
TTL: "60"
# ------------------------------------------------------------
# Outputs section
# ------------------------------------------------------------
Outputs:
DBCluster:
Description: An Aurora(MySQL) Cluster
Value: !Ref DBCluster
Export:
Name: !Sub ${EnvName}-DBCluster
RdsWriterDnsRecord:
Description: A DNS record of the RDS writer Endpoint.
Value: !Ref RdsWriterDnsRecord
Export:
Name: !Sub ${EnvName}-RdsWriterDnsRecord
RdsReaderDnsRecord:
Description: A DNS record of the RDS reader Endpoint.
Value: !Ref RdsReaderDnsRecord
Export:
Name: !Sub ${EnvName}-RdsReaderDnsRecord