-
Notifications
You must be signed in to change notification settings - Fork 6
/
stack.yaml
337 lines (311 loc) · 8.65 KB
/
stack.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
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
KeyName:
Type: AWS::EC2::KeyPair::KeyName
Description: Name of an existing EC2 KeyPair to enable SSH access to the ECS instances.
VpcId:
Type: AWS::EC2::VPC::Id
Description: Id of the VPC
ServiceSubnets:
Type: List<AWS::EC2::Subnet::Id>
Description: Select a default subnet ID.
TaskDesiredCapacity:
Type: Number
Default: '1'
Description: Number of containers to launch in your ECS cluster.
TaskMemory:
Type: Number
Default: '512'
Description: Reserved memory of the task definition
ClusterDesiredCapacity:
Type: Number
Default: '1'
Description: Number of instances to launch in your ECS cluster.
ClusterMaxSize:
Type: Number
Default: '1'
Description: Maximum number of instances that can be launched in your ECS cluster.
ClusterMaxMemoryReservationPercentage:
Type: Number
Default: '80'
Description: Max allowed percentage of memory reservation before auto scaling gets triggered
InstanceType:
Description: The EC2 instance type
Type: String
Default: t2.small
AllowedValues:
- t2.small
ConstraintDescription: You can specify only t2.small.
Mappings:
AWSRegionToAMI:
us-east-1:
AMIID: ami-55870742
us-west-1:
AMIID: ami-07713767
us-west-2:
AMIID: ami-241bd844
eu-west-1:
AMIID: ami-c74127b4
eu-central-1:
AMIID: ami-3b54be54
ap-northeast-1:
AMIID: ami-2b08f44a
ap-southeast-1:
AMIID: ami-6b61bc08
ap-southeast-2:
AMIID: ami-d5b59eb6
Resources:
ECSCluster:
Type: AWS::ECS::Cluster
service:
Type: AWS::ECS::Service
DependsOn:
- ECSAutoScalingGroup
Properties:
Cluster:
Ref: ECSCluster
DesiredCount: !Ref TaskDesiredCapacity
LoadBalancers:
- TargetGroupArn:
Ref: TargetGroup
# - LoadBalancerName: !Ref Loadbalancer
ContainerPort: 80
ContainerName: nginx
Role:
Ref: ECSServiceRole
TaskDefinition:
Ref: taskdefinition
taskdefinition:
Type: AWS::ECS::TaskDefinition
Properties:
ContainerDefinitions:
- Name: nginx
Cpu: 10
Essential: true
Image: nginx
Memory: !Ref TaskMemory
PortMappings:
- ContainerPort: 80
HostPort: 0
Loadbalancer:
Type: AWS::ElasticLoadBalancing::LoadBalancer
Properties:
CrossZone: 'true'
HealthCheck:
Target: HTTP:80/
HealthyThreshold: '2'
UnhealthyThreshold: '3'
Interval: '7'
Timeout: '5'
LoadBalancerName: !Sub ${AWS::StackName}-elb
Listeners:
- InstancePort: '80'
InstanceProtocol: HTTP
LoadBalancerPort: '80'
Protocol: HTTP
# SecurityGroups:
# - !Ref LoadbalancerServiceSecurityGroup
Subnets: !Ref ServiceSubnets
loadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Scheme: internet-facing
Subnets:
Ref: ServiceSubnets
# SecurityGroups:
# - Ref: AlbSecurityGroup
Listener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn:
Ref: TargetGroup
LoadBalancerArn:
Ref: loadBalancer
Port: '80'
Protocol: HTTP
TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 5
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 3
HealthCheckPath: "/"
HealthyThresholdCount: 4
Matcher:
HttpCode: '200'
Port: 80
Protocol: HTTP
UnhealthyThresholdCount: 3
VpcId:
Ref: VpcId
ECSAutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier:
Ref: ServiceSubnets
LaunchConfigurationName:
Ref: ContainerInstances
MinSize: '1'
MaxSize:
Ref: ClusterMaxSize
DesiredCapacity:
Ref: ClusterDesiredCapacity
CreationPolicy:
ResourceSignal:
Timeout: PT15M
UpdatePolicy:
AutoScalingRollingUpdate:
MinInstancesInService: '1'
MaxBatchSize: '1'
PauseTime: PT15M
WaitOnResourceSignals: 'true'
MemoryReservationScaleUpPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: PercentChangeInCapacity
AutoScalingGroupName:
Ref: ECSAutoScalingGroup
Cooldown: '300'
ScalingAdjustment: '30'
MemoryReservationHighAlert:
Type: AWS::CloudWatch::Alarm
Properties:
EvaluationPeriods: '1'
Statistic: Maximum
Threshold: !Ref ClusterMaxMemoryReservationPercentage
AlarmDescription: Alarm if CPU too high or metric disappears indicating instance
is down
Period: '60'
AlarmActions:
- Ref: MemoryReservationScaleUpPolicy
Namespace: AWS/ECS
Dimensions:
- Name: ClusterName
Value:
Ref: ECSCluster
ComparisonOperator: GreaterThanThreshold
MetricName: MemoryReservation
ContainerInstances:
Type: AWS::AutoScaling::LaunchConfiguration
Metadata:
AWS::CloudFormation::Init:
config:
commands:
01_add_instance_to_cluster:
command: !Sub |
#!/bin/bash
echo ECS_CLUSTER=${ECSCluster} >> /etc/ecs/ecs.config
files:
"/etc/cfn/cfn-hup.conf":
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
mode: '000400'
owner: root
group: root
"/etc/cfn/hooks.d/cfn-auto-reloader.conf":
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.ContainerInstances.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource ContainerInstances --region ${AWS::Region}
runas=root
services:
sysvinit:
cfn-hup:
enabled: 'true'
ensureRunning: 'true'
files:
- "/etc/cfn/cfn-hup.conf"
- "/etc/cfn/hooks.d/cfn-auto-reloader.conf"
Properties:
ImageId:
Fn::FindInMap:
- AWSRegionToAMI
- Ref: AWS::Region
- AMIID
InstanceType:
Ref: InstanceType
IamInstanceProfile:
Ref: EC2InstanceProfile
KeyName:
Ref: KeyName
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
yum install -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource ContainerInstances --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource ECSAutoScalingGroup --region ${AWS::Region}
ECSServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ecs.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: ecs-service
PolicyDocument:
Statement:
- Effect: Allow
Action:
- elasticloadbalancing:Describe*
- elasticloadbalancing:DeregisterInstancesFromLoadBalancer
- elasticloadbalancing:RegisterInstancesWithLoadBalancer
- elasticloadbalancing:DeregisterTargets
- elasticloadbalancing:RegisterTargets
- ec2:Describe*
- ec2:AuthorizeSecurityGroupIngress
Resource: "*"
EC2Role:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: ecs-service
PolicyDocument:
Statement:
- Effect: Allow
Action:
- ecs:CreateCluster
- ecs:DeregisterContainerInstance
- ecs:DiscoverPollEndpoint
- ecs:Poll
- ecs:RegisterContainerInstance
- ecs:StartTelemetrySession
- ecs:Submit*
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
EC2InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: "/"
Roles:
- Ref: EC2Role
Outputs:
ecsservice:
Value:
Ref: service
ecscluster:
Value:
Ref: ECSCluster
taskdef:
Value:
Ref: taskdefinition