forked from vote/fax_gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
285 lines (244 loc) · 9.21 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
service: fax-gateway
provider:
name: aws
runtime: python3.7
# The default stage is the "local" stage; CI can deploy other stages (dev and prod)
stage: local
# We use us-west-2 as our AWS region
region: us-west-2
# 30-second timeout by default
timeout: 30
# We deploy Fax Gateway into a VPC. You can remove this stanza to deploy it outside your VPC.
vpc:
securityGroupIds:
- sg-7492cc4f
subnetIds:
- subnet-64b8642e
- subnet-b212a1ca
- subnet-196d6832
- subnet-bbc14ce6
# Tags that will be added to each of the deployment resources
tags:
env: ${self:custom.stage}
# Environment variables
environment:
# Sentry integration: sends Lambda errors to Sentry. Remove this if you don't want to
# use Sentry
# SENTRY_DSN: ${ssm:fax_gateway.common.sentry_dsn~true}
# SENTRY_ENVIRONMENT: ${self:custom.stage}
# Your twilio auth parameters
TWILIO_SID: ${ssm:fax_gateway.common.twilio_sid~true}
TWILIO_AUTH_TOKEN: ${ssm:fax_gateway.common.twilio_auth_token~true}
# The outgoing phone number the faxes will be sent from. This must be a
# fax-capable number in your Twilio account.
TWILIO_PHONE_NUMBER: ${ssm:fax_gateway.${self:custom.stage}.twilio_phone_number~true}
# How many times to try sending a fax before giving up. Faxes can fail because
# the receiver is busy or out of paper, so you should set this pretty
# generously
MAX_FAX_ATTEMPTS: "20"
# How many seconds to wait between retries
BACKOFF_DELAY: "600"
# Pass the queue URLs to the function
QUEUE_URL_FAX: { "Ref" : "FaxQueue" }
QUEUE_URL_WEBHOOK: { "Ref" : "WebhookQueue" }
QUEUE_URL_RETRY: { "Ref" : "RetryQueue" }
# Memory allocated to each lambda function
memorySize: 512
# Allow the lambda functions to access the SQS queues
iamRoleStatements:
- Effect: Allow
Action:
- sqs:*
Resource:
- "Fn::GetAtt": [ FaxQueue, Arn ]
- "Fn::GetAtt": [ WebhookQueue, Arn ]
- "Fn::GetAtt": [ RetryQueue, Arn ]
- "Fn::GetAtt": [ FaxDLQ, Arn ]
- "Fn::GetAtt": [ WebhookDLQ, Arn ]
- "Fn::GetAtt": [ RetryDLQ, Arn ]
package:
exclude:
- 'node_modules/**'
- '.vscode/**'
- '.mypy_cache/**'
- 'package.json'
- 'yarn.lock'
plugins:
# This plugin installs our python dependencies, using docker to properly compile
# them for Lambda
- serverless-python-requirements
# Clean up old versions so we don't exceed our code storage quota
- serverless-prune-plugin
# Datadog integration: remove this if you don't use datadog
# - serverless-plugin-datadog
custom:
# Specify how to build our python dependencies, and which ones are
# already available in Lambda and don't need to be bundled with the
# application
pythonRequirements:
dockerizePip: true
noDeploy: [
'boto3',
'botocore',
'docutils',
'jmespath',
'python-dateutil',
's3transfer',
'six',
'pip',
'setuptools'
]
# Make stage/region accessible to other parts of the config
stage: ${opt:stage, self:provider.stage}
region: ${opt:region, self:provider.region}
# Configure DataDog integration. If you've removed serverless-plugin-datadog
# above, you can remove this. flushMetricsToLogs requires that you have
# the datadog forwarder installed; see
# https://github.com/DataDog/datadog-serverless-functions/tree/master/aws/logs_monitoring#installation
# for details.
datadog:
flushMetricsToLogs: true
# Automatically remove old function versions to avoid filling up your lambda code storage
# quota.
prune:
automatic: true
number: 3
# SQS queue names
queueNames:
fax: fax-gateway-${self:custom.stage}-fax.fifo
retry: fax-gateway-${self:custom.stage}-retry
webhook: fax-gateway-${self:custom.stage}-webhook
faxDLQ: fax-gateway-${self:custom.stage}-fax-dlq.fifo
retryDLQ: fax-gateway-${self:custom.stage}-retry-dlq
webhookDLQ: fax-gateway-${self:custom.stage}-webhook-dlq
resources:
Resources:
# The fax queue is where incoming requests are written to. It's
# FIFO queue that uses the destination phone number as the message
# group, so we won't fax the same number twice at the same time.
FaxQueue:
Type: AWS::SQS::Queue
Properties:
FifoQueue: true
QueueName: ${self:custom.queueNames.fax}
ContentBasedDeduplication: true
# This controls how long between retries if the fax lambda fails.
# The fax lambda just send the request to Twilio -- it should never
# fail unless Twilio is down (it won't fail if the fax is undeliverable).
#
# This VisibilityTimeout *must* be higher than the Lambda timeout --
# otherwise messages will be retried before the lambda can finish! The
# fax sending lambda has a 15-minute timeout, because it doesn't return
# until the fax is done sending, and faxes can take a long time (~1 minute
# per page).
#
# So we set this pretty high -- we wait 20 minutes between retries, and give
# it 3 retries before we give up
VisibilityTimeout: 1200
RedrivePolicy:
deadLetterTargetArn:
"Fn::GetAtt": [ FaxDLQ, Arn ]
maxReceiveCount: 3
# The webhook queue stores the callbacks we need to make to the calling
# application, letting it know about success or failure of sending the
# fax.
WebhookQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:custom.queueNames.webhook}
# This controls how long between retries if the webhook lambda fails.
# This lambda can fail if the calling application can't receive the
# webhook, so we use pretty aggressive retry behavior -- we include
# an ID the receiver can use for deduplication/idempotence.
#
# We wait 3 minutes between retries, and will attempt to send the
# webhook up to 20 times.
VisibilityTimeout: 180
RedrivePolicy:
deadLetterTargetArn:
"Fn::GetAtt": [ WebhookDLQ, Arn ]
maxReceiveCount: 20
# The retry queue stores faxes that failed to send (because the receiver was
# busy, offline, or out of paper, for example). It's a regular queue rather
# than a FIFO queue so that we can use per-message delays to implement
# exponential backoff.
RetryQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:custom.queueNames.retry}
# This controls how long between retries if the retry lambda fails.
# The retry lambda just enqueues the message in the fax queue (it
# doesn't send it directly, because we need to fax queue's FIFO
# behavior to ensure we're not sending to the same fax multiple times
# at once). So we shouldn't need much retry -- this will only fail if
# lambda or SQS is down: retry every 3 minutes and give up after 3 tries.
VisibilityTimeout: 180
RedrivePolicy:
deadLetterTargetArn:
"Fn::GetAtt": [ RetryDLQ, Arn ]
maxReceiveCount: 3
# These two queues are dead-letter queues (DLQ): if messages can't be processed
# from the above queues, they end up in here after maxReceiveCount tries.
#
# This *does not* include expected failures to send the fax itself -- it
# only includes failures due to errors in our lambda handles, or outages
# of Twilio, SQS, or the calling application.
#
# We never read out of these queues; they're just here to store failures
# for later inspection. You should monitor these queues and examine any
# messages that end up in them (for example, you might want to set up a
# DataDog or CloudWatch alert if the queue size is ever greater than 0).
FaxDLQ:
Type: AWS::SQS::Queue
Properties:
FifoQueue: true
ContentBasedDeduplication: true
QueueName: ${self:custom.queueNames.faxDLQ}
MessageRetentionPeriod: 1209600
RetryDLQ:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:custom.queueNames.retryDLQ}
MessageRetentionPeriod: 1209600
WebhookDLQ:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:custom.queueNames.webhookDLQ}
MessageRetentionPeriod: 1209600
functions:
fax_processor:
handler: app.fax_processor.handler
timeout: 900
events:
- sqs:
# Only accept one fax at a time -- we only have 15 minutes to
# send the fax, so we can't count on delivering a bunch of faxes
# within that timeframe.
batchSize: 1
# Don't use lambda's built-in retry -- keep it simple and just use
# SQS for retry
maximumRetryAttempts: 0
arn:
'Fn::GetAtt':
- FaxQueue
- Arn
retry_processor:
handler: app.retry_processor.handler
events:
- sqs:
batchSize: 1
maximumRetryAttempts: 0
arn:
'Fn::GetAtt':
- RetryQueue
- Arn
webhook_processor:
handler: app.webhook_processor.handler
events:
- sqs:
batchSize: 1
maximumRetryAttempts: 0
arn:
'Fn::GetAtt':
- WebhookQueue
- Arn