-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
228 lines (193 loc) · 6.32 KB
/
template.yaml
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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Backstage Database
Parameters:
Domain:
Type: String
Description: 'Name of Domain'
System:
Type: String
Description: "Name of System"
Component:
Type: String
Description: "Name of Component"
CodeBranch:
Type: String
Description: "Name of GitHub Branch"
DbFamily:
Type: String
Description: "DB family"
DbEngine:
Type: String
Description: "DB engine"
DbEngineVersion:
Type: String
Description: "DB engine version"
DbUsername:
Type: String
Description: "DB Backstage username"
Default: postgres
DbPort:
Type: String
Description: "DB port"
RdsScalingMinCapacity:
Type: Number
Description: "RDS scaling min capacity"
RdsScalingMaxCapacity:
Type: Number
Description: "RDS scaling max capacity"
RdsScalingMinInstances:
Type: Number
Description: "RDS scaling min instances"
RdsScalingMaxInstances:
Type: Number
Description: "RDS scaling max instances"
VpcId:
Type: AWS::SSM::Parameter::Value<String>
Description: Account VPC ID
VpcSubnets:
Type: AWS::SSM::Parameter::Value<CommaDelimitedList>
Description: Account subnets (private)
ClusterSecurityGroupId:
Type: AWS::SSM::Parameter::Value<String>
Description: ID of cluster security group
Resources:
########
# RDS
########
## Admin Password
RdsDbAdminPassword:
Type: AWS::SecretsManager::Secret
Properties:
Description: Backstage RDS Admin Password
GenerateSecretString:
SecretStringTemplate: !Sub '{"username": "${DbUsername}"}'
GenerateStringKey: 'password'
PasswordLength: 64
ExcludePunctuation: true
## DB Cluster
RdsDbClusterParameterGroup:
Type: AWS::RDS::DBClusterParameterGroup
Properties:
Description: Aurora Postgresql Cluster Parameter Group
Family: !Ref DbFamily
Parameters:
rds.force_ssl: 1
log_connections: true
log_disconnections: true
RdsDbSubnetGroup:
Type: 'AWS::RDS::DBSubnetGroup'
Properties:
DBSubnetGroupDescription: !Sub '${AWS::StackName} Subnet Group'
SubnetIds: !Ref VpcSubnets
RdsSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: !Sub '${AWS::StackName} Security Group'
VpcId: !Ref VpcId
RdsDbCluster:
Type: AWS::RDS::DBCluster
UpdateReplacePolicy: Snapshot
DeletionPolicy: Snapshot
Properties:
Engine: !Ref DbEngine
EngineVersion: !Ref DbEngineVersion
Port: !Ref DbPort
DBClusterParameterGroupName: !Ref RdsDbClusterParameterGroup
DBSubnetGroupName: !Ref RdsDbSubnetGroup
VpcSecurityGroupIds:
- !Ref RdsSecurityGroup
BackupRetentionPeriod: 7
DeletionProtection: false
MasterUsername: !Ref DbUsername
MasterUserPassword: !Join ['', ['{{resolve:secretsmanager:', !Ref RdsDbAdminPassword, ':SecretString:password}}' ]]
UseLatestRestorableTime: true
ServerlessV2ScalingConfiguration:
MaxCapacity: !Ref RdsScalingMaxCapacity
MinCapacity: !Ref RdsScalingMinCapacity
KmsKeyId: alias/aws/rds
StorageEncrypted: true
EnableCloudwatchLogsExports:
- postgresql
PreferredBackupWindow: 03:00-04:00
RdsDbInstance:
Type: AWS::RDS::DBInstance
Properties:
DBInstanceClass: db.serverless
DBClusterIdentifier: !Ref RdsDbCluster
Engine: aurora-postgresql
RdsDbClusterScalableTarget:
Type: AWS::ApplicationAutoScaling::ScalableTarget
Properties:
MaxCapacity: !Ref RdsScalingMaxInstances
MinCapacity: !Ref RdsScalingMinInstances
RoleARN: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:role/aws-service-role/rds.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_RDSCluster'
ServiceNamespace: rds
ScalableDimension: rds:cluster:ReadReplicaCount
ResourceId: !Sub "cluster:${RdsDbCluster}"
RdsDbClusterScalingPolicy:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
Properties:
PolicyName: !Sub "${RdsDbCluster} Scaling"
PolicyType: TargetTrackingScaling
ScalingTargetId: !Ref RdsDbClusterScalableTarget
TargetTrackingScalingPolicyConfiguration:
TargetValue: 80
PredefinedMetricSpecification:
PredefinedMetricType: RDSReaderAverageCPUUtilization
ScaleInCooldown: 600
ScaleOutCooldown: 300
## ECS Network Access
RdsSecurityGroupIngressContainers:
Type: AWS::EC2::SecurityGroupIngress
Properties:
Description: Container to RDS ingress
GroupId: !Ref RdsSecurityGroup
IpProtocol: tcp
FromPort: !Ref DbPort
ToPort: !Ref DbPort
SourceSecurityGroupId: !Ref ClusterSecurityGroupId
# SSM Values
RdsDbClusterNameSsmParam:
Type: AWS::SSM::Parameter
Properties:
Type: String
Description: Name of RDS cluster
Name: !Sub /${Domain}/${System}/${Component}/${CodeBranch}/RdsDbClusterName
Value: !Ref RdsDbCluster
RdsDbClusterHostSsmParam:
Type: AWS::SSM::Parameter
Properties:
Type: String
Description: Hostname of R/W DB endpoint
Name: !Sub /${Domain}/${System}/${Component}/${CodeBranch}/RdsDbClusterHost
Value: !GetAtt RdsDbCluster.Endpoint.Address
RdsDbClusterROHostSsmParam:
Type: AWS::SSM::Parameter
Properties:
Type: String
Description: DB endpoint port
Name: !Sub /${Domain}/${System}/${Component}/${CodeBranch}/RdsDbClusterROHost
Value: !GetAtt RdsDbCluster.ReadEndpoint.Address
RdsDbClusterPortSsmParam:
Type: AWS::SSM::Parameter
Properties:
Type: String
Description: Hostname of read-only DB endpoint
Name: !Sub /${Domain}/${System}/${Component}/${CodeBranch}/RdsDbClusterPort
Value: !GetAtt RdsDbCluster.Endpoint.Port
RdsDbAdminUsernameSsmParam:
Type: AWS::SSM::Parameter
Properties:
Type: String
Description: Backstage RDS Admin Password
Name: !Sub /${Domain}/${System}/${Component}/${CodeBranch}/RdsDbAdminUsername
Value: !Ref DbUsername
RdsDbAdminPasswordSsmParam:
Type: AWS::SSM::Parameter
Properties:
Type: String
Description: Backstage RDS Admin Password
Name: !Sub /${Domain}/${System}/${Component}/${CodeBranch}/RdsDbAdminPassword
Value: !Ref RdsDbAdminPassword