forked from lemoncloud-io/lemon-hello-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
359 lines (345 loc) · 11.9 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
#------------------------------------------------
# Serverless Configuration.
#------------------------------------------------
service: eureka-hello-api
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
frameworkVersion: "3"
plugins:
- serverless-offline
- serverless-aws-documentation
- serverless-prune-plugin
- '@cruglobal/serverless-merge-config'
#------------------------------------------------
# Custome: 초기 환경 설정 로딩을 위한 핵심 설정.
# - config.js 에서 profile별 core 정보를 얻음.
#------------------------------------------------
custom:
CORE: # Core Configuration.
profile: ${param:profile, 'none'} # run option `--profile`
region: ${opt:region, 'ap-northeast-2'} # run option `--region` `-r`
stage: ${opt:stage, 'dev'} # run option `--stage` `-s`
env: ${param:env, './env/none.yml'} # run option `--env`
CONF: ${file(./env/config.js):CONF} # merge with `config.js` by profile.
#------------------------------------------------
# serverless-offline
#------------------------------------------------
serverless-offline:
port: 8000
#------------------------------------------------
# Documentation
# - https://github.com/9cookies/serverless-aws-documentation
#------------------------------------------------
documentation: ${file(./swagger/documentation.yml):documentation}
#------------------------------------------------
# Dynamo: (MovesTest)
#------------------------------------------------
tableName:
local: eureka-hello-table-dev
dev: eureka-hello-table-dev
prod: eureka-hello-table
#------------------------------------------------
# SQS Queue
#------------------------------------------------
queueName:
local: eureka-hello-sqs-dev
dev: eureka-hello-sqs-dev
prod: eureka-hello-sqs
#------------------------------------------------
# SNS Registration
#------------------------------------------------
topicName:
local: eureka-hello-sns-dev
dev: eureka-hello-sns-dev
prod: eureka-hello-sns
topicArn:
Fn::Join:
- ''
- - 'arn:aws:sns:'
- Ref: AWS::Region
- ':'
- Ref: AWS::AccountId
- ':'
- Fn::GetAtt:
- MyInputTopic
- TopicName
#------------------------------------------------
# pruning old versions
#------------------------------------------------
prune:
automatic: true
number: 3
#------------------------------------------------
# CORS Default (use like `cors: ${self:custom.cors, true}`)
#------------------------------------------------
cors:
origin: "*"
headers:
- Content-Type
- X-Amz-Date
- Authorization
- X-Api-Key
- X-Amz-Security-Token
- X-Amz-User-Agent
- X-Lemon-Language
- X-Lemon-Identity
allowCredentials: true
#------------------------------------------------
# Provider
#------------------------------------------------
provider:
name: aws
# profile: ${param:profile, 'none'}
stage: ${opt:stage, 'dev'}
# Lambda Runtime Environment
memorySize: 512
timeout: 30
runtime: ${self:custom.CONF.${self:custom.CORE.profile}.runtime, 'nodejs18.x'}
region: ${self:custom.CONF.${self:custom.CORE.profile}.region, self:custom.CORE.region} # profile의 region를 먼저 사용하고, 없으면 실행 옵션값.
# WARN! NO VPC required due to NAT configuration to connect outside.
# # 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:
# Basic setting without VPC configuration.
- Effect: Allow
Action:
- lambda:InvokeFunction
- SNS:GetEndpointAttributes
- cloudwatch:PutMetricData
Resource: "*"
# IAM/STS - For VPC, Use static ARN Name for 'eureka-hello-api'.
- Effect: "Allow"
Action:
- iam:GetUser
- sts:GetCallerIdentity
Resource: "*"
# SQS to receive the queue job
- Effect: "Allow"
Action:
- sqs:SendMessage
- sqs:DeleteMessage
- sqs:ReceiveMessage
Resource: "arn:aws:sqs:${opt:region, self:provider.region}:*:${self:custom.queueName.${self:custom.CORE.stage}}"
# SNS to receive the notification.
- Effect: "Allow"
Action:
- sns:Publish
Resource: ${self:custom.topicArn}
# KMS to verify the identity
- Effect: Allow
Action:
- kms:Verify
Resource: "arn:aws:kms:${self:provider.region}:*:key/${self:custom.CONF.${self:custom.CORE.profile}.kmsKey, ''}"
# DynamoDB to store the models
- 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}}"
# -------------------------------------------------------
# you can define service wide environment variables here
# WARN! - 환경변수에는 object/null 으로 설정할 수 없음.
environment:
DEFAULT_REGION: '${self:provider.region}'
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
# -------------------------------------------------------
# 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 that AWS will send to your Lambda function on a single trigger. (default: 10)
batchSize: 1
#WARN - must use `lambda` as name for `lemon-protocol`.
lambda:
handler: handler.lambda # use core `lambda-handler`.
timeout: 30 # max timeout(30s) by API Gateway.
events:
# -------------------------------------------------------
# REST API : /
- http:
path: /
method: get
cors: ${self:custom.cors, true}
documentation:
summary: "show service name and version"
# -------------------------------------------------------
# REST API : /hello (public)
- http:
path: /hello
method: get
cors: ${self:custom.cors, true}
# authorizer: aws_iam
- http:
path: /hello/{id}
method: get
cors: ${self:custom.cors, true}
# authorizer: aws_iam
- http:
path: /hello/{id}/{cmd}
method: get
cors: ${self:custom.cors, true}
# authorizer: aws_iam
- http:
path: /hello/{id}
method: put
cors: ${self:custom.cors, true}
authorizer: aws_iam
- http:
path: /hello/subscribe
method: post
cors: ${self:custom.cors, true}
- http:
path: /hello/{id}
method: post
cors: ${self:custom.cors, true}
authorizer: aws_iam
- http:
path: /hello/{id}/{cmd}
method: post
cors: ${self:custom.cors, true}
authorizer: aws_iam
- http:
path: /hello/{id}
method: delete
cors: ${self:custom.cors, true}
authorizer: aws_iam
# -------------------------------------------------------
# REST API : General Controller. (authed)
- http:
path: /{type}
method: get
cors: ${self:custom.cors, true}
authorizer: aws_iam
- http:
path: /{type}/{id}
method: get
cors: ${self:custom.cors, true}
authorizer: aws_iam
- http:
path: /{type}/{id}/{cmd}
method: get
cors: ${self:custom.cors, true}
authorizer: aws_iam
- http:
path: /{type}/{id}
method: put
cors: ${self:custom.cors, true}
authorizer: aws_iam
- http:
path: /{type}/{id}/{cmd}
method: put
cors: ${self:custom.cors, true}
authorizer: aws_iam
- http:
path: /{type}/{id}
method: post
cors: ${self:custom.cors, true}
authorizer: aws_iam
- http:
path: /{type}/{id}/{cmd}
method: post
cors: ${self:custom.cors, true}
authorizer: aws_iam
- http:
path: /{type}/{id}
method: delete
cors: ${self:custom.cors, true}
authorizer: aws_iam
- http:
path: /{type}/{id}/{cmd}
method: delete
cors: ${self:custom.cors, true}
authorizer: aws_iam
#--------------------------------------------------------------
# ! INCLUDE ONLY DISTRIBUTIONS !
#--------------------------------------------------------------
package:
exclude:
- ./**
- '!node_modules/**'
include:
- handler.js
- package.json
- dist/**
- data/**
excludeDevDependencies: true
#==============================================================
# Resources
#--------------------------------------------------------------
resources:
Resources:
# -------------------------------
# Dynamo Table
# -------------------------------
MyTable:
Type: 'AWS::DynamoDB::Table'
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"
# -------------------------------
# SQS Queue
# -------------------------------
MyQueue:
Type: 'AWS::SQS::Queue'
# https://docs.aws.amazon.com/ko_kr/AWSSimpleQueueService/latest/APIReference/API_SetQueueAttributes.html
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
# -------------------------------
# This SNS Topic
# -------------------------------
MyInputTopic:
Type: 'AWS::SNS::Topic'
Properties:
TopicName: '${self:custom.topicName.${self:custom.CORE.stage}}'