-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
611 lines (548 loc) · 19.4 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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
amazon-ec2-autoscaling-predictive-scaling-example
Sample CloudFormation template that deploys an Auto Scaling group for demonstrating predictive scaling.
Parameters:
ApplicationAmiId:
Description: AMI Id
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
ApplicationAutoScalingGroupName:
Description: Application Auto Scaling Group Name
Type: String
Default: Example Application Auto Scaling Group
ApplicationAutoScalingGroupMinSize:
Description: Application Auto Scaling Min Size
Type: Number
Default: 6
ApplicationAutoScalingGroupMaxSize:
Description: Application Auto Scaling Max Size
Type: Number
Default: 100
ApplicationAutoScalingGroupDesiredCapacity:
Description: Application Auto Scaling Desired Capacity
Type: Number
Default: 6
ApplicationInstanceType:
Description: Amazon EC2 Instance Type
Type: String
Default: "m5.large"
ApplicationInstanceKeyPair:
Description: Amazon EC2 Key Pair
Type: "AWS::EC2::KeyPair::KeyName"
LoadAmiId:
Description: AMI Id
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
LoadAutoScalingGroupName:
Description: Load Auto Scaling Group Name
Type: String
Default: Example Load Auto Scaling Group
LoadAutoScalingGroupMinSize:
Description: Load Auto Scaling Group Min Size
Type: Number
Default: 2
LoadAutoScalingGroupMaxSize:
Description: Load Auto Scaling Group Max Size
Type: Number
Default: 10
LoadAutoScalingGroupDesiredCapacity:
Description: Load Auto Scaling Group Desired Capacity
Type: Number
Default: 2
LoadInstanceType:
Description: Amazon EC2 Instance Type
Type: String
Default: "m5.8xlarge"
LoadInstanceKeyPair:
Description: Amazon EC2 Key Pair
Type: "AWS::EC2::KeyPair::KeyName"
VpcCIDR:
Description: Please enter the IP range (CIDR notation) for this VPC
Type: String
Default: 10.192.0.0/16
AvailabilityZone1CIDR:
Description: Please enter the IP range (CIDR notation) for the app subnet in the first Availability Zone
Type: String
Default: 10.192.10.0/24
AvailabilityZone2CIDR:
Description: Please enter the IP range (CIDR notation) for the app subnet in the second Availability Zone
Type: String
Default: 10.192.11.0/24
AvailabilityZone3CIDR:
Description: Please enter the IP range (CIDR notation) for the app subnet in the third Availability Zone
Type: String
Default: 10.192.12.0/24
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: "VPC Configuration"
Parameters:
- VpcCIDR
- AvailabilityZone1CIDR
- AvailabilityZone2CIDR
- AvailabilityZone3CIDR
-
Label:
default: "Application Auto Scaling Group Configuration"
Parameters:
- ApplicationAmiId
- ApplicationInstanceType
- ApplicationInstanceKeyPair
- ApplicationAutoScalingGroupName
- ApplicationAutoScalingGroupVpcID
- ApplicationAutoScalingGroupSubnetIDs
- ApplicationAutoScalingGroupMinSize
- ApplicationAutoScalingGroupMaxSize
- ApplicationAutoScalingGroupDesiredCapacity
-
Label:
default: "Load Generator Auto Scaling Group Configuration"
Parameters:
- LoadAmiId
- LoadInstanceType
- LoadInstanceKeyPair
- LoadAutoScalingGroupName
- LoadAutoScalingGroupVpcID
- LoadAutoScalingGroupSubnetIDs
- LoadAutoScalingGroupMinSize
- LoadAutoScalingGroupMaxSize
- LoadAutoScalingGroupDesiredCapacity
Resources:
# VPC/Networking Resources
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCIDR
EnableDnsHostnames: true
InternetGateway:
Type: AWS::EC2::InternetGateway
InternetGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
AvailabilityZoneSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [ 0, !GetAZs '' ]
CidrBlock: !Ref AvailabilityZone1CIDR
MapPublicIpOnLaunch: true
AvailabilityZoneSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [ 1, !GetAZs '' ]
CidrBlock: !Ref AvailabilityZone2CIDR
MapPublicIpOnLaunch: true
AvailabilityZoneSubnet3:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [ 2, !GetAZs '' ]
CidrBlock: !Ref AvailabilityZone3CIDR
MapPublicIpOnLaunch: true
ApplicationInstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPC
GroupDescription: Application Instance Security Group
SecurityGroupIngress:
- SourceSecurityGroupId:
Ref: ApplicationLoadBalancerSecurityGroup
IpProtocol: -1
- CidrIp: '0.0.0.0/0'
IpProtocol: TCP
FromPort: 80
ToPort: 80
- SourceSecurityGroupId:
Ref: LoadInstanceSecurityGroup
IpProtocol: -1
- CidrIp: '0.0.0.0/0'
IpProtocol: TCP
FromPort: 80
ToPort: 80
LoadInstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPC
GroupDescription: Load Instance Security Group
RouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
DefaultPublicRoute:
Type: AWS::EC2::Route
DependsOn: InternetGatewayAttachment
Properties:
RouteTableId: !Ref RouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
AvailabilityZoneSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref RouteTable
SubnetId: !Ref AvailabilityZoneSubnet1
AvailabilityZoneSubnet2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref RouteTable
SubnetId: !Ref AvailabilityZoneSubnet2
AvailabilityZoneSubnet3RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref RouteTable
SubnetId: !Ref AvailabilityZoneSubnet3
# Application Auto Scaling Group Resources
ApplicationLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
ImageId: !Ref ApplicationAmiId
InstanceType: !Ref ApplicationInstanceType
KeyName: !Ref ApplicationInstanceKeyPair
SecurityGroupIds:
- !Ref ApplicationInstanceSecurityGroup
IamInstanceProfile:
Arn: !GetAtt
- ApplicationInstanceProfile
- Arn
TagSpecifications:
- ResourceType: instance
Tags:
- Key: Name
Value: ScalingApplicationInstance
UserData:
'Fn::Base64':
!Sub
- |-
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
sudo yum install golang -y
cat <<EoF > /tmp/app.go
package main
import (
"fmt"
"net/http"
"runtime"
"time"
"log"
"html"
)
func stress() {
n := runtime.NumCPU()
runtime.GOMAXPROCS(n)
quit := make(chan bool)
for i := 0; i < n; i++ {
go func() {
for {
select {
case <-quit:
return
default:
}
}
}()
}
time.Sleep(10 * time.Millisecond)
for i := 0; i < n; i++ {
quit <- true
}
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
stress()
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
log.Fatal(http.ListenAndServe(":80", nil))
}
EoF
export GOCACHE="/tmp/.cache/go-build"
go build -o /tmp/app /tmp/app.go
/tmp/app
--//
- {
autoScalingGroupName: !Ref ApplicationAutoScalingGroupName
}
ApplicationInstanceRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "ec2.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
ApplicationInstanceProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Path: "/"
Roles:
-
Ref: "ApplicationInstanceRole"
ApplicationAutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
AutoScalingGroupName: !Ref ApplicationAutoScalingGroupName
LaunchTemplate:
LaunchTemplateId: !Ref ApplicationLaunchTemplate
Version: !GetAtt ApplicationLaunchTemplate.LatestVersionNumber
DesiredCapacity: !Ref ApplicationAutoScalingGroupDesiredCapacity
MaxSize: !Ref ApplicationAutoScalingGroupMaxSize
MinSize: !Ref ApplicationAutoScalingGroupMinSize
MetricsCollection:
- Granularity: "1Minute"
VPCZoneIdentifier:
- !Ref AvailabilityZoneSubnet1
- !Ref AvailabilityZoneSubnet2
- !Ref AvailabilityZoneSubnet3
TargetGroupARNs:
- Ref: ApplicationTargetGroup
TargetTrackingScalingCPUUtilization:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AutoScalingGroupName:
Ref: ApplicationAutoScalingGroup
PolicyType: TargetTrackingScaling
TargetTrackingConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ASGAverageCPUUtilization
TargetValue: 25
EstimatedInstanceWarmup: 300
# Load Auto Scaling Group Resources
LoadLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
ImageId: !Ref LoadAmiId
InstanceType: !Ref LoadInstanceType
KeyName: !Ref LoadInstanceKeyPair
SecurityGroupIds:
- !Ref LoadInstanceSecurityGroup
IamInstanceProfile:
Arn: !GetAtt
- LoadInstanceProfile
- Arn
TagSpecifications:
- ResourceType: instance
Tags:
- Key: Name
Value: ScalingLoadInstance
UserData:
'Fn::Base64':
!Sub
- |-
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
sudo amazon-linux-extras install epel -y
sudo yum -y install curl
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
sudo yum install -y nodejs
sudo npm install -g artillery@latest -y
sudo artillery dino
sudo systemctl start crond
sudo systemctl enable crond
cat <<EoF > /tmp/recurring-ramp-up-load.yaml
config:
target: "http://${applicationLoadBalancerDnsName}"
http:
timeout: 240
phases:
- duration: 300
arrivalRate: 20
name: Warm up
- duration: 600
arrivalRate: 20
rampTo: 200
name: Ramp up load
- duration: 5400
arrivalRate: 200
name: Sustained load
- duration: 900
arrivalRate: 200
rampTo: 20
name: Ramp down load
scenarios:
- name: "Get Home Page"
flow:
- get:
url: "/"
EoF
cat <<EoF > /tmp/recurring-morning-spike.yaml
config:
target: "http://${applicationLoadBalancerDnsName}"
http:
timeout: 240
phases:
- duration: 10
arrivalRate: 20
name: Warm up
- duration: 10
arrivalRate: 20
rampTo: 400
name: Ramp up load
- duration: 6280
arrivalRate: 400
name: Sustained load
- duration: 900
arrivalRate: 400
rampTo: 20
name: Ramp down load
scenarios:
- name: "Get Home Page"
flow:
- get:
url: "/"
EoF
crontab<<EoF
0 0 * * * MULTICORE=1 ARTILLERY_WORKERS=16 node --max_old_space_size=3092 `which artillery` run /tmp/recurring-ramp-up-load.yaml
0 4 * * * MULTICORE=1 ARTILLERY_WORKERS=16 node --max_old_space_size=3092 `which artillery` run /tmp/recurring-ramp-up-load.yaml
0 8 * * * MULTICORE=1 ARTILLERY_WORKERS=16 node --max_old_space_size=3092 `which artillery` run /tmp/recurring-morning-spike.yaml
0 12 * * * MULTICORE=1 ARTILLERY_WORKERS=16 node --max_old_space_size=3092 `which artillery` run /tmp/recurring-ramp-up-load.yaml
0 16 * * * MULTICORE=1 ARTILLERY_WORKERS=16 node --max_old_space_size=3092 `which artillery` run /tmp/recurring-ramp-up-load.yaml
0 20 * * * MULTICORE=1 ARTILLERY_WORKERS=16 node --max_old_space_size=3092 `which artillery` run /tmp/recurring-ramp-up-load.yaml
EoF
--//
- {
applicationLoadBalancerDnsName: !GetAtt ApplicationLoadBalancer.DNSName
}
LoadInstanceRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "ec2.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
LoadInstanceProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Path: "/"
Roles:
-
Ref: "LoadInstanceRole"
LoadAutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
AutoScalingGroupName: !Ref LoadAutoScalingGroupName
LaunchTemplate:
LaunchTemplateId: !Ref LoadLaunchTemplate
Version: !GetAtt LoadLaunchTemplate.LatestVersionNumber
DesiredCapacity: !Ref LoadAutoScalingGroupDesiredCapacity
MaxSize: !Ref LoadAutoScalingGroupMaxSize
MinSize: !Ref LoadAutoScalingGroupMinSize
VPCZoneIdentifier:
- !Ref AvailabilityZoneSubnet1
- !Ref AvailabilityZoneSubnet2
- !Ref AvailabilityZoneSubnet3
# Application Load Balancer
ApplicationLoadBalancerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: ALB Security Group
SecurityGroupIngress:
- CidrIp: '0.0.0.0/0'
IpProtocol: TCP
FromPort: 80
ToPort: 80
VpcId:
Ref: VPC
ApplicationLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Subnets:
- Ref: AvailabilityZoneSubnet1
- Ref: AvailabilityZoneSubnet2
- Ref: AvailabilityZoneSubnet3
SecurityGroups:
- Ref: ApplicationLoadBalancerSecurityGroup
ApplicationLoadBalancerListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn:
Ref: ApplicationLoadBalancer
Port: 80
Protocol: HTTP
DefaultActions:
- Type: forward
TargetGroupArn:
Ref: ApplicationTargetGroup
ApplicationTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
DependsOn: ApplicationLoadBalancer
Properties:
VpcId:
Ref: VPC
Port: 80
Protocol: HTTP
TargetGroupAttributes:
- Key: deregistration_delay.timeout_seconds
Value: 60
Outputs:
ApplicationLoadBalancerArn:
Description: "Load Balancer Arn"
Value: !Ref ApplicationLoadBalancer
ApplicationTargetGroupArn:
Description: "Target Group Arn"
Value: !Ref ApplicationTargetGroup