-
Notifications
You must be signed in to change notification settings - Fork 1
/
serverless.yml
390 lines (376 loc) · 13.5 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
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
#------------------------------------------------
# Serverless Configuration.
#------------------------------------------------
service: lemon-clusters-api
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"
plugins:
- serverless-offline
- serverless-aws-documentation
- serverless-pseudo-parameters
- '@cruglobal/serverless-merge-config'
#------------------------------------------------
# Custome: 초기 환경 설정 로딩을 위한 핵심 설정.
# - config.js 에서 profile별 core 정보를 얻음.
#------------------------------------------------
custom:
CORE: # Core Configuration.
profile: ${opt:profile, 'none'} # run option `--profile`
region: ${opt:region, 'ap-northeast-2'} # run option `--region` `-r`
stage: ${opt:stage, 'dev'} # run option `--stage` `-s`
env: ${opt:env, './env/none.yml'} # run option `--env`
CONF: ${file(./env/config.js):CONF} # `config.js` 에서 CONF() 함수 호출하여 가져옴.
#------------------------------------------------
# serverless-offline
#------------------------------------------------
serverless-offline:
port: 8121
#------------------------------------------------
# Documentation
# - https://github.com/9cookies/serverless-aws-documentation
#------------------------------------------------
documentation: ${file(./swagger/documentation.yml):documentation}
#------------------------------------------------
# Dynamo (TableName)
#------------------------------------------------
tableName:
local: ClustersTest
dev: ClustersTest
prod: Clusters
#------------------------------------------------
# SNS Registration: ${self:custom.topicName.${self:custom.CORE.stage}}
#------------------------------------------------
topicName:
local: lemon-clusters-sns-dev
dev: lemon-clusters-sns-dev
prod: lemon-clusters-sns
#------------------------------------------------
# SQS Registration:
#------------------------------------------------
queueName:
local: lemon-clusters-sqs-dev
dev: lemon-clusters-sqs-dev
prod: lemon-clusters-sqs
#==============================================================
# Provider
#--------------------------------------------------------------
provider:
name: aws
profile: ${opt:profile, 'none'}
stage: ${opt:stage, 'dev'}
# Lambda Runtime Environment
memorySize: 512
timeout: 180
cfLogs: true
runtime: ${self:custom.CONF.${self:custom.CORE.profile}.runtime, 'nodejs6.10'}
region: ${self:custom.CONF.${self:custom.CORE.profile}.region, self:custom.CORE.region}
# VPC Configuration for Lambda.
vpc:
securityGroupIds: ${self:custom.CONF.${self:custom.CORE.profile}.securityGroupIds}
subnetIds: ${self:custom.CONF.${self:custom.CORE.profile}.subnetIds}
# Lambda function's IAM Role here
iamRoleStatements:
#NOTE! - remove할때 lambda ENI Interface를 제거하지 못해서 블럭당하고 있을때, EC2 콘솔에서 강제 지우기 하면 됨!.. (VPC 안쓰면 관계 없음!)
- Effect: Allow
Action:
- cloudwatch:PutMetricData
- lambda:InvokeFunction
- ec2:CreateNetworkInterface
- ec2:DescribeNetworkInterfaces
- ec2:AttachNetworkInterface
- ec2:DescribeNetworkInterfaces
- ec2:DeleteNetworkInterface
- ec2:DetachNetworkInterface
- ec2:ModifyNetworkInterfaceAttribute
- ec2:ResetNetworkInterfaceAttribute
Resource: "*"
# KMS
- Effect: Allow
Action:
- kms:Decrypt
- kms:Encrypt
Resource: "arn:aws:kms:${self:provider.region}:*:key/${self:custom.kmsName.${self:custom.CORE.stage}, ''}"
# Lambda + SNS + SQS
- Effect: "Allow"
Action:
- lambda:InvokeFunction
- sns:GetEndpointAttributes
- sns:Publish
- sqs:SendMessage
Resource: "*"
# SQS
- Effect: "Allow"
Action:
- sqs:DeleteMessage
- sqs:ReceiveMessage
- sqs:GetQueueAttributes
Resource: "arn:aws:sqs:${opt:region, self:provider.region}:*:${self:custom.queueName.${self:custom.CORE.stage}}"
# DynamoDB (ModelTable)
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:GetRecords
- dynamodb:GetShardIterator
- dynamodb:DescribeTable
- dynamodb:DescribeStream
- dynamodb:ListStreams
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:custom.tableName.${self:custom.CORE.stage}}"
# [WSS] send message vis web-socket.
- Effect: Allow
Action:
- "execute-api:Invoke"
- "execute-api:ManageConnections"
Resource:
- "arn:aws:execute-api:*:*:**/@connections/*"
# [WSS] custom routes are selected by the value of the action property in the body
websocketApiRouteSelectionExpression: $request.body.action
# -------------------------------------------------------
# Environment variables
# `$ npm install --save-dev @cruglobal/serverless-merge-config`
# -------------------------------------------------------
environment:
DEFAULT_REGION: '${self:provider.region}'
REPORT_ERROR_ARN: 'arn:aws:sns:${self:provider.region}:#{AWS::AccountId}:lemon-hello-sns'
MY_DYNAMO_TABLE: '${self:custom.tableName.${self:custom.CORE.stage}}'
MY_SQS_ENDPOINT: 'https://sqs.${self:provider.region}.amazonaws.com/#{AWS::AccountId}/${self:custom.queueName.${self:custom.CORE.stage}}' # see `sqs-service`
MY_SNS_ENDPOINT: 'arn:aws:sns:${self:provider.region}:#{AWS::AccountId}:${self:custom.topicName.${self:custom.CORE.stage}}' # see `sns-service`
$<<: ${file(./env/${self:custom.CONF.${self:custom.CORE.profile}.env}):${self:custom.CORE.stage}}
#==============================================================
# Functions
#--------------------------------------------------------------
functions:
#INFO - use seperated function for so many dynamo-stream.
backend:
handler: handler.lambda # use core `lambda-handler`.
timeout: 300 # max timeout. (= 5 min)
events:
# -------------------------------------------------------
# Shared DynamoStream Service.
- stream:
type: dynamodb
batchSize: 1
startingPosition: LATEST
arn:
Fn::GetAtt:
- MyTable
- StreamArn
#WARN - must be `lambda` to support `lemon-protocol`.
lambda:
handler: handler.lambda # use core `lambda-handler`.
timeout: 300 # max timeout. (= 5 min)
events:
# -------------------------------------------------------
# Shared SNS Service.
- sns: 'arn:aws:sns:${self:provider.region}:#{AWS::AccountId}:${self:custom.topicName.${self:custom.CORE.stage}}'
# -------------------------------------------------------
# Shared SQS Service.
- sqs:
arn:
Fn::GetAtt:
- MyQueue
- Arn
# the maximum number of SQS messages of AWS to send on a single trigger. (default: 10)
batchSize: 1
# -------------------------------------------------------
# SCHEDULE: CRON (5MIN)
- schedule:
name: lemon-clusters-${self:custom.CORE.stage}-5min
description: 'run every 5 minute to trigger batch'
rate: 'cron(0/5 * * * ? *)'
enabled: true
input:
cron:
name: 5MIN
stage: ${self:custom.CORE.stage}
# -------------------------------------------------------
# WSS: WEB-SOCKET
- websocket:
route: $connect
- websocket:
route: $disconnect
- websocket:
route: $default
# authorizer: auth
# authorizer:
# name: auth
# identitySource:
# - 'route.request.header.Auth'
# - 'route.request.querystring.Auth'
- websocket:
route: echo # sample route by routeKey. echo request.
# -------------------------------------------------------
# REST API : /
- http:
path: /
method: get
cors: true
documentation:
summary: "show service name and version"
# -------------------------------------------------------
# REST API : /hello
- http:
path: /hello
method: get
cors: true
documentation:
summary: "search hello"
- http:
path: /hello/{id}
method: get
cors: true
documentation:
summary: "read hello"
- http:
path: /hello/{id}/{cmd}
method: get
cors: true
documentation:
summary: "run action per hello"
# -------------------------------------------------------
# REST API : `clusters`
# WARN - be ware of production deployment (public open).
- http:
path: /clusters/{id}/{cmd}
method: get
cors: true
documentation:
summary: "for management purpose."
# -------------------------------------------------------
# REST API : General Controller.
- http:
path: /{type}
method: get
cors: true
authorizer: aws_iam
documentation:
summary: |
검색하여 목록을 가져온다. 검색항목은 QueryString으로 지정할 수 있음.
- http:
path: /{type}/{id}
method: get
cors: true
authorizer: aws_iam
documentation:
summary: |
해당 항목의 상세 내용을 가져온다. 만일 없을 경우 404 에러 발생.
- http:
path: /{type}/{id}/{cmd}
method: get
cors: true
authorizer: aws_iam
documentation:
summary: |
추가적인 :cmd 를 수행한다.
- http:
path: /{type}/{id}
method: put
cors: true
authorizer: aws_iam
documentation:
summary: |
특정한 필드만 업데이트 가능하며, incremental 업데이트 기능 가능함.
- http:
path: /{type}/{id}/{cmd}
method: put
cors: true
authorizer: aws_iam
documentation:
summary: |
특정한 필드만 업데이트 가능하며, incremental 업데이트 기능 가능함.
- http:
path: /{type}/{id}
method: post
cors: true
authorizer: aws_iam
documentation:
summary: |
:id 항목에 신규 데이터를 생성하거나, 기존 데이터를 업데이트한다.
- http:
path: /{type}/{id}/{cmd}
method: post
cors: true
authorizer: aws_iam
documentation:
summary: |
:cmd 추가적인 command 를 수행할 수 있다.
- http:
path: /{type}/{id}
method: delete
cors: true
authorizer: aws_iam
documentation:
summary: |
:id 노드를 삭제 처리함. (단, prepared/created 상태 노드만)
- http:
path: /{type}/{id}/{cmd}
method: delete
cors: true
authorizer: aws_iam
documentation:
summary: |
:id 노드를 삭제 처리함. (단, prepared/created 상태 노드만)
#--------------------------------------------------------------
# ! INCLUDE ONLY DISTRIBUTIONS !
#--------------------------------------------------------------
package:
exclude:
- ./**
- '!node_modules/**'
include:
- handler.js
- package.json
- dist/**
- data/**
excludeDevDependencies: true
#==============================================================
# Resources
# ## 리소스 생성.
# - 리소스를 deploy 시점에 생성할 수 있다
# - 주위! 테스트 목적으로 자동 생성하도록 함.
# - 운영시, 이 항목은 안쓰일듯.
#--------------------------------------------------------------
resources:
# 리소스 지정.
Resources:
# -------------------------------
# This SNS Topic
# -------------------------------
MyTopic:
Type: 'AWS::SNS::Topic'
Properties:
TopicName: '${self:custom.topicName.${self:custom.CORE.stage}}'
# -------------------------------
# SQS Queue
# -------------------------------
MyQueue:
Type: 'AWS::SQS::Queue'
Properties:
QueueName: '${self:custom.queueName.${self:custom.CORE.stage}}'
MessageRetentionPeriod: 1800 # 30 mins
VisibilityTimeout: 300 # 5 mins
MaximumMessageSize: 4096 # 4 Kb
DelaySeconds: 3 # 3 seconds
ReceiveMessageWaitTimeSeconds: 20 # 20 seconds
# -------------------------------
# Dynamo Table
# -------------------------------
MyTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
TableName: '${self:custom.tableName.${self:custom.CORE.stage}}'
AttributeDefinitions:
- AttributeName: '_id'
AttributeType: S
KeySchema:
- AttributeName: '_id'
KeyType: HASH
BillingMode: PAY_PER_REQUEST
StreamSpecification:
StreamViewType: "NEW_AND_OLD_IMAGES"