forked from netbears/traefik-cluster-ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudformation-template-traefik.yaml
379 lines (345 loc) · 9.69 KB
/
cloudformation-template-traefik.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
Parameters:
ClusterHostMonitor:
Type: String
Description: The cluster which is being monitored
Domain:
Type: String
Description: Domain in which tasks should be registered to - MUST already exist as a zone in Route53
Default: example.com
EcsCluster:
Type: String
Description: CloudFormation stack name
Environment:
Type: String
AllowedValues:
- dev
- staging
- prod
Default: dev
TraefikImage:
Type: String
Description: The Repository URI for the Traefik docker image
LoadBalancerScheme:
Type: String
AllowedValues:
- internal
- internet-facing
Default: internal
Subnets:
Type: List<AWS::EC2::Subnet::Id>
Description: The subnets in which the load balancer will run
VpcId:
Type: AWS::EC2::VPC::Id
Description: The VPC in which the laod balancer will run
Resources:
LogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName:
Fn::Sub: ${AWS::StackName}-LogGroup
RetentionInDays: 14
TaskRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ecs-tasks.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: !Sub ${AWS::StackName}-Ecs
PolicyDocument:
Statement:
- Effect: Allow
Action:
- "ecs:*"
- "ec2:DescribeInstances"
Resource:
- "*"
TaskDef:
Type: AWS::ECS::TaskDefinition
Properties:
ContainerDefinitions:
- Name: traefik
Image: !Ref TraefikImage
Environment:
- Name: STACK_NAME
Value: !Ref AWS::StackName
- Name: ENVIRONMENT
Value: !Ref Environment
- Name: CLUSTER_HOST
Value: !Ref ClusterHostMonitor
- Name: AWS_REGION
Value: !Ref AWS::Region
- Name: DOMAIN
Value: !Ref Domain
PortMappings:
- ContainerPort: 80
- ContainerPort: 8080
Memory: 512
MemoryReservation: 256
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-region:
Ref: AWS::Region
awslogs-group: !Ref LogGroup
awslogs-stream-prefix:
Ref: AWS::StackName
Ulimits:
- HardLimit: 65536
SoftLimit: 10240
Name: nofile
TaskRoleArn: !GetAtt TaskRole.Arn
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:DeregisterInstancesFromLoadBalancer', 'elasticloadbalancing:DeregisterTargets',
'elasticloadbalancing:Describe*', 'elasticloadbalancing:RegisterInstancesWithLoadBalancer',
'elasticloadbalancing:RegisterTargets', 'ec2:Describe*', 'ec2:AuthorizeSecurityGroupIngress']
Resource: '*'
Service:
Type: AWS::ECS::Service
DependsOn: ALBListenerHttp
Properties:
Cluster: !Ref EcsCluster
TaskDefinition: !Ref TaskDef
DesiredCount: 2
Role: !Ref 'ECSServiceRole'
LoadBalancers:
- ContainerName: traefik
ContainerPort: '80'
TargetGroupArn: !Ref 'ECSTG'
DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 50
PlacementStrategies:
- Field: attribute:ecs.availability-zone
Type: spread
- Field: instanceId
Type: spread
ApplicationScalingRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- application-autoscaling.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: !Sub ${AWS::StackName}-Ecs
PolicyDocument:
Statement:
- Effect: Allow
Action:
- "application-autoscaling:*"
- "cloudwatch:DescribeAlarms"
- "cloudwatch:PutMetricAlarm"
- "ecs:*"
Resource:
- "*"
ScalableTarget:
Type: AWS::ApplicationAutoScaling::ScalableTarget
Properties:
MinCapacity: 2
MaxCapacity: 10
ResourceId: !Join
- /
- - service
- !Ref EcsCluster
- !GetAtt Service.Name
RoleARN: !GetAtt ApplicationScalingRole.Arn
ScalableDimension: ecs:service:DesiredCount
ServiceNamespace: ecs
ScaleOut:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
Properties:
PolicyName: ScaleOut
PolicyType: StepScaling
ScalingTargetId: !Ref ScalableTarget
StepScalingPolicyConfiguration:
AdjustmentType: ChangeInCapacity
Cooldown: 600
StepAdjustments:
- MetricIntervalLowerBound: 0
ScalingAdjustment: 1
ScaleIn:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
Properties:
PolicyName: ScaleIn
PolicyType: StepScaling
ScalingTargetId: !Ref ScalableTarget
StepScalingPolicyConfiguration:
AdjustmentType: ChangeInCapacity
Cooldown: 600
StepAdjustments:
- MetricIntervalUpperBound: 0
ScalingAdjustment: -1
CPUHigh:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- !Ref ScaleOut
ComparisonOperator: GreaterThanThreshold
Dimensions:
- Name: ClusterName
Value: !Ref EcsCluster
- Name: ServiceName
Value: !GetAtt Service.Name
EvaluationPeriods: 2
MetricName: CPUUtilization
Namespace: AWS/ECS
Period: 300
Statistic: Average
Threshold: 70
CPULow:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- !Ref ScaleIn
ComparisonOperator: LessThanThreshold
Dimensions:
- Name: ClusterName
Value: !Ref EcsCluster
- Name: ServiceName
Value: !GetAtt Service.Name
EvaluationPeriods: 2
MetricName: CPUUtilization
Namespace: AWS/ECS
Period: 300
Statistic: Average
Threshold: 30
EcsSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: ECS Allowed Ports
VpcId: !Ref VpcId
EcsSecurityGroupHttpInbound:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref 'EcsSecurityGroup'
IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
EcsSecurityGroupHttpAltInbound:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref 'EcsSecurityGroup'
IpProtocol: tcp
FromPort: '8080'
ToPort: '8080'
CidrIp: 0.0.0.0/0
EcsSecurityGroupHttpsInbound:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref 'EcsSecurityGroup'
IpProtocol: tcp
FromPort: '443'
ToPort: '443'
CidrIp: 0.0.0.0/0
Alb:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: !Sub "${AWS::StackName}-traefik"
Scheme: !Ref LoadBalancerScheme
LoadBalancerAttributes:
- Key: idle_timeout.timeout_seconds
Value: '30'
Subnets: !Ref Subnets
SecurityGroups: [ !Ref EcsSecurityGroup ]
ALBListenerHttp:
Type: AWS::ElasticLoadBalancingV2::Listener
DependsOn: ECSServiceRole
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref 'ECSTG'
LoadBalancerArn: !Ref 'Alb'
Port: '80'
Protocol: HTTP
ALBListenerHttpStatus:
Type: AWS::ElasticLoadBalancingV2::Listener
DependsOn: ECSServiceRole
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref 'ECSTG'
LoadBalancerArn: !Ref 'Alb'
Port: '8080'
Protocol: HTTP
ALBListenerRuleHttp:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Properties:
Actions:
- Type: forward
TargetGroupArn: !Ref 'ECSTG'
Conditions:
- Field: path-pattern
Values: [/]
ListenerArn: !Ref 'ALBListenerHttp'
Priority: 1
ALBListenerRuleHttpStatus:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Properties:
Actions:
- Type: forward
TargetGroupArn: !Ref 'ECSTG'
Conditions:
- Field: path-pattern
Values: [/]
ListenerArn: !Ref 'ALBListenerHttpStatus'
Priority: 2
ECSTG:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
DependsOn: Alb
Properties:
HealthCheckIntervalSeconds: 10
HealthCheckPath: /
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 5
Matcher:
HttpCode: '404'
Name: !Sub "${AWS::StackName}-traefik"
Port: 80
Protocol: HTTP
UnhealthyThresholdCount: 2
VpcId: !Ref VpcId
DnsRecord:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneName: !Sub "${Domain}."
Comment: !Sub Route53-record-for-${AWS::StackName}-traefik.
RecordSets:
- Name: !Sub "traefik.${Environment}.${Domain}."
Type: A
AliasTarget:
HostedZoneId: !GetAtt Alb.CanonicalHostedZoneID
DNSName: !GetAtt Alb.DNSName
Outputs:
TraefikAlbDualstackName:
Export:
Name: !Sub ${AWS::StackName}-TraefikAlbDualstackName
Value: !GetAtt Alb.DNSName
TraefikAlbHostedZone:
Export:
Name: !Sub ${AWS::StackName}-TraefikAlbHostedZone
Value: !GetAtt Alb.CanonicalHostedZoneID