-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverless.yml
More file actions
323 lines (298 loc) · 10 KB
/
serverless.yml
File metadata and controls
323 lines (298 loc) · 10 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
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
service:
name: udagram-app-serverless
# app and org for use with dashboard.serverless.com
#app: your-app-name
#org: your-org-name
# Add the serverless-webpack plugin
plugins:
- serverless-webpack
provider:
name: aws
runtime: nodejs10.x
stage: ${opt:stage, 'dev'} # either provided by user or default dev
region: ${opt:region, 'ap-south-1'}
environment:
GROUPS_TABLE: Groups-${self:provider.stage} # replce the later by stage name
IMAGES_TABLE: Images-${self:provider.stage}
IMAGES_INDEX: ImageIdIndex
CONNECTION_TABLE: Connections-${self:provider.stage}
IMAGES_S3_BUCKET: serverless-images-udagram-sak-${self:provider.stage}
THUMBNAILS_S3_BUCKET: serverless-udagram-thumbnail-sak-${self:provider.stage}
# SIGNED_URL_EXPIRATION: 300
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Scan
- dynamodb:PutItem
- dynamodb:GetItem
Resource: arn:aws:dynamodb:${self:provider.region}:*:table/${self:provider.environment.GROUPS_TABLE}
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:PutItem
Resource: arn:aws:dynamodb:${self:provider.region}:*:table/${self:provider.environment.IMAGES_TABLE}
- Effect: Allow
Action:
- dynamodb:Query
Resource: arn:aws:dynamodb:${self:provider.region}:*:table/${self:provider.environment.IMAGES_TABLE}/index/${self:provider.environment.IMAGES_INDEX}
- Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
Resource: arn:aws:s3:::${self:provider.environment.IMAGES_S3_BUCKET}/*
- Effect: Allow
Action:
- s3:PutObject
Resource: "arn:aws:s3:::${self:provider.environment.THUMBNAILS_S3_BUCKET}/*"
- Effect: Allow
Action:
- dynamodb:Scan
- dynamodb:PutItem
- dynamodb:DeleteItem
Resource: arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.CONNECTION_TABLE}
custom:
topicName: imagesTopic-${self:provider.stage} #name for the topic
webpack:
webpackConfig: ./webpack.config.js
includeModules: true
functions:
GetGroups: #name of function independent
handler: src/lambda/http/getGroups.handler
events:
- http:
method: get
path: groups
cors: true # enable cors
CreateGroups: #name of function independent
handler: src/lambda/http/createGroups.handler
events:
- http:
method: post
path: groups
cors: true # enable cors
request:
schema:
application/json: ${file(models/create-group-request.json)}
GetImages:
handler: src/lambda/http/getImages.handler
events:
- http:
method: get
path: groups/{groupId}/images
cors: true
GetImage:
handler: src/lambda/http/getImage.handler
events:
- http:
method: get
path: images/{imageId}
cors: true
CreateImage:
handler: src/lambda/http/createImage.handler
events:
- http:
method: post
path: groups/{groupId}/images
cors: true
# request:
# schema:
# application/json: ${file(models/create-image-request.json)}
# SendUploadNotifications: #add handler without event
# environment:
# STAGE: ${self:provider.stage}
# API_ID:
# Ref: WebsocketsApi
# handler: src/lambda/s3/sendNotifications.handler # change this func to process event from sns
SendUploadNotifications:
environment:
STAGE: ${self:provider.stage}
API_ID:
Ref: WebsocketsApi
handler: src/lambda/s3/sendNotifications.handler
events:
- sns:
arn:
Fn::Join:
- ':'
- - arn:aws:sns
- Ref: AWS::Region
- Ref: AWS::AccountId
- ${self:custom.topicName}
topicName: ${self:custom.topicName}
ConnectionHandler:
handler: src/lambda/websocket/connect.handler
events:
- websocket:
route: $connect
DisconnectionHandler:
handler: src/lambda/websocket/disconnect.handler
events:
- websocket:
route: $disconnect
# SyncWithElasticsearch:
# handler: src/lambda/dynamoDb/elasticSearchSync.handler
# environment:
# ES_ENDPOINT: !GetAtt ImagesSearch.DomainEndpoint
# events: # specify the event for which we need to invoke this func that is dynamodb stream and speicfy its table stream arn
# - stream:
# type: dynamodb
# arn: !GetAtt ImagesDynamoDBTable.StreamArn # like imagedynamodb resources and get attr from it
ResizeImage:
handler: src/lambda/s3/resizeImage.handler
events:
- sns:
arn:
Fn::Join:
- ':'
- - arn:aws:sns
- Ref: AWS::Region
- Ref: AWS::AccountId
- ${self:custom.topicName}
topicName: ${self:custom.topicName}
resources:
Resources:
GroupsDynamoDBTable:
Type: "AWS::DynamoDB::Table"
Properties:
TableName: ${self:provider.environment.GROUPS_TABLE}
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
BillingMode: PAY_PER_REQUEST
ImagesDynamoDBTable:
Type: "AWS::DynamoDB::Table"
Properties:
TableName: ${self:provider.environment.IMAGES_TABLE}
AttributeDefinitions:
- AttributeName: groupId
AttributeType: S
- AttributeName: timestamp
AttributeType: S
- AttributeName: imageId
AttributeType: S
KeySchema:
- AttributeName: groupId
KeyType: HASH
- AttributeName: timestamp
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: ${self:provider.environment.IMAGES_INDEX}
KeySchema:
- AttributeName: imageId
KeyType: HASH
Projection:
ProjectionType: ALL
BillingMode: PAY_PER_REQUEST
# StreamSpecification:
# StreamViewType: NEW_IMAGE # contains after image only
WebSocketConnectionsDynamoDBTable:
Type: "AWS::DynamoDB::Table"
Properties:
TableName: ${self:provider.environment.CONNECTION_TABLE}
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
BillingMode: PAY_PER_REQUEST
# add permisiion to allow s3 to invoke lambda func
# SendUploadNotificationsPermission:
# Type: AWS::Lambda::Permission
# Properties:
# FunctionName: !Ref SendUploadNotificationsLambdaFunction #func to invoke
# Principal: s3.amazonaws.com
# Action: lambda:InvokeFunction
# SourceAccount: !Ref AWS::AccountId
# SourceArn: arn:aws:s3:::${self:provider.environment.IMAGES_S3_BUCKET}
# resource to add topic
AttachmentsBucket: #cors headers
Type: AWS::S3::Bucket
DependsOn: SNSTopicPolicy
Properties:
BucketName: ${self:provider.environment.IMAGES_S3_BUCKET}
NotificationConfiguration: #config to connect event from this bucket to lambda func
TopicConfigurations:
- Event: s3:ObjectCreated:Put
Topic: !Ref ImagesTopic
CorsConfiguration:
CorsRules:
-
AllowedOrigins:
- '*'
AllowedHeaders:
- '*'
AllowedMethods:
- 'GET'
- 'POST'
- 'PUT'
- 'DELETE'
- 'HEAD'
MaxAge: 3000
BucketPolicy: # IAM policies
Type: AWS::S3::BucketPolicy
Properties:
PolicyDocument:
Id: MyPolicy # to read all images without any auth and identification
Version: "2012-10-17"
Statement:
- Sid: PublicReadForGetBucketObjects
Effect: Allow
Principal: '*'
Action: 's3:GetObject'
Resource: 'arn:aws:s3:::${self:provider.environment.IMAGES_S3_BUCKET}/*'
Bucket: !Ref AttachmentsBucket
SNSTopicPolicy:
# to allow s3 to send events to this topic
Type: AWS::SNS::TopicPolicy
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
AWS: "*"
Action: sns:Publish # allows only publish
Resource:
!Ref ImagesTopic # on this topic
Condition:
ArnLike:
AWS:SourceArn: arn:aws:s3:::${self:provider.environment.IMAGES_S3_BUCKET} # from this bucket
Topics:
- !Ref ImagesTopic
ThumbnailsBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:provider.environment.THUMBNAILS_S3_BUCKET}
ImagesTopic: # resource to add topic
Type: AWS::SNS::Topic
Properties:
DisplayName: Image bucket topic
TopicName: ${self:custom.topicName}
# ImagesSearch: # resource to creaate elastic cluster
# Type: AWS::Elasticsearch::Domain
# Properties:
# ElasticsearchVersion: '6.3'
# DomainName: image-seacrh-${self:provider.stage}
# ElasticsearchClusterConfig: #machine config
# DedicatedMasterEnabled: false #no master host
# InstanceCount: '1' # one machine one instance
# ZoneAwarenessEnabled: false
# InstanceType: t2.small.elasticsearch # tyoe of machine
# EBSOptions: #elastic block store this section define storage available for cluster node
# EBSEnabled: true
# Iops: 0
# VolumeSize: 10
# VolumeType: 'gp2'
# AccessPolicies:
# Version: '2012-10-17'
# Statement:
# -
# Effect: Allow
# Principal:
# AWS: '*'
# Action: 'es:*'
# Resource: '*'