forked from nathanpeck/nodejs-aws-workshop
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservice.yml
296 lines (285 loc) · 8.83 KB
/
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
296
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
StackName:
Type: String
Default: ""
Description: The environment name
ServiceName:
Type: String
Description: A name for the service
ImageUrl:
Type: String
Description: The url of a docker image that is the application for this service
TaskRole:
Type: String
Default: ""
Description: A task role to grant to the service container
ListenerArn:
Type: String
Default: ""
Description: The load balancer listener to add the service to
Path:
Type: String
Default: ""
Description: A path on the load balancer that this service should be connected to
Priority:
Type: Number
Default: 0
Description: The priority of the rule that is added to the load balancer
DesiredCount:
Type: Number
Default: 2
Description: How many copies of the service task to run
# Determine whether or not to create a service that is attached to a load balancer
Conditions:
AttachedToLoadBalancer: !Not [ !Equals [!Ref 'Path', ''] ]
Resources:
# A log group for storing the container logs for this service
LogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join ['-', [!Ref 'StackName', 'service', !Ref 'ServiceName']]
# The task definition that describes how to run the docker container
TaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: !Ref 'ServiceName'
TaskRoleArn: !Ref TaskRole
ContainerDefinitions:
- Name: !Ref 'ServiceName'
Cpu: 256
Memory: 256
Image: !Ref 'ImageUrl'
PortMappings:
- ContainerPort: 8081
HostPort: 0
Environment:
- Name: 'EXTERNAL_URL'
Value:
Fn::ImportValue:
!Join [':', [!Ref 'StackName', 'ExternalUrl']]
- Name: 'NODE_ENV'
Value: 'production'
- Name: 'REGION'
Value: !Ref 'AWS::Region'
LogConfiguration:
LogDriver: 'awslogs'
Options:
awslogs-group: !Join ['-', [!Ref 'StackName', 'service', !Ref 'ServiceName']]
awslogs-region: !Ref 'AWS::Region'
awslogs-stream-prefix: !Ref 'ServiceName'
# Create an ECS service that isn't attached to a loadbalancer
Service:
Type: AWS::ECS::Service
DependsOn:
- LogGroup
Properties:
Cluster:
Fn::ImportValue:
!Join [':', [!Ref 'StackName', 'ClusterName']]
DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 75
PlacementStrategies:
- Type: spread
Field: attribute:ecs.availability-zone
- Type: binpack
Field: cpu
DesiredCount: !Ref 'DesiredCount'
ServiceName:
# The following statements always produces the same value for
# the service name, but the select exists as a hack to conditionally
# introduce a dependency on the load balancer rule if the service is
# attached to a load balancer. This hack is necessary because the service
# depends on the load balancer rule only if attached to a load balancer
# but the normal DependsOn property only accepts a static value, no
# conditionals possible.
Fn::If:
- 'AttachedToLoadBalancer'
- Fn::Select:
- 0
- [!Ref 'ServiceName', !Ref 'LoadBalancerRule']
- !Ref 'ServiceName'
TaskDefinition: !Ref 'TaskDefinition'
Role:
Fn::If:
- 'AttachedToLoadBalancer'
- Fn::ImportValue:
!Join [':', [!Ref 'StackName', 'ECSRole']]
- !Ref "AWS::NoValue"
LoadBalancers:
Fn::If:
- 'AttachedToLoadBalancer'
- - ContainerName: !Ref 'ServiceName'
ContainerPort: 8081
TargetGroupArn: !Ref 'TargetGroup'
- []
# Create a target group for tracking this task across the cluster
TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Condition: AttachedToLoadBalancer
Properties:
HealthCheckIntervalSeconds: 6
HealthCheckPath: /
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 2
Name: !Ref 'ServiceName'
Port: 80
Protocol: HTTP
UnhealthyThresholdCount: 2
VpcId:
Fn::ImportValue:
!Join [':', [!Ref 'StackName', 'VPCId']]
# Create a rule on the load balancer for routing traffic to the target group
LoadBalancerRule:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Condition: AttachedToLoadBalancer
Properties:
Actions:
- TargetGroupArn: !Ref 'TargetGroup'
Type: 'forward'
Conditions:
- Field: path-pattern
Values: [!Ref 'Path']
ListenerArn: !Ref 'ListenerArn'
Priority: !Ref 'Priority'
# Enable autoscaling for this service
ScalableTarget:
Type: AWS::ApplicationAutoScaling::ScalableTarget
DependsOn: Service
Properties:
ServiceNamespace: 'ecs'
ScalableDimension: 'ecs:service:DesiredCount'
ResourceId:
Fn::Join:
- '/'
- - service
- Fn::ImportValue: !Join [':', [!Ref 'StackName', 'ClusterName']]
- !Ref 'ServiceName'
MinCapacity: 2
MaxCapacity: 10
RoleARN:
Fn::ImportValue:
!Join [':', [!Ref 'StackName', 'AutoscalingRole']]
# Create scaling policies for the service
ScaleDownPolicy:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
DependsOn: ScalableTarget
Properties:
PolicyName:
Fn::Join:
- '/'
- - scale
- !Ref 'StackName'
- !Ref 'ServiceName'
- down
PolicyType: StepScaling
ResourceId:
Fn::Join:
- '/'
- - service
- Fn::ImportValue: !Join [':', [!Ref 'StackName', 'ClusterName']]
- !Ref 'ServiceName'
ScalableDimension: 'ecs:service:DesiredCount'
ServiceNamespace: 'ecs'
StepScalingPolicyConfiguration:
AdjustmentType: 'ChangeInCapacity'
StepAdjustments:
- MetricIntervalLowerBound: 0
ScalingAdjustment: -1
MetricAggregationType: 'Average'
Cooldown: 60
ScaleUpPolicy:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
DependsOn: ScalableTarget
Properties:
PolicyName:
Fn::Join:
- '/'
- - scale
- !Ref 'StackName'
- !Ref 'ServiceName'
- up
PolicyType: StepScaling
ResourceId:
Fn::Join:
- '/'
- - service
- Fn::ImportValue: !Join [':', [!Ref 'StackName', 'ClusterName']]
- !Ref 'ServiceName'
ScalableDimension: 'ecs:service:DesiredCount'
ServiceNamespace: 'ecs'
StepScalingPolicyConfiguration:
AdjustmentType: 'ChangeInCapacity'
StepAdjustments:
- MetricIntervalLowerBound: 0
MetricIntervalUpperBound: 20
ScalingAdjustment: 1
- MetricIntervalLowerBound: 20
ScalingAdjustment: 2
MetricAggregationType: 'Average'
Cooldown: 60
# Create alarms to trigger these policies
LowCpuUsageAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName:
Fn::Join:
- '-'
- - low-cpu
- !Ref 'StackName'
- !Ref 'ServiceName'
AlarmDescription:
Fn::Join:
- ' '
- - "Low CPU utilization for service"
- !Ref 'ServiceName'
- "in stack"
- !Ref 'StackName'
MetricName: CPUUtilization
Namespace: AWS/ECS
Dimensions:
- Name: ServiceName
Value: !Ref 'ServiceName'
- Name: ClusterName
Value:
Fn::ImportValue: !Join [':', [!Ref 'StackName', '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 'StackName'
- !Ref 'ServiceName'
AlarmDescription:
Fn::Join:
- ' '
- - "High CPU utilization for service"
- !Ref 'ServiceName'
- "in stack"
- !Ref 'StackName'
MetricName: CPUUtilization
Namespace: AWS/ECS
Dimensions:
- Name: ServiceName
Value: !Ref 'ServiceName'
- Name: ClusterName
Value:
Fn::ImportValue: !Join [':', [!Ref 'StackName', 'ClusterName']]
Statistic: Average
Period: 60
EvaluationPeriods: 1
Threshold: 70
ComparisonOperator: GreaterThanOrEqualToThreshold
AlarmActions:
- !Ref ScaleUpPolicy