-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless.yml
262 lines (243 loc) · 6.65 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
# For full config options, check the docs:
# docs.serverless.com
service: books
configValidationMode: error
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
frameworkVersion: '3'
custom:
stage: ${opt:stage, 'dev'}
# dbConnection: ${ssm:/aws/reference/secretsmanager/books/dev/db}
smtp: ${ssm:/aws/reference/secretsmanager/books/dev/smtp}
tempBookPrefix: temp
topicName: books-${self:custom.stage}
topicArn:
Fn::Join:
- ":"
- - arn
- aws
- sns
- Ref: AWS::Region
- Ref: AWS::AccountId
- ${self:custom.topicName}
downloadQueueName: books-download-${self:custom.stage}
sendQueueName: books-send-${self:custom.stage}
provider:
name: aws
runtime: python3.8
stage: ${self:custom.stage}
region: ${opt:region, 'eu-central-1'}
stackTags:
project: kindle-books-sender
apiGateway:
shouldStartNameWithService: true
apiKeys:
- name: test1
iam:
role:
name: kindle-books-sender-role
statements:
- Effect: Allow
Action:
- sns:Publish
Resource:
!Ref booksTopic
- Effect: Allow
Action:
- s3:PutObject
- s3:PutObjectAcl
- s3:DeleteObject
- s3:GetObject
Resource:
!Join ["", [!GetAtt storage.Arn, "/*"]]
environment:
# disabled for now
# DB_HOST: ${self:custom.dbConnection.host}
# DB_NAME: ${self:custom.dbConnection.dbname}
# DB_USER: ${self:custom.dbConnection.username}
# DB_PASSWORD: ${self:custom.dbConnection.password}
# DB_ENGINE: postgres
BOOKS_TOPIC: ${self:custom.topicArn}
package:
patterns:
- '!tests'
- '!appConfig.local.json'
functions:
add_book:
handler: handler.handle_add_book
events:
- http:
path: books
method: post
private: true
cors: true
download_book:
handler: handler.handle_download_book
environment:
FILE_PREFIX: ${self:custom.tempBookPrefix}
FILE_STORAGE: !GetAtt storage.Arn
events:
- sqs:
arn:
!GetAtt downloadQueue.Arn
send_book:
handler: handler.handle_send_book
environment:
SMTP_SERVER: ${self:custom.smtp.server}
SMTP_USER: ${self:custom.smtp.username}
SMTP_PASSWORD: ${self:custom.smtp.password}
SMTP_PORT: ${self:custom.smtp.port}
FILE_STORAGE: !GetAtt storage.Arn
events:
- sqs:
arn:
!GetAtt sendQueue.Arn
resources:
Resources:
downloadsBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: books-downloads
booksTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: ${self:custom.topicName}
downloadDeadletterQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:custom.downloadQueueName}-error
VisibilityTimeout: 120
downloadQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:custom.downloadQueueName}
VisibilityTimeout: 120
RedrivePolicy:
deadLetterTargetArn:
Fn::GetAtt:
- downloadDeadletterQueue
- Arn
maxReceiveCount: 3
downloadSubscription:
Type: AWS::SNS::Subscription
Properties:
Protocol: sqs
Endpoint:
Fn::GetAtt:
- downloadQueue
- Arn
TopicArn: !Ref booksTopic
FilterPolicy:
state:
- new
downloadQueuePolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref downloadQueue
PolicyDocument:
Statement:
-
Action:
- "SQS:SendMessage"
- "SQS:GetQueueAttributes"
Effect: "Allow"
Resource: !GetAtt downloadQueue.Arn
Principal:
Service:
- "sns.amazonaws.com"
Condition:
ArnEquals:
aws:SourceArn: !Ref booksTopic
downloadErrorQueuePolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref downloadDeadletterQueue
PolicyDocument:
Statement:
-
Action:
- "SQS:SendMessage"
- "SQS:GetQueueAttributes"
Effect: "Allow"
Resource: !GetAtt downloadDeadletterQueue.Arn
Principal:
Service:
- "sns.amazonaws.com"
Condition:
ArnEquals:
aws:SourceArn: !Ref booksTopic
sendQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:custom.sendQueueName}
VisibilityTimeout: 60
RedrivePolicy:
deadLetterTargetArn: !GetAtt sendErrorQueue.Arn
maxReceiveCount: 3
sendErrorQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:custom.sendQueueName}-error
VisibilityTimeout: 60
sendSubscription:
Type: AWS::SNS::Subscription
Properties:
Protocol: sqs
Endpoint: !GetAtt sendQueue.Arn
TopicArn: !Ref booksTopic
FilterPolicy:
state:
- downloaded
sendQueuePolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref sendQueue
PolicyDocument:
Statement:
-
Action:
- "SQS:SendMessage"
- "SQS:GetQueueAttributes"
Effect: Allow
Resource: !GetAtt sendQueue.Arn
Principal:
Service:
- "sns.amazonaws.com"
Condition:
ArnEquals:
aws:SourceArn: !Ref booksTopic
sendErrorQueuePolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref sendErrorQueue
PolicyDocument:
Statement:
-
Action:
- "SQS:SendMessage"
- "SQS:GetQueueAttributes"
Effect: Allow
Resource: !GetAtt sendErrorQueue.Arn
Principal:
Service:
- "sns.amazonaws.com"
Condition:
ArnEquals:
aws:SourceArn: !Ref booksTopic
storage:
Type: AWS::S3::Bucket
Properties:
BucketName: books-download-${self:custom.stage}
AccessControl: Private
LifecycleConfiguration:
Rules:
- Id: TempBooks
Prefix: ${self:custom.tempBookPrefix}
Status: Enabled
ExpirationInDays: "1"
plugins:
- serverless-python-requirements