-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless.yml
267 lines (257 loc) · 9.51 KB
/
serverless.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
#
# ECS Deity creates and destroys on-demand environments through separate ECS services
#
service: ${self:custom.${self:provider.stage}.sls_prefix, ''}ecs-deity
provider:
name: aws
runtime: python2.7
stage: ${opt:stage, 'dev'}
region: ap-northeast-1
environment:
sns_arn: ${self:custom.${self:provider.stage}.sns_arn}
channel: ${self:custom.${self:provider.stage}.slack.channel}
repo_ommit: ${self:custom.${self:provider.stage}.repo_ommit, ''}
iamRoleStatements:
- Effect: "Allow"
Action:
- "lambda:InvokeFunction"
- "states:*"
- "application-autoscaling:*"
- "autoscaling:*"
- "cloudformation:*"
- "cloudwatch:*"
- "ec2:*"
- "ecs:*"
- "elasticloadbalancing:*"
- "events:*"
- "sns:Publish"
- "iam:List*"
- "iam:PassRole"
- "logs:CreateLogGroup"
- "logs:DescribeLogGroups"
- "logs:FilterLogEvents"
Resource: "*"
package:
exclude:
- .git/**
- node_module/**
- ./*.yml
- ./*.json
custom: ${file(conf.yml)}
functions:
github:
handler: github.step_function
environment:
launch_type: ${self:custom.${self:provider.stage}.launch_type, 'ec2'}
vpc_id: ${self:custom.${self:provider.stage}.vpc_id}
subnets: ${self:custom.${self:provider.stage}.subnets, ''}
security_groups: ${self:custom.${self:provider.stage}.security_groups, ''}
assign_public_ip: ${self:custom.${self:provider.stage}.assign_public_ip, 'enabled'}
cluster: ${self:custom.${self:provider.stage}.cluster}
container_name: ${self:custom.${self:provider.stage}.container_name}
container_port: ${self:custom.${self:provider.stage}.container_port}
task_def: ${self:custom.${self:provider.stage}.task_def}
lb_arn: ${self:custom.${self:provider.stage}.lb_arn}
port_range: ${self:custom.${self:provider.stage}.port_range, '8000-8100'}
health_check_protocol: ${self:custom.${self:provider.stage}.health_check.protocol, ''}
health_check_port: ${self:custom.${self:provider.stage}.health_check.port, ''}
health_check_path: ${self:custom.${self:provider.stage}.health_check.path, ''}
health_check_interval: ${self:custom.${self:provider.stage}.health_check.interval, ''}
health_check_timeout: ${self:custom.${self:provider.stage}.health_check.timeout, ''}
health_check_healthy_count: ${self:custom.${self:provider.stage}.health_check.healthy_count, ''}
health_check_unhealthy_count: ${self:custom.${self:provider.stage}.health_check.unhealthy_count, ''}
health_check_matcher: ${self:custom.${self:provider.stage}.health_check.matcher, ''}
branch_prefix: ${self:custom.${self:provider.stage}.branch_prefix, 'feature'}
create_sfn_arn: arn:aws:states:#{AWS::Region}:#{AWS::AccountId}:stateMachine:${self:service}-${self:provider.stage}-create
delete_sfn_arn: arn:aws:states:#{AWS::Region}:#{AWS::AccountId}:stateMachine:${self:service}-${self:provider.stage}-destroy
events:
- http:
path: github
method: post
chat_ops:
handler: github.chat_ops
environment:
branch_prefix: ${self:custom.${self:provider.stage}.branch_prefix, 'feature'}
gw_url: { "Fn::Join" : ["", [ "https://", { "Ref" : "ApiGatewayRestApi" }, ".execute-api.${self:provider.region}.amazonaws.com/${self:provider.stage}" ] ] }
events:
- http:
path: chat_ops
method: post
create:
handler: ecs.create
timeout: 30
service_creation_status:
handler: ecs.service_creation_status
timeout: 30
destroy:
handler: ecs.destroy
timeout: 30
stop_tasks:
handler: ecs.stop_tasks
timeout: 30
task_status:
handler: ecs.task_status
timeout: 30
delete_service:
handler: ecs.delete_service
timeout: 30
service_status:
handler: ecs.service_status
timeout: 30
delete_listener_and_tg:
handler: ecs.delete_listener_and_tg
timeout: 30
delete_alb_tg:
handler: ecs.delete_alb_tg
timeout: 30
catch_sfn_error:
handler: catch.sfn_error
environment:
channel: ${self:custom.${self:provider.stage}.slack.channel}
stepFunctions:
stateMachines:
ecs_deity_create:
name: ${self:service}-${self:provider.stage}-create
definition:
Comment: "Creates ECS service"
StartAt: ecs_create
States:
ecs_create:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-create
Catch:
- ErrorEquals:
- States.TaskFailed
- Lambda.Unknown
Next: catch_error
Next: service_exists
service_exists:
Type: Choice
Choices:
- Not:
Variable: "$.branch"
StringEquals: none
Next: wait_for_service_creation
Default: no_deployment
wait_for_service_creation:
Type: Wait
Seconds: 5
Next: service_creation_status
service_creation_status:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-service_creation_status
Next: is_service_created
is_service_created:
Type: Choice
Choices:
- Variable: "$.service_created"
BooleanEquals: true
Next: ecs_deploy
- Variable: "$.service_created"
BooleanEquals: false
Next: wait_for_service_creation
Default: wait_for_service_creation
ecs_deploy:
Type: Task # maybe pass
Resource: ${self:custom.${self:provider.stage}.deploy_lambda_arn}
Catch:
- ErrorEquals:
- States.TaskFailed
- Lambda.Unknown
Next: catch_error
End: true
no_deployment:
Type: Fail
Cause: Nothing to deploy!
Error: NothingToDeploy
catch_error:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-catch_sfn_error
End: true
ecs_deity_destroy:
name: ${self:service}-${self:provider.stage}-destroy
definition:
Comment: "Destroys ECS service"
TimeoutSeconds: 600
StartAt: ecs_destroy
States:
ecs_destroy:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-destroy
Catch:
- ErrorEquals:
- States.TaskFailed
- Lambda.Unknown
Next: catch_error
Next: stop_tasks
stop_tasks:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-stop_tasks
Catch:
- ErrorEquals:
- States.TaskFailed
- Lambda.Unknown
Next: catch_error
Next: wait_for_tasks_to_stop
wait_for_tasks_to_stop:
Type: Wait
Seconds: 5
Next: task_status
task_status:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-task_status
Next: is_task_stopped
is_task_stopped:
Type: Choice
Choices:
- Variable: "$.task"
StringEquals: stopped
Next: delete_service
- Variable: "$.task"
StringEquals: running
Next: wait_for_tasks_to_stop
Default: wait_for_tasks_to_stop
delete_service:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-delete_service
Catch:
- ErrorEquals:
- States.TaskFailed
- Lambda.Unknown
Next: catch_error
Next: wait_for_service_deletion
wait_for_service_deletion:
Type: Wait
Seconds: 3
Next: service_status
service_status:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-service_status
Next: is_service_deleted
is_service_deleted:
Type: Choice
Choices:
- Variable: "$.status"
StringEquals: INACTIVE
Next: delete_listener_and_tg
- Variable: "$.status"
StringEquals: DRAINING
Next: wait_for_service_deletion
Default: wait_for_service_deletion
delete_listener_and_tg:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-delete_listener_and_tg
Catch:
- ErrorEquals:
- States.TaskFailed
- Lambda.Unknown
Next: catch_error
End: true
catch_error:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:provider.stage}-catch_sfn_error
End: true
plugins:
- serverless-step-functions
- serverless-pseudo-parameters
- serverless-prune-plugin