/
cloudformation.yml
355 lines (332 loc) · 9.88 KB
/
cloudformation.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
AWSTemplateFormatVersion: "2010-09-09"
Description: Frontend rendering service
Parameters:
Subnets:
Description: The subnets where rendering instances will run
Type: List<AWS::EC2::Subnet::Id>
VpcId:
Description: The VPC in which rendering instances will run
Type: AWS::EC2::VPC::Id
App:
Description: Application name
Type: String
Default: rendering
Stage:
Description: Stage name
Type: String
AllowedValues:
- PROD
- CODE
Default: CODE
Stack:
Description: Stack name
Type: String
Default: frontend
AMI:
Description: AMI to use for instances
Type: AWS::EC2::Image::Id
NotificationAlarmAction:
Type: CommaDelimitedList
Description: (Optional) ARN of action to execute when notification alarms change state
Default: ''
Backend5XXAlarmThreshold:
Type: String
Description: (Optional) Max number of errors before the 5XX alarm is triggered
Default: '100'
Backend5XXAlarmPeriod:
Type: String
Description: (Optional) Duration in seconds before 5XX alarm is triggered
Default: '60'
Backend5XXConsecutivePeriod:
Type: String
Description: (Optional) Number of consecutive periods the threshold needs to be reached before 5XX alarm is triggered
Default: '5'
InstanceType:
Type: String
Description: EC2 Instance Type to use for dotcom-rendering
VPCIpBlock:
Type: String
Description: CIDR block for IP addresses inside this VPC
Default: 10.248.136.0/22
ELKStream:
Type: String
Description: name of the kinesis stream to use to send logs to the central ELK stack
FrontendConfigKey:
Description: Parameter store KMS key
Type: String
Conditions:
HasLatencyScalingAlarm: !Equals [!Ref Stage, 'PROD']
HasBackend5XXAlarm: !Equals [!Ref Stage, 'PROD']
Mappings:
Constants:
Stack:
Value: frontend
App:
Value: rendering
StageMap:
PROD:
MinCapacity: 9
MaxCapacity: 36
CODE:
MinCapacity: 1
MaxCapacity: 4
Resources:
InternalLoadBalancerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allows HTTP and HTTPS inbound connections from within the VPC
VpcId:
Ref: VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: !Ref VPCIpBlock
- IpProtocol: tcp
FromPort: '443'
ToPort: '443'
CidrIp: !Ref VPCIpBlock
Tags:
- Key: Stage
Value:
Ref: Stage
- Key: Stack
Value:
Fn::FindInMap: [ Constants, Stack, Value ]
- Key: App
Value:
Fn::FindInMap: [ Constants, App, Value ]
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: rendering instance
VpcId:
Ref: VpcId
SecurityGroupIngress:
# allow ELB to talk to instance
- IpProtocol: tcp
FromPort: 9000
ToPort: 9000
SourceSecurityGroupId:
Ref: InternalLoadBalancerSecurityGroup
Tags:
- Key: Stage
Value:
Ref: Stage
- Key: Stack
Value:
Fn::FindInMap: [ Constants, Stack, Value ]
- Key: App
Value:
Fn::FindInMap: [ Constants, App, Value ]
InstanceRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: instance-policy
PolicyDocument:
Statement:
# grant access to the distribution bucket in S3
- Effect: Allow
Action: s3:GetObject
Resource: arn:aws:s3:::aws-frontend-artifacts/*
- Effect: Allow
Action:
- cloudwatch:*
- logs:*
Resource: '*'
- Effect: Allow
Action:
- ec2:DescribeTags
- ec2:DescribeInstances
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeAutoScalingInstances
Resource: '*'
- Resource: !Ref FrontendConfigKey
Effect: Allow
Action:
- kms:Decrypt
- kms:DescribeKey
- Effect: Allow
Action:
- ssm:GetParametersByPath
- ssm:GetParameter
Resource:
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/frontend/*
- Effect: Allow
Action:
- ssm:GetParametersByPath
- ssm:GetParameter
Resource:
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/dotcom/*
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- Ref: InstanceRole
InternalLoadBalancer:
Type: AWS::ElasticLoadBalancing::LoadBalancer
Properties:
Scheme: internal
LoadBalancerName: !Sub '${Stack}-${Stage}-${App}-ELB'
Listeners:
- { LoadBalancerPort: 80, InstancePort: 9000, Protocol: HTTP }
CrossZone: true
HealthCheck:
Target: HTTP:9000/_healthcheck
HealthyThreshold: 2
UnhealthyThreshold: 10
Interval: 30
Timeout: 10
Subnets:
Ref: Subnets
SecurityGroups:
- Ref: InternalLoadBalancerSecurityGroup
AccessLoggingPolicy:
EmitInterval: 5
Enabled: true
S3BucketName: gu-elb-logs
S3BucketPrefix:
Fn::Join:
- "/"
- - ELBLogs
- Fn::FindInMap: [ Constants, Stack, Value ]
- Fn::FindInMap: [ Constants, App, Value ]
- Ref: Stage
Tags:
- Key: Stage
Value:
Ref: Stage
- Key: Stack
Value:
Fn::FindInMap: [ Constants, Stack, Value ]
- Key: App
Value:
Fn::FindInMap: [ Constants, App, Value ]
LaunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
ImageId:
Ref: AMI
SecurityGroups:
- Ref: InstanceSecurityGroup
InstanceType: !Ref InstanceType
IamInstanceProfile:
Ref: InstanceProfile
AssociatePublicIpAddress: true
UserData:
'Fn::Base64':
!Sub |
#!/bin/bash -ev
groupadd frontend
useradd -r -m -s /usr/bin/nologin -g frontend dotcom-rendering
usermod -a -G frontend aws-kinesis-agent-user
cd /home/dotcom-rendering
aws --region eu-west-1 s3 cp s3://aws-frontend-artifacts/frontend/${Stage}/${App}/dist/${App}.zip ./
unzip -q ${App}.zip -d ${App}
chown -R dotcom-rendering:frontend ${App}
cd ${App}/dotcom-rendering
export TERM=xterm-256color
export NODE_ENV=production
export GU_STAGE=${Stage}
mkdir /var/log/dotcom-rendering
chown -R dotcom-rendering:frontend /var/log/dotcom-rendering
make start-prod
/opt/aws-kinesis-agent/configure-aws-kinesis-agent ${AWS::Region} ${ELKStream} /var/log/dotcom-rendering/dotcom-rendering.log
AutoscalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
AvailabilityZones: !GetAZs ''
VPCZoneIdentifier:
Ref: Subnets
LaunchConfigurationName:
Ref: LaunchConfig
MinSize: !FindInMap [StageMap, !Ref Stage, MinCapacity]
MaxSize: !FindInMap [StageMap, !Ref Stage, MaxCapacity]
HealthCheckType: ELB
HealthCheckGracePeriod: 120
LoadBalancerNames:
- Ref: InternalLoadBalancer
Tags:
- Key: Stage
Value:
Ref: Stage
PropagateAtLaunch: true
- Key: Stack
Value:
Fn::FindInMap: [ Constants, Stack, Value ]
PropagateAtLaunch: true
- Key: App
Value:
Fn::FindInMap: [ Constants, App, Value ]
PropagateAtLaunch: true
ScaleDownPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: ChangeInCapacity
AutoScalingGroupName: !Ref AutoscalingGroup
Cooldown: '120'
ScalingAdjustment: '-1'
ScaleUpPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: PercentChangeInCapacity
AutoScalingGroupName: !Ref AutoscalingGroup
Cooldown: '600'
ScalingAdjustment: '100'
LatencyScalingAlarm:
Condition: HasLatencyScalingAlarm
Properties:
AlarmDescription: !Sub |
Scale-Up if latency is greater than 0.2 seconds over 1 period(s) of 60 seconds
Dimensions:
- Name: LoadBalancerName
Value: !Ref InternalLoadBalancer
EvaluationPeriods: '1'
MetricName: Latency
Namespace: AWS/ELB
Period: '60'
Statistic: Average
Threshold: '0.2'
ComparisonOperator: GreaterThanOrEqualToThreshold
OKActions:
- !Ref ScaleDownPolicy
AlarmActions:
- !Ref ScaleUpPolicy
Type: AWS::CloudWatch::Alarm
Backend5xxAlarm:
Type: AWS::CloudWatch::Alarm
Condition: HasBackend5XXAlarm
Properties:
AlarmDescription: !Sub |
Alarm if 5XX backend errors are greater than ${Backend5XXAlarmThreshold} over last ${Backend5XXAlarmPeriod} seconds
ComparisonOperator: GreaterThanOrEqualToThreshold
Dimensions:
- Name: LoadBalancerName
Value: !Ref InternalLoadBalancer
MetricName: HTTPCode_Backend_5XX
Namespace: AWS/ELB
EvaluationPeriods: !Ref Backend5XXConsecutivePeriod
Period: !Ref Backend5XXAlarmPeriod
Statistic: Sum
Threshold: !Ref Backend5XXAlarmThreshold
AlarmActions: !Ref NotificationAlarmAction
OKActions: !Ref NotificationAlarmAction
Outputs:
LoadBalancerUrl:
Value:
'Fn::GetAtt':
- InternalLoadBalancer
- DNSName