-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf-stack.yml
396 lines (383 loc) · 12.3 KB
/
cf-stack.yml
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
AWSTemplateFormatVersion: "2010-09-09"
Description: "This example template shows how to ECS Fargate Cluster with CloudWatch alarms and logs / S3 connectivity. This template creates an Amazon ECS Fargate Cluster, CW Alarms, CW Logs groups, Security Group and etc. You will be billed for the AWS resources used if you create a stack using this template."
Parameters:
VPC:
Type: AWS::EC2::VPC::Id
SubnetA:
Type: AWS::EC2::Subnet::Id
SubnetB:
Type: AWS::EC2::Subnet::Id
ClusterName:
Description: Cluster name to be used
Type: String
Default: ecs-cluster
EnvironmentName:
Description: An environment name that will be prefixed to resource names
Type: String
Default: ecs-cluster
MyResourceName:
Description: An environment name that will be prefixed to resource names
Type: String
ServiceName:
Description: Service Name to be created.
Type: String
AppContainerPort:
Description: App container port
Type: Number
AutoScalingTargetValue:
Type: Number
Default: 50
LoadBalancerPort:
Type: Number
Default: 80
HealthCheckPath:
Type: String
Default: /api/
MinCapacity:
Type: Number
Default: 1
MaxCapacity:
Type: Number
Default: 2
ServiceDesiredCount:
Type: Number
Default: 1
Resources:
ECSCluster:
Type: AWS::ECS::Cluster
Properties:
CapacityProviders:
- FARGATE
ClusterName: !Join ["-", [!Ref ServiceName, Cluster]]
ClusterSettings:
- Name: containerInsights
Value: disabled
DefaultCapacityProviderStrategy:
- CapacityProvider: FARGATE
Weight: 1
Tags:
- Key: env
Value: !Ref EnvironmentName
ECSTaskS3ManagedPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: "ECS Task S3 Policy to Read/Write to/from S3"
ManagedPolicyName:
{
"Fn::Join":
[
"-",
["s3", { "Ref": "AWS::Region" }, { "Ref": "MyResourceName" }],
],
}
Path: /
PolicyDocument: |
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::fluent-bit-ecs-test"
]
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::fluent-bit-ecs-test/*"
]
}
]
}
ECSTaskCloudWatchManagedPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: "ECS Task Cloud Watch Policy to create and put log groups and log streams"
ManagedPolicyName:
{
"Fn::Join":
[
"-",
["CW", { "Ref": "AWS::Region" }, { "Ref": "MyResourceName" }],
],
}
Path: /
PolicyDocument: |
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:DescribeLogStreams",
"logs:CreateLogGroup"
],
"Resource": "arn:aws:logs:eu-west-1:035864429412:log-group:*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": "logs:PutLogEvents",
"Resource": "arn:aws:logs:eu-west-1:035864429412:log-group:*:log-stream:*"
}
]
}
AutoScalingRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Join ["-", [!Ref ServiceName, AutoScalingRole]]
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Action: "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceAutoscaleRole"
ExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument: |
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
Description: "ECS Task Execution ROle Policy"
ManagedPolicyArns:
- !Ref ECSTaskS3ManagedPolicy
- !Ref ECSTaskCloudWatchManagedPolicy
Path: /
Policies:
- PolicyName: !Sub ecs-service-${AWS::StackName}
PolicyDocument:
{
"Version": "2012-10-17",
"Statement":
[
{
"Effect": "Allow",
"Action":
[
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"logs:CreateLogStream",
"logs:PutLogEvents",
],
"Resource": "*",
},
],
}
RoleName: !Sub ecs-service-${AWS::StackName}
Tags:
- Key: env
Value: !Ref EnvironmentName
TaskDefinition:
Type: AWS::ECS::TaskDefinition
DependsOn:
- CloudWatchLogsGroup
Properties:
RequiresCompatibilities:
- "FARGATE"
ContainerDefinitions:
- Name: !Ref ServiceName
Essential: true
Image: "rv0718/fb-app:3"
# Cpu: 512
PortMappings:
- ContainerPort: !Ref AppContainerPort
HostPort: !Ref AppContainerPort
Protocol: "tcp"
# Memory: 256
Environment:
- Name: LOG_FILE_PATH
Value: "/usr/local/tomcat/logs"
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Ref CloudWatchLogsGroup
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: "ecs"
Cpu: 512
ExecutionRoleArn: !Ref ExecutionRole
Family: !Join ["-", [!Ref ServiceName, TaskDefinition]]
Memory: 1024
NetworkMode: "awsvpc"
Tags:
- Key: env
Value: !Ref EnvironmentName
TaskRoleArn: !Ref ExecutionRole
Service:
Type: AWS::ECS::Service
# This dependency is needed so that the load balancer is setup correctly in time
DependsOn:
- ListenerHTTP
Properties:
ServiceName: !Ref ServiceName
Cluster: !Ref ECSCluster
TaskDefinition: !Ref TaskDefinition
DeploymentConfiguration:
MinimumHealthyPercent: 100
MaximumPercent: 200
DesiredCount: !Ref ServiceDesiredCount
# This may need to be adjusted if the container takes a while to start up
HealthCheckGracePeriodSeconds: 30
LaunchType: FARGATE
NetworkConfiguration:
AwsvpcConfiguration:
# change to DISABLED if you're using private subnets that have access to a NAT gateway
AssignPublicIp: ENABLED
Subnets:
- !Ref SubnetA
- !Ref SubnetB
SecurityGroups:
- !Ref ContainerSecurityGroup
LoadBalancers:
- ContainerName: !Ref ServiceName
ContainerPort: !Ref AppContainerPort
TargetGroupArn: !Ref TargetGroup
CloudWatchLogsGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join ['', [/ecs/, !Ref ServiceName, TaskDefinition,!Ref AWS::StackName]]
RetentionInDays: 365
ContainerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: !Join ["-", [!Ref ServiceName, ContainerSecurityGroup]]
GroupName: !Join ["-", [!Ref ServiceName, ContainerSecurityGroup]]
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: !Ref AppContainerPort
ToPort: !Ref AppContainerPort
SourceSecurityGroupId: !Ref LoadBalancerSecurityGroup
SecurityGroupEgress:
- IpProtocol: "-1"
FromPort: 0
ToPort: 0
CidrIp: 0.0.0.0/0
LoadBalancerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription:
!Join ["-", [!Ref ServiceName, LoadBalancerSecurityGroup]]
GroupName: !Join ["-", [!Ref ServiceName, LoadBalancerSecurityGroup]]
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: !Ref LoadBalancerPort
ToPort: !Ref LoadBalancerPort
CidrIp: 0.0.0.0/0
SecurityGroupEgress:
- IpProtocol: "-1"
FromPort: 0
ToPort: 0
CidrIp: "0.0.0.0/0"
TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 30
# will look for a 200 status code by default unless specified otherwise
HealthCheckPath: !Ref HealthCheckPath
HealthCheckTimeoutSeconds: 15
UnhealthyThresholdCount: 2
HealthyThresholdCount: 3
# Matcher:
# - HttpCode: '200'
Name: !Join ["-", [!Ref ServiceName, TargetGroup]]
Port: !Ref AppContainerPort
Protocol: HTTP
TargetGroupAttributes:
- Key: deregistration_delay.timeout_seconds
Value: 60 # default is 300
# TargetType ip defines that it is a fargate type cluster/target
TargetType: ip
VpcId: !Ref VPC
ListenerHTTP:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- TargetGroupArn: !Ref TargetGroup
Type: "forward"
LoadBalancerArn: !Ref LoadBalancer
Port: !Ref LoadBalancerPort
Protocol: HTTP
LoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
LoadBalancerAttributes:
# this is the default, but is specified here in case it needs to be changed
- Key: idle_timeout.timeout_seconds
Value: 60
- Key: deletion_protection.enabled
Value: false
Name: !Join ["-", [!Ref ServiceName, LoadBalancer]]
Type: "application"
# "internal" is also an option
Scheme: internet-facing
SecurityGroups:
- !Ref LoadBalancerSecurityGroup
Subnets:
- !Ref SubnetA
- !Ref SubnetB
AutoScalingTarget:
Type: AWS::ApplicationAutoScaling::ScalableTarget
Properties:
MinCapacity: !Ref MinCapacity
MaxCapacity: !Ref MaxCapacity
ResourceId: !Join ["/", [service, !Ref ECSCluster, !GetAtt Service.Name]]
ScalableDimension: ecs:service:DesiredCount
ServiceNamespace: ecs
# "The Amazon Resource Name (ARN) of an AWS Identity and Access Management (IAM) role that allows Application Auto Scaling to modify your scalable target."
RoleARN: !GetAtt AutoScalingRole.Arn
AutoScalingPolicy:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
Properties:
PolicyName: !Join ["-", [!Ref ServiceName, AutoScalingPolicy]]
PolicyType: TargetTrackingScaling
ScalingTargetId: !Ref AutoScalingTarget
TargetTrackingScalingPolicyConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ECSServiceAverageCPUUtilization
ScaleInCooldown: 10
ScaleOutCooldown: 10
# Keep things at or lower than 50% CPU utilization, for example
TargetValue: !Ref AutoScalingTargetValue
Outputs:
Cluster:
Description: A reference to the ECS cluster
Value: !Ref ECSCluster
S3ManagedPolicy:
Description: A reference to the S3 Managed Policy
Value: !Ref ECSTaskS3ManagedPolicy
CWManagedPolicy:
Description: A reference to the Cloud Watch Managed Policy
Value: !Ref ECSTaskCloudWatchManagedPolicy
TaskDefinition:
Description: A reference to the TaskDefinition
Value: !Ref TaskDefinition
CloudWatchLogsGroup:
Description: A reference to the TaskDefinition
Value: !Ref CloudWatchLogsGroup
Endpoint:
Description: Endpoint
Value: !Join ["", ["http://", !GetAtt LoadBalancer.DNSName]]