-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsms-poc-ecs-service.yml
295 lines (295 loc) · 8.78 KB
/
awsms-poc-ecs-service.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
AWSTemplateFormatVersion: 2010-09-09
Description: >-
Create fargate service, task definition, target group, log group, asg for ECS
Cluster
Parameters:
EnvironmentName:
Type: String
Description: Environment Name
AllowedValues:
- development
- staging
- sandbox
Default: sandbox
ServiceName:
Type: String
Default: test-awsms-poc
Description: Name for the service
ImageUrl:
Type: String
Default: >-
160357565307.dkr.ecr.us-west-2.amazonaws.com/awsms-poc-springboot:awsms-poc-springboot
Description: The url of a docker image/ ecr repo that contains the application process
ContainerPort:
Type: Number
Default: 5030
Description: What port number the application inside the docker container is binding to
ContainerCpu:
Type: Number
Default: 1024
Description: How much CPU to give the container. 1024 is 1 CPU
ContainerMemory:
Type: Number
Default: 2048
Description: How much memory in MB to give the container
Path:
Type: String
Default: '*'
Description: >-
A path on the load balancer that this service should be connected to. Use
* to send all load balancer traffic to this service.
Priority:
Type: Number
Default: 1
Description: The priority for the routing rule added to the load balancer.
DesiredCount:
Type: Number
Default: 2
Description: How many copies of the service task to run
Role:
Type: String
Default: ''
Description: >-
(Optional) An IAM role to give the service's containers if the code within
needs to access other AWS resources like S3 buckets, DynamoDB tables, etc
Conditions:
HasCustomRole: !Not
- !Equals
- !Ref Role
- ''
Resources:
LogGroup:
Type: 'AWS::Logs::LogGroup'
Properties:
LogGroupName: !Sub '${EnvironmentName}-service-${ServiceName}'
DockerLogGroup:
Type: 'AWS::Logs::LogGroup'
Properties:
LogGroupName: !Sub '/aws/docker/${EnvironmentName}-${ServiceName}'
TaskDefinition:
Type: 'AWS::ECS::TaskDefinition'
Properties:
Family: !Ref ServiceName
Cpu: !Ref ContainerCpu
Memory: !Ref ContainerMemory
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
ExecutionRoleArn:
'Fn::ImportValue': !Sub '${EnvironmentName}:ECSTaskExecutionRole'
TaskRoleArn:
'Fn::If':
- HasCustomRole
- !Ref Role
- !Ref 'AWS::NoValue'
ContainerDefinitions:
- Name: !Ref ServiceName
Cpu: !Ref ContainerCpu
Memory: !Ref ContainerMemory
Image: !Ref ImageUrl
PortMappings:
- ContainerPort: !Ref ContainerPort
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Sub '${EnvironmentName}-service-${ServiceName}'
awslogs-region: !Ref 'AWS::Region'
awslogs-stream-prefix: !Ref ServiceName
Service:
Type: 'AWS::ECS::Service'
DependsOn: LoadBalancerRule
Properties:
ServiceName: !Ref ServiceName
Cluster:
'Fn::ImportValue': !Sub '${EnvironmentName}:ClusterName'
LaunchType: FARGATE
DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 75
DesiredCount: !Ref DesiredCount
HealthCheckGracePeriodSeconds: 60
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: ENABLED
SecurityGroups:
- 'Fn::ImportValue': !Sub '${EnvironmentName}:ContainerSecurityGroup'
Subnets:
- 'Fn::ImportValue': !Sub '${EnvironmentName}:PrivateSubnetOne'
- 'Fn::ImportValue': !Sub '${EnvironmentName}:PrivateSubnetTwo'
TaskDefinition: !Ref TaskDefinition
LoadBalancers:
- ContainerName: !Ref ServiceName
ContainerPort: !Ref ContainerPort
TargetGroupArn: !Ref TargetGroup
TargetGroup:
Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'
Properties:
HealthCheckIntervalSeconds: 20
HealthCheckPath: /actuator/health
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 10
HealthyThresholdCount: 10
TargetType: ip
Name: !Ref ServiceName
Port: !Ref ContainerPort
Protocol: HTTP
UnhealthyThresholdCount: 10
VpcId:
'Fn::ImportValue': !Sub '${EnvironmentName}:VpcId'
LoadBalancerRule:
Type: 'AWS::ElasticLoadBalancingV2::ListenerRule'
Properties:
Actions:
- TargetGroupArn: !Ref TargetGroup
Type: forward
Conditions:
- Field: path-pattern
Values:
- !Ref Path
ListenerArn:
'Fn::ImportValue': !Sub '${EnvironmentName}:PublicListener'
Priority: !Ref Priority
ScalableTarget:
Type: 'AWS::ApplicationAutoScaling::ScalableTarget'
DependsOn: Service
Properties:
ServiceNamespace: ecs
ScalableDimension: 'ecs:service:DesiredCount'
ResourceId:
'Fn::Join':
- /
- - service
- 'Fn::ImportValue': !Sub '${EnvironmentName}:ClusterName'
- !Ref ServiceName
MinCapacity: 1
MaxCapacity: 2
RoleARN:
'Fn::ImportValue': !Sub '${EnvironmentName}:AutoscalingRole'
ScaleDownPolicy:
Type: 'AWS::ApplicationAutoScaling::ScalingPolicy'
DependsOn: ScalableTarget
Properties:
PolicyName:
'Fn::Join':
- /
- - scale
- !Ref EnvironmentName
- !Ref ServiceName
- down
PolicyType: StepScaling
ResourceId:
'Fn::Join':
- /
- - service
- 'Fn::ImportValue': !Sub '${EnvironmentName}:ClusterName'
- !Ref ServiceName
ScalableDimension: 'ecs:service:DesiredCount'
ServiceNamespace: ecs
StepScalingPolicyConfiguration:
AdjustmentType: ChangeInCapacity
StepAdjustments:
- MetricIntervalUpperBound: 0
ScalingAdjustment: -1
MetricAggregationType: Average
Cooldown: 60
ScaleUpPolicy:
Type: 'AWS::ApplicationAutoScaling::ScalingPolicy'
DependsOn: ScalableTarget
Properties:
PolicyName:
'Fn::Join':
- /
- - scale
- !Ref EnvironmentName
- !Ref ServiceName
- up
PolicyType: StepScaling
ResourceId:
'Fn::Join':
- /
- - service
- 'Fn::ImportValue': !Sub '${EnvironmentName}:ClusterName'
- !Ref ServiceName
ScalableDimension: 'ecs:service:DesiredCount'
ServiceNamespace: ecs
StepScalingPolicyConfiguration:
AdjustmentType: ChangeInCapacity
StepAdjustments:
- MetricIntervalLowerBound: 0
MetricIntervalUpperBound: 15
ScalingAdjustment: 1
- MetricIntervalLowerBound: 15
MetricIntervalUpperBound: 25
ScalingAdjustment: 2
- MetricIntervalLowerBound: 25
ScalingAdjustment: 3
MetricAggregationType: Average
Cooldown: 60
LowCpuUsageAlarm:
Type: 'AWS::CloudWatch::Alarm'
Properties:
AlarmName:
'Fn::Join':
- '-'
- - low-cpu
- !Ref EnvironmentName
- !Ref ServiceName
AlarmDescription:
'Fn::Join':
- ' '
- - Low CPU utilization for service
- !Ref ServiceName
- in environment
- !Ref EnvironmentName
MetricName: CPUUtilization
Namespace: AWS/ECS
Dimensions:
- Name: ServiceName
Value: !Ref ServiceName
- Name: ClusterName
Value:
'Fn::ImportValue': !Sub '${EnvironmentName}:ClusterName'
Statistic: Average
Period: 60
EvaluationPeriods: 1
Threshold: 20
ComparisonOperator: LessThanOrEqualToThreshold
AlarmActions:
- !Ref ScaleDownPolicy
HighCpuUsageAlarm:
Type: 'AWS::CloudWatch::Alarm'
Properties:
AlarmName:
'Fn::Join':
- '-'
- - high-cpu
- !Ref EnvironmentName
- !Ref ServiceName
AlarmDescription:
'Fn::Join':
- ' '
- - High CPU utilization for service
- !Ref ServiceName
- in environment
- !Ref EnvironmentName
MetricName: CPUUtilization
Namespace: AWS/ECS
Dimensions:
- Name: ServiceName
Value: !Ref ServiceName
- Name: ClusterName
Value:
'Fn::ImportValue': !Sub '${EnvironmentName}:ClusterName'
Statistic: Average
Period: 60
EvaluationPeriods: 1
Threshold: 70
ComparisonOperator: GreaterThanOrEqualToThreshold
AlarmActions:
- !Ref ScaleUpPolicy
Outputs:
ServiceName:
Description: The name of the ECS Service
Value: !Ref Service
Export:
Name: !Sub '${EnvironmentName}:ServiceName'