-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs-stack.yaml
329 lines (297 loc) · 9.15 KB
/
ecs-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
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
StackName:
Type: String
Default: "vpc-stack"
ImageUrl:
Type: String
Default: "REPO_URL/IMAGE:TAG" # Replace REPO_URL, IMAGE, TAG values
ContainerPort:
Type: Number
Default: 80
ContainerCpu:
Type: Number
Default: 1024
ContainerMemory:
Type: Number
Default: 2048
Priority:
Type: Number
Default: 1
DesiredCount:
Type: Number
Default: 1
CertificateArn:
Description: "AWS ACM Certificate for endpoint"
Type: String
Default: "ACM_CERT_NAME" # Replace ACM_CERT_NAME values
ContainerEnvValue:
Type: String
Default: "Value_if_any"
HighCPUThreshold:
Type: Number
Default: 80
LowCPUThreshold:
Type: Number
Default: 20
MaxContainers:
Type: Number
Default: 4
MinContainers:
Type: Number
Default: 1
environment:
Type: String
Default: "prod"
HealthCheckPath:
Type: String
Default: "/"
Resources:
CloudWatchLogsGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join [ "", ["/ecs/", !Ref environment, "-app" ]]
RetentionInDays: 365
ECSCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: !Join ['-', [!Ref environment, "app", "cluster"]]
ECSTaskRole:
Type: AWS::IAM::Role
Properties:
RoleName: "EcsTaskRole"
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [ ecs-tasks.amazonaws.com ]
Action: [ "sts:AssumeRole" ]
Policies:
- PolicyName: "EcsTaskRolePolicy"
PolicyDocument:
Statement:
- Effect: "Allow"
Action:
- "ec2:*"
- "elasticloadbalancing:*"
- "cloudwatch:*"
- "logs:*"
- "ecr:*"
Resource: "*"
TaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
ExecutionRoleArn: !Ref ECSTaskRole
Family: !Join ['_', [!Ref environment, "app"]]
Cpu: !Ref ContainerCpu
Memory: !Ref ContainerMemory
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
ContainerDefinitions:
- Name: "app_container"
Cpu: !Ref ContainerCpu
Memory: !Ref ContainerMemory
Image: !Ref ImageUrl
Essential: true
Environment:
- Name: ContainerEnvValue
Value: !Ref ContainerEnvValue
PortMappings:
- ContainerPort: !Ref ContainerPort
HostPort: !Ref ContainerPort
Protocol: "tcp"
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Join [ "", ["/ecs/", !Ref environment, "-app" ]]
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: "ecs"
Service:
Type: AWS::ECS::Service
DependsOn: LoadBalancerListener
Properties:
ServiceName: !Join ['_', [!Ref environment, "app","service"]]
Cluster: !Ref ECSCluster
LaunchType: FARGATE
DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 50
DesiredCount: !Ref DesiredCount
HealthCheckGracePeriodSeconds: 600
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: DISABLED
SecurityGroups:
- !Ref ECSSecurityGroup
Subnets:
- Fn::ImportValue: !Join [':', [!Ref 'StackName', 'PrivateSubnet1']]
- Fn::ImportValue: !Join [':', [!Ref 'StackName', 'PrivateSubnet2']]
TaskDefinition: !Ref TaskDefinition
LoadBalancers:
- ContainerName: "app_container"
ContainerPort: !Ref ContainerPort
TargetGroupArn: !Ref TargetGroup
ECSSecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupName: !Join ['-', [!Ref environment, "ecs","sg"]]
GroupDescription: !Sub "Controls access to ${environment} ecs"
SecurityGroupIngress:
- SourceSecurityGroupId: !Ref LBSecurityGroup
IpProtocol: "TCP"
FromPort: !Ref ContainerPort
ToPort: !Ref ContainerPort
SecurityGroupEgress:
- CidrIp: "0.0.0.0/0"
IpProtocol: "-1"
FromPort: 0
ToPort: 0
VpcId:
Fn::ImportValue:
!Join [':', [!Ref 'StackName', 'VPC']]
LBSecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupName: !Join ['-', [!Ref environment, "lb","sg"]]
GroupDescription: !Sub "Controls access to ${environment} lb"
SecurityGroupIngress:
- CidrIp: "0.0.0.0/0"
IpProtocol: "TCP"
FromPort: 443
ToPort: 443
SecurityGroupEgress:
- CidrIp: "0.0.0.0/0"
IpProtocol: "-1"
FromPort: 0
ToPort: 0
VpcId:
Fn::ImportValue:
!Join [':', [!Ref 'StackName', 'VPC']]
LoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: !Join ['-', [!Ref environment, "lb"]]
Subnets:
- Fn::ImportValue: !Join [':', [!Ref 'StackName', 'PublicSubnet1']]
- Fn::ImportValue: !Join [':', [!Ref 'StackName', 'PublicSubnet2']]
SecurityGroups:
- !Ref LBSecurityGroup
TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Name: !Join ['-', [!Ref environment, "target", "group"]]
Port: !Ref ContainerPort
Protocol: HTTP
VpcId:
Fn::ImportValue:
!Join [':', [!Ref 'StackName', 'VPC']]
TargetType: ip
HealthCheckIntervalSeconds: 15
HealthCheckPath: !Ref HealthCheckPath
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 3
UnhealthyThresholdCount: 2
LoadBalancerListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn: !Ref LoadBalancer
Port: 443
Protocol: HTTPS
Certificates:
- CertificateArn: !Ref CertificateArn
DefaultActions:
- Type: forward
TargetGroupArn: !Ref TargetGroup
ECSAutoscalingRole:
Type: AWS::IAM::Role
Properties:
RoleName: "ECSAutoscalingRole"
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [ application-autoscaling.amazonaws.com ]
Action: [ "sts:AssumeRole" ]
Policies:
- PolicyName: "ECSAutoscalingRolePolicy"
PolicyDocument:
Statement:
- Effect: "Allow"
Action:
- "application-autoscaling:*"
- "cloudwatch:DescribeAlarms"
- "cloudwatch:PutMetricAlarm"
- "ecs:DescribeServices"
- "ecs:UpdateService"
Resource: "*"
ECSAutoscalingTarget:
Type: AWS::ApplicationAutoScaling::ScalableTarget
Properties:
RoleARN: !GetAtt ECSAutoscalingRole.Arn
MaxCapacity: !Ref MaxContainers
MinCapacity: !Ref MinContainers
ResourceId: !Join [ "/", [ "service", !Ref ECSCluster, !GetAtt Service.Name]]
ScalableDimension: "ecs:service:DesiredCount"
ServiceNamespace: ecs
ECSscaleUp:
Type : "AWS::ApplicationAutoScaling::ScalingPolicy"
Properties:
PolicyName: !Join [ "-", [ !Ref environment, "ecs","scale","up"]]
PolicyType: StepScaling
ResourceId: !Join [ "/", [ "service", !Ref ECSCluster, !GetAtt Service.Name]]
ScalableDimension: "ecs:service:DesiredCount"
ServiceNamespace: ecs
StepScalingPolicyConfiguration:
AdjustmentType: "ChangeInCapacity"
Cooldown: 60
MetricAggregationType: "Average"
StepAdjustments:
-
MetricIntervalLowerBound: 0
ScalingAdjustment: 1
DependsOn: ECSAutoscalingTarget
ECSscaleDown:
Type : "AWS::ApplicationAutoScaling::ScalingPolicy"
Properties:
PolicyName: !Join [ "-", [ !Ref environment, "ecs","scale","down"]]
PolicyType: StepScaling
ResourceId: !Join [ "/", [ "service", !Ref ECSCluster, !GetAtt Service.Name]]
ScalableDimension: "ecs:service:DesiredCount"
ServiceNamespace: ecs
StepScalingPolicyConfiguration:
AdjustmentType: "ChangeInCapacity"
Cooldown: 120
MetricAggregationType: "Average"
StepAdjustments:
-
MetricIntervalLowerBound: 0
ScalingAdjustment: -1
DependsOn: ECSAutoscalingTarget
HighCPUAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- !Ref ECSscaleUp
AlarmName: !Join [ "-", [ !Ref environment, "cpu","high"]]
ComparisonOperator: "GreaterThanOrEqualToThreshold"
EvaluationPeriods: 1
MetricName: "CPUUtilization"
Namespace: "AWS/ECS"
Period: 60
Statistic: "Average"
Threshold: !Ref HighCPUThreshold
LowCPUAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- !Ref ECSscaleDown
AlarmName: !Join [ "-", [ !Ref environment, "cpu","low"]]
ComparisonOperator: "LessThanThreshold"
EvaluationPeriods: 1
MetricName: "CPUUtilization"
Namespace: "AWS/ECS"
Period: 60
Statistic: "Average"
Threshold: !Ref LowCPUThreshold