-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserverless.yml
More file actions
241 lines (220 loc) · 6.71 KB
/
serverless.yml
File metadata and controls
241 lines (220 loc) · 6.71 KB
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
service:
name: serverless-api-template
plugins:
- serverless-webpack
- serverless-cloudformation-resource-counter
- serverless-plugin-iam-checker
- serverless-plugin-test-helper
- serverless-prune-plugin
- serverless-lumigo
provider:
name: aws
stage: ${opt:stage, env:STAGE, env:USER, 'local'}
runtime: nodejs12.x
apiGateway:
minimumCompressionSize: 1024 # Enable gzip compression for responses > 1 KB
environment:
NODE_ENV: PRODUCTION
AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1 # https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-reusing-connections.html
LAMBDA_WRAPPER_LOG: true
MESSAGES_TABLE: !Ref MessagesTable
INVALIDATE_CACHE_TOPIC: !Ref InvalidateCacheTopic
iamRoleStatements:
- Effect: Allow
Action: cloudfront:CreateInvalidation
Resource: '*'
- Effect: Allow
Action: sns:Publish
Resource: !Ref InvalidateCacheTopic
- Effect: Allow
Action:
- dynamodb:Scan
- dynamodb:PutItem
- dynamodb:DeleteItem
- dynamodb:GetItem
Resource: !GetAtt [MessagesTable, Arn]
custom:
invalidateCacheTopic: ${self:service.name}-${self:provider.stage}-invalidate-cache
# Serverless plugin configurations
# https://github.com/manwaring/serverless-plugin-iam-checker
iamChecker:
resources:
allowWildcardOnly: true # Unfortunately need to allow wildcard only because CloudFront doesn't support resource ARNs in IAM
# https://github.com/lumigo-io/serverless-lumigo-plugin
lumigo:
token: ${env:LUMIGO_TOKEN}
nodePackageManager: npm
# https://github.com/claygregory/serverless-prune-plugin
prune:
automatic: true
number: 3
# https://github.com/drexler/serverless-cloudformation-resource-counter
warningThreshold: 100
# https://github.com/serverless-heaven/serverless-webpack
webpack:
webpackConfig: ./webpack.config.js
includeModules:
forceExclude:
- aws-sdk
package:
exclude:
- .git
- .env
- package-lock.json
- package.json
- README.md
excludeDevDependencies: true
functions:
load-sample-data:
handler: src/messages/sample-data/load.handler
get-all-messages:
handler: src/messages/get-all.handler
events:
- http:
method: get
path: v1/messages
cors: true
get-message:
handler: src/messages/get.handler
events:
- http:
method: get
path: v1/messages/{id}
cors: true
request:
parameters:
paths:
id: true
add-message:
handler: src/messages/add.handler
events:
- http:
method: post
path: v1/messages
cors: true
delete-message:
handler: src/messages/delete.handler
events:
- http:
method: delete
path: v1/messages/{id}
cors: true
request:
parameters:
paths:
id: true
update-message:
handler: src/messages/update.handler
events:
- http:
method: put
path: v1/messages/{id}
cors: true
request:
parameters:
paths:
id: true
invalidate-cache:
handler: src/cache/invalidate.handler
events:
- stream:
type: dynamodb
arn: !GetAtt MessagesTable.StreamArn
- sns:
arn: !Ref InvalidateCacheTopic
topicName: ${self:custom.invalidateCacheTopic}
environment:
CLOUDFRONT: !If [IsPermanentStage, !Ref CDN, '']
resources:
Description: The ${self:provider.stage} version of the Serverless API starter using the Serverless Framework and TypeScript
Conditions:
IsPermanentStage: !Or
- !Equals ['${self:provider.stage}', prod]
- !Equals ['${self:provider.stage}', staging]
- !Equals ['${self:provider.stage}', dev]
Resources:
InvalidateCacheTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: ${self:custom.invalidateCacheTopic}
MessagesTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
BillingMode: PAY_PER_REQUEST
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
CDN:
Type: AWS::CloudFront::Distribution
Condition: IsPermanentStage
Properties:
DistributionConfig:
Comment: Powering the ${self:provider.stage} Pariveda Serverless Starter
CustomErrorResponses:
- ErrorCode: 503
ErrorCachingMinTTL: 0
- ErrorCode: 404
ErrorCachingMinTTL: 0
DefaultCacheBehavior:
TargetOriginId: api
AllowedMethods: [GET, HEAD, OPTIONS, PUT, PATCH, POST, DELETE]
CachedMethods: [GET, HEAD, OPTIONS]
ViewerProtocolPolicy: redirect-to-https
DefaultTTL: 2592000
ForwardedValues:
QueryString: true
Headers: [Accept, Referer, Authorization, Content-Type]
Enabled: true
PriceClass: PriceClass_100
Origins:
- Id: api
DomainName:
Fn::Join:
- '.'
- - !Ref ApiGatewayRestApi
- execute-api
- !Ref AWS::Region
- amazonaws.com
OriginPath: /${self:provider.stage}
CustomOriginConfig:
OriginProtocolPolicy: https-only
ApiDocumentationBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: PublicRead
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: index.html
ApiDocumentationBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref ApiDocumentationBucket
PolicyDocument:
Id: APIDocumentationBucketReadPolicy
Version: '2012-10-17'
Statement:
- Sid: PublicReadForGetBucketObjects
Effect: Allow
Principal: '*'
Action: s3:GetObject
Resource: !Join
- ''
- - 'arn:aws:s3:::'
- !Ref ApiDocumentationBucket
- /*
Outputs:
ApiDocumentationBucket:
Value: !Ref ApiDocumentationBucket
Description: Bucket for hosting Api documentation
ApiDocumentationUrl:
Value: !GetAtt [ApiDocumentationBucket, WebsiteURL]
Description: URL for Api documentation website
CloudFrontEndpoint:
Condition: IsPermanentStage
Description: URL of the CloudFront endpoint
Value: !Join ['', [https://, !GetAtt [CDN, DomainName]]]