-
Notifications
You must be signed in to change notification settings - Fork 19
/
EC2-SQS-S3.yml
398 lines (366 loc) · 11.7 KB
/
EC2-SQS-S3.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# The only required property in a CloudFormation document is Resources
# Everything else is optional
Description: "Example EC2 Set-up with S3 and SQS, plus CloudWatch AutoScaling"
# Paramaters are referenced using the Ref property
# e.g. Ref: "CPUAlarmLowThreshold"
#
# Parameters can be assigned a default value, but if not then the value
# needs to be provided when uploading the CloudFormation.
# If uploading via CLI then you'll provide a --parameters flag
# If uploading via the AWS console, a GUI will be provided
Parameters:
CPUAlarmLowThreshold:
Default: 20
Type: "String"
Description: "This is the metric on which the alarm is activated. Our metric is percentage of CPU usage that will trigger the scaling group"
CPUAlarmLowEvalPeriods:
Default: 20
Type: "String"
Description: "This is how many times the metric is checked during the alarm period"
AlarmPeriod:
Default: 600
Type: "String"
Description: "The length of time that the eval periods will check within, E.g 600 seconds / 10 evals = checking every 60 seconds for 600 seconds"
UpdatePauseTime:
Default: "PT0S"
Type: "String"
Description: "The time to wait between new instances coming online and the next batch being killed during an ASG update"
UpdateMaxBatchSize:
Default: 1
Type: "String"
Description: "The maximum number of instances to be killed at one time during an ASG update"
UpdateMinInService:
Default: 0
Type: "String"
Description: "The minimum number of instances in service during an ASG update"
MinSize:
Default: 0
Type: "String"
Description: "The minimum number of instances"
MaxSize:
Default: 1
Type: "String"
Description: "The maximum number of instances"
S3BucketBasePath:
Default: "/foobar"
Type: "String"
S3BucketArn:
Default: "arn:aws:s3:::foobucket-sl9vplg1o48d"
Type: "String"
VisibilityTimeout:
Default: "21600"
Type: "String"
Description: "The length of time during which the queue will be unavailable once a message is delivered from the queue"
InstanceType:
Default: "m3.xlarge"
Type: "String"
Description: "The size of the instances"
MaxCount:
Default: "1"
Type: "String"
Description: "The maximum number of times a message can be received before it is sent to the Dead Letter Queue"
# Mappings allow us to map a key to a corresponding set of named values
# So if our instance is brought up in the eu-west-1 region we can use
# AWS::Region to tell us the current zone and then use the intrinsic function
# Fn::FindInMap to return the relevant matched value
Mappings:
RegionMap:
eu-west-1:
AMI:
- "ami-47cefa33"
# When pushing this CloudFormation we need to provide the public half of our key-pair
# See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html
#
# Then when attempting to SSH into the instance we can provide the private half
# See https://gist.github.com/Integralist/438898d6164daebec0c9
Resources:
ComponentLaunchConfiguration:
Type: "AWS::AutoScaling::LaunchConfiguration"
Properties:
KeyName:
Ref: "KeyName"
SecurityGroups:
Ref: "InstanceSecurityGroup"
InstanceType:
Ref: "InstanceType"
IamInstanceProfile:
Ref: "ComponentInstanceProfile"
ImageId:
Fn::FindInMap:
- "RegionMap"
- Ref: "AWS::Region"
- "AMI"
# We're allowing any ip address to access port 22 and 3000
InstanceSecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: "Enable SSH access via port 22"
SecurityGroupIngress:
- IpProtocol: "tcp"
FromPort: "22"
ToPort: "22"
CidrIp: "0.0.0.0/0"
EC2ComponentPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: "EC2ComponentPolicy"
PolicyDocument:
Statement:
- Action:
- "cloudwatch:*"
Resource:
- "*"
Effect:
- "Allow"
- Action:
- "cloudformation:Describe*"
Resource:
- "*"
Effect:
- "Allow"
- Action:
- "ec2:Describe*"
Resource:
- "*"
Effect:
- "Allow"
# If an application runs on an Amazon EC2 instance and needs to make
# requests for AWS resources such as Amazon S3 buckets or an DynamoDB table,
# it must have security credentials. It isn't a good practice to embed or
# pass IAM user credentials to each instance; distributing long-term
# credentials to each instance is challenging to manage and a potential
# security risk. A better strategy is to create a role that is used when
# the Amazon EC2 instance is launched. An application can then get
# temporary security credentials from the Amazon EC2 instance.
# These temporary security credentials are auto-rotated before they expire,
# and the AWS SDK automatically tries to utilise them if no other credentials
# are provided. Note: the credentials are deployed regularly to the
# EC2 instance metadata service
#
# See the following AWS Ruby Blog post that demonstrates this manually
# http://ruby.awsblog.com/post/TxP3XGEX8F3SHX/IAM-Roles-for-Amazon-EC2-Instances-Credential-Management-Part-4
Roles:
Ref: "FooRole"
# An instance profile is a container for an IAM role. Instance profiles are
# used to pass role information to an Amazon EC2 instance when the instance
# starts. An instance profile can contain only one role. However, a role can
# be included in multiple instance profiles.
ComponentInstanceProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Path: "/"
Roles:
- Ref: "FooRole"
FooRole:
Type: "AWS::IAM::Role"
Properties:
Path: "/"
# A user in one account (the trusted account) can assume a role in another
# account (the trusting account). To assume a role, a user (or an
# application that the user is running) calls the AWS STS AssumeRole API.
# Before the user can assume a role, in the trusting account an
# administrator must configure the role to assume. In addition, in the
# trusted account, the user must be given permissions to call the
# AssumeRole API.
AssumeRolePolicyDocument:
Statement:
- Action:
- "sts:AssumeRole"
Effect: "Allow"
Principal:
Service:
- "ec2.amazonaws.com"
ComponentAutoScalingGroup:
Type: "AWS::AutoScaling::AutoScalingGroup"
UpdatePolicy:
AutoScalingRollingUpdate:
PauseTime:
Ref: "UpdatePauseTime"
MaxBatchSize:
Ref: "UpdateMaxBatchSize"
MinInstancesInService:
Ref: "UpdateMinInService"
Properties:
Tags:
- Value: "Foo Project"
PropagateAtLaunch: true
Key: "FooKeyProject"
- Value: "Bar Application"
PropagateAtLaunch: true
Key: "BarKeyComponent"
MinSize:
Ref: "MinSize"
MaxSize:
Ref: "MaxSize"
LaunchConfigurationName:
Ref: "ComponentLaunchConfiguration"
AvailabilityZones:
- "eu-west-1a"
- "eu-west-1b"
- "eu-west-1c"
ComponentScaleUpPolicy:
Type: "AWS::AutoScaling::ScalingPolicy"
Properties:
ScalingAdjustment: 1
AutoScalingGroupName:
Ref: "ComponentAutoScalingGroup"
Cooldown: 240
AdjustmentType: ChangeInCapacity
ComponentScaleDownPolicy:
Type: "AWS::AutoScaling::ScalingPolicy"
Properties:
ScalingAdjustment: -1
AutoScalingGroupName:
Ref: "ComponentAutoScalingGroup"
Cooldown: 60
AdjustmentType: ChangeInCapacity
CPUAlarmLow:
Type: "AWS::CloudWatch::Alarm"
Properties:
EvaluationPeriods:
Ref: "CPUAlarmLowEvalPeriods"
Dimensions:
- Name: "AutoScalingGroupName"
Value:
Ref: "ComponentAutoScalingGroup"
AlarmActions:
- Ref: "ComponentScaleDownPolicy"
AlarmDescription: "Scale-down if CPU < 20% for 10 minutes"
Namespace: "AWS/EC2"
Period:
Ref: "AlarmPeriod"
ComparisonOperator: "LessThanThreshold"
Statistic: "Average"
Threshold:
Ref: "CPUAlarmLowThreshold"
MetricName: "CPUUtilization"
SQSJmeterQueue:
Type: "AWS::SQS::Queue"
Properties:
VisibilityTimeout:
Ref: "VisibilityTimeout"
RedrivePolicy:
deadLetterTargetArn:
Fn::GetAtt:
- "SQSJmeterDeadLetterQueue"
- "Arn"
maxReceiveCount:
Ref: "MaxCount"
MessageRetentionPeriod: 86400
SQSJmeterDeadLetterQueue:
Type: "AWS::SQS::Queue"
Properties:
MessageRetentionPeriod: 432000
SQSJmeterQueuePolicy:
Type: "AWS::SQS::QueuePolicy"
Properties:
Queues:
- Ref: "SQSJmeterQueue"
PolicyDocument:
Version: "2012-10-17"
Id: "SQSJmeterQueuePolicy"
Statement:
- Resource:
- Fn::GetAtt:
- "SQSJmeterQueue"
- "Arn"
Effect: "Allow"
Sid: "Allow-User-SendMessage"
Action:
- "sqs:*"
Condition:
IpAddress:
aws:SourceIp:
- "135.185.0.0/32"
- "212.58.224.0/19"
- "212.58.231.0/19"
- "54.217.70.105"
Principal:
AWS: "*"
SQSAvailableHigh:
Type: "AWS::CloudWatch::Alarm"
Properties:
EvaluationPeriods: 1
Dimensions:
- Name: "QueueName"
Value:
Fn::GetAtt:
- "SQSJmeterQueue"
- "QueueName"
AlarmActions:
- Ref: "ComponentScaleUpPolicy"
AlarmDescription: "Scale-up if SQS available messages is > 0 for 5 minutes"
Namespace: "AWS/SQS"
Period: 300
ComparisonOperator: "GreaterThanThreshold"
Statistic: "Average"
Threshold: 0
MetricName: "ApproximateNumberOfMessagesVisible"
QueuesPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: "QueuesPolicy"
PolicyDocument:
Statement:
- Action:
- "sqs:*"
Resource:
- Fn::GetAtt:
- "SQSJmeterQueue"
- "Arn"
Effect: "Allow"
Role:
- Ref: "FooRole"
StoragePolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: "StoragePolicy"
PolicyDocument:
Statement:
- Action:
- "s3:*"
Resource:
- Ref: "S3BucketArn"
Effect: "Allow"
- Action:
- "s3:*"
Resource:
- Fn::Join:
- ""
-
- Ref: "S3BucketArn"
- Ref: "S3BucketBasePath"
- "/*"
Effect: "Allow"
- Action:
- "sts:AssumeRole"
Resource:
- "*"
Effect: "Allow"
- Action:
- "s3:*"
Resource:
- Fn::Join:
- ""
-
- Ref: "S3BucketArn"
- Ref: "S3BucketBasePath"
- "/logs/*"
Effect: "Allow"
Role:
- Ref: "FooRole"
# The optional Outputs section declares the values that you want to return
# in response to describe stack calls.
# This output appears in the AWS console
Outputs:
WebsiteURL:
Description: "Our Website URL"
Value:
Fn::Join:
- ""
-
- "http://"
- Fn::GetAtt:
- "Ec2Instance"
- "PublicIp"
- ":3000"