This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathAutoScalingGroup.template.json
330 lines (315 loc) · 12.8 KB
/
AutoScalingGroup.template.json
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
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "Creates an autoscaling group.",
"Parameters" : {
"InstanceType" : {
"Description" : "AppServer EC2 instance type",
"Type" : "String",
"Default" : "t2.micro",
"AllowedValues" : [ "t2.micro", "t2.small", "t2.medium", "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "m3.medium", "m3.large", "m3.xlarge", "m3.2xlarge", "c1.medium", "c1.xlarge", "c3.large", "c3.xlarge", "c3.2xlarge", "c3.4xlarge", "c3.8xlarge", "g2.2xlarge", "r3.large", "r3.xlarge", "r3.2xlarge", "r3.4xlarge", "r3.8xlarge", "i2.xlarge", "i2.2xlarge", "i2.4xlarge", "i2.8xlarge", "hi1.4xlarge", "hs1.8xlarge", "cr1.8xlarge", "cc2.8xlarge", "cg1.4xlarge" ]
,
"ConstraintDescription" : "Must be a valid EC2 instance type."
},
"KeyName" : {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances",
"Type" : "AWS::EC2::KeyPair::KeyName",
"ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
},
"InstanceSecurityGroup" : {
"Description" : "Security Group for the instances.",
"Type" : "AWS::EC2::SecurityGroup::Id",
"Default": "sg-******"
},
"ServiceELB" : {
"Description" : "LoadBalancer to attach the instances to.",
"Type" : "String"
},
"InstanceAvailabilityZones" : {
"Description" : "A list of avilability zones in which instances will be launched. ",
"Type" : "CommaDelimitedList",
"Default" : "us-east-1b,us-east-1d"
},
"ASGDesiredCapacity" : {
"Description" : "The desired capacity for the Auto Scaling group",
"Default" : "2",
"Type" : "Number"
},
"ASGHealthCheckGracePeriod" : {
"Description" : "The length of time in seconds after a new EC2 instance comes into service that Auto Scaling starts checking its health.",
"Default" : "600",
"Type" : "Number"
},
"ASGHealthCheckType": {
"Description" : "The service you want the health status from, Amazon EC2 or Elastic Load Balancer. Valid values are EC2 or ELB.",
"Default" : "ELB",
"Type" : "String",
"AllowedValues" : [ "ELB", "EC2" ]
},
"ASGMinSize" : {
"Description" : "The minimum size of the Auto Scaling group.",
"Type" : "Number",
"Default" : "2"
},
"ASGMaxSize" : {
"Description" : "The maximum size of the Auto Scaling group.",
"Type" : "Number",
"Default" : "10"
},
"InstanceSubnets" : {
"Description" : "A list of subnet identifiers of Amazon Virtual Private Cloud (Amazon VPCs). The subnets that you specify for VPCZoneIdentifier must reside in the Availability Zones that you specify with the AvailabilityZones parameter.",
"Default" : "subnet-******,subnet-******",
"Type" : "List<AWS::EC2::Subnet::Id>"
},
"InstanceAMI" : {
"Description" : "AMI to use for instances.",
"Default" : "ami-146e2a7c",
"Type" : "String"
},
"InstanceIAMRole" : {
"Description" : "IAM Role to be attached to the instances.",
"Default" : "CfnRole",
"Type" : "String"
},
"Environment" : {
"Description" : "Instance Environment.",
"Default" : "uat2",
"Type" : "String"
},
"Role" : {
"Description" : "Instance Role.",
"Default" : "webcontainer-notification",
"Type" : "String"
}
},
"Mappings" : {
},
"Resources" : {
"AppServerGroup" : {
"Type" : "AWS::AutoScaling::AutoScalingGroup",
"Properties" : {
"AvailabilityZones" : { "Ref" : "InstanceAvailabilityZones" },
"DesiredCapacity" : { "Ref" : "ASGDesiredCapacity" },
"HealthCheckGracePeriod" : { "Ref" : "ASGHealthCheckGracePeriod" },
"HealthCheckType" : { "Ref" : "ASGHealthCheckType" },
"LaunchConfigurationName" : { "Ref" : "LaunchConfig" },
"MinSize" : { "Ref" : "ASGMinSize" },
"MaxSize" : { "Ref" : "ASGMaxSize" },
"LoadBalancerNames" : [ { "Ref" : "ServiceELB" } ],
"VPCZoneIdentifier" : { "Ref" : "InstanceSubnets" },
"Tags" : [ { "Key" : "Role", "Value" : { "Ref" : "Role" }, "PropagateAtLaunch" : "true"} ,
{ "Key" : "Environment", "Value" : { "Ref" : "Environment" }, "PropagateAtLaunch" : "true" } ,
{ "Key" : "CreatedBy", "Value" : "CloudFormation", "PropagateAtLaunch" : "true" },
{ "Key" : "Name", "Value" : { "Fn::Join" : [ "", [ { "Ref" : "Role" }, "-", { "Ref" : "AWS::Region" }, "-", { "Ref" : "Environment" }]]}, "PropagateAtLaunch" : "false" }
],
"NotificationConfiguration" : { "TopicARN" : "arn:aws:sns:us-east-1:725021207524:CloudformationNotifications",
"NotificationTypes" : [ "autoscaling:EC2_INSTANCE_LAUNCH",
"autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
"autoscaling:EC2_INSTANCE_TERMINATE",
"autoscaling:EC2_INSTANCE_TERMINATE_ERROR" ]
},
"MetricsCollection": [
{
"Granularity": "1Minute"
}
]
},
"CreationPolicy" : {
"ResourceSignal" : {
"Timeout" : "PT30M",
"Count" : "1"
}
},
"UpdatePolicy": {
"AutoScalingRollingUpdate": {
"MinInstancesInService": "2",
"MaxBatchSize": "1",
"PauseTime" : "PT15M",
"WaitOnResourceSignals": "true"
}
}
},
"LaunchConfig": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"configSets" : {
"bootstrap" : [ "Install_Cloudformation_Tools", "Verify_Instance_Health_From_ELB" ]
},
"Install_Cloudformation_Tools" : {
"files" : {
"/etc/cfn/cfn-hup.conf" : {
"content" : { "Fn::Join" : ["", [
"[main]\n",
"stack=", { "Ref" : "AWS::StackId" }, "\n",
"region=", { "Ref" : "AWS::Region" }, "\n"
]]},
"mode" : "000400",
"owner" : "root",
"group" : "root"
},
"/etc/cfn/hooks.d/cfn-auto-reloader.conf" : {
"content": { "Fn::Join" : ["", [
"[cfn-auto-reloader-hook]\n",
"triggers=post.update\n",
"path=Resources.LaunchConfig.Metadata.AWS::CloudFormation::Init\n",
"action=/opt/aws/bin/cfn-init -v ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource LaunchConfig ",
" --configsets bootstrap ",
" --region ", { "Ref" : "AWS::Region" }, "\n",
"runas=root\n"
]]}
}
},
"services" : {
"sysvinit" : {
"cfn-hup" : { "enabled" : "true", "ensureRunning" : "true",
"files" : ["/etc/cfn/cfn-hup.conf", "/etc/cfn/hooks.d/cfn-auto-reloader.conf"]}
}
}
},
"Verify_Instance_Health_From_ELB" : {
"commands" : {
"ELBHealthCheck" : {
"command" : { "Fn::Join" : ["", [
"until [ \"$state\" == \"\\\"InService\\\"\" ]; do ",
" state=$(aws --region ", { "Ref" : "AWS::Region" }, " elb describe-instance-health ",
" --load-balancer-name ", { "Ref" : "ServiceELB" },
" --instances $(curl -s http://169.254.169.254/latest/meta-data/instance-id) ",
" --query InstanceStates[0].State); ",
" sleep 10; ",
"done"
]]}
}
}
}
}
},
"Properties": {
"KeyName": { "Ref": "KeyName" },
"ImageId": { "Ref" : "InstanceAMI" },
"IamInstanceProfile": { "Ref" : "InstanceIAMRole" },
"InstanceType": { "Ref": "InstanceType" },
"SecurityGroups": [ { "Ref": "InstanceSecurityGroup" } ],
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash -xe\n",
"ROLE=", { "Ref" : "Role" }, "\n",
"ENV=", { "Ref" : "Environment" }, "\n",
"yum update -y \n",
"yum install -y aws-cfn-bootstrap\n",
"echo \"$(/sbin/ifconfig eth0 | grep \"inet addr:\" | cut -d: -f2 | cut -d\" \" -f1) $(hostname)\" >> /etc/hosts\n",
"true && curl -L https://www.opscode.com/chef/install.sh | bash -s -- -v 12.5.1\n",
"mkdir /etc/chef\n",
"mkdir /var/log/chef\n",
"aws s3 cp s3://bootstrap/bootstrap.tar.gz /tmp/bootstrap.tar.gz\n",
"tar -zxf /tmp/bootstrap.tar.gz -C /tmp\n",
"cd /tmp/bootstrap\n",
"cat > roles/$ROLE.json << EOL\n",
"{\n",
" \"name\": \"$ROLE\",\n",
" \"json_class\": \"Chef::Role\",\n",
" \"chef_type\": \"role\",\n",
" \"run_list\": [\n",
" \"recipe[ec2-ohai]\",\n",
" \"recipe[hostname]\"\n",
" ]\n",
"}\n",
"EOL\n",
"cat > environments/$ENV.json << EOL\n",
"{\n",
" \"name\": \"$ENV\",\n",
" \"json_class\": \"Chef::Environment\",\n",
" \"chef_type\": \"environment\"\n",
"}\n",
"EOL\n",
"chef-client --local-mode -E ", { "Ref" : "Environment" }, " -r 'role[", { "Ref" : "Role" }, "]'\n",
"aws s3 cp s3://bootstrap/dev-validator.pem /etc/chef/dev-validator.pem\n",
"aws s3 cp s3://bootstrap/client.rb /etc/chef/client.rb\n",
"chef-client -R -d -E ", { "Ref" : "Environment" }, " -r 'role[", { "Ref" : "Role" }, "]'\n",
"/opt/aws/bin/cfn-init -v ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource LaunchConfig ",
" --role ", { "Ref" : "InstanceIAMRole" },
" --configsets bootstrap ",
" --region ", { "Ref" : "AWS::Region" }, "\n",
"/opt/aws/bin/cfn-signal -e $? ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource AppServerGroup ",
" --region ", { "Ref" : "AWS::Region" }, "\n"
]]}}
}
},
"AppServerScaleUpPolicy" : {
"Type" : "AWS::AutoScaling::ScalingPolicy",
"Properties" : {
"AdjustmentType" : "ChangeInCapacity",
"AutoScalingGroupName" : { "Ref" : "AppServerGroup" },
"Cooldown" : "60",
"ScalingAdjustment" : "1"
}
},
"AppServerScaleDownPolicy" : {
"Type" : "AWS::AutoScaling::ScalingPolicy",
"Properties" : {
"AdjustmentType" : "ChangeInCapacity",
"AutoScalingGroupName" : { "Ref" : "AppServerGroup" },
"Cooldown" : "60",
"ScalingAdjustment" : "-1"
}
},
"CPUAlarmHigh": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"AlarmDescription": "Scale-up if CPU > 90% for 10 minutes",
"MetricName": "CPUUtilization",
"Namespace": "AWS/EC2",
"Statistic": "Average",
"Period": "300",
"EvaluationPeriods": "2",
"Threshold": "90",
"AlarmActions": [ { "Ref": "AppServerScaleUpPolicy" } ],
"Dimensions": [
{
"Name": "AutoScalingGroupName",
"Value": { "Ref": "AppServerGroup" }
}
],
"ComparisonOperator": "GreaterThanThreshold"
}
},
"CPUAlarmLow": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"AlarmDescription": "Scale-down if CPU < 50% for 10 minutes",
"MetricName": "CPUUtilization",
"Namespace": "AWS/EC2",
"Statistic": "Average",
"Period": "300",
"EvaluationPeriods": "2",
"Threshold": "50",
"AlarmActions": [ { "Ref": "AppServerScaleDownPolicy" } ],
"Dimensions": [
{
"Name": "AutoScalingGroupName",
"Value": { "Ref": "AppServerGroup" }
}
],
"ComparisonOperator": "LessThanThreshold"
}
}
},
"Outputs" : {
"AutoScalingGroupName" : {
"Value" : { "Ref": "AppServerGroup" },
"Description" : "AutoScaling Group Name."
},
"ScaleUpAlarm" : {
"Value" : { "Ref" : "CPUAlarmHigh" },
"Description" : "Scale Up Alarm"
},
"ScaleDownAlarm" : {
"Value" : { "Ref" : "CPUAlarmLow" },
"Description" : "Scale Down Alarm"
}
}
}