Skip to content

Commit 779d9ca

Browse files
committed
feat: Use Lift in aws-sqs-python-worker
1 parent 404f9d7 commit 779d9ca

File tree

3 files changed

+20
-42
lines changed

3 files changed

+20
-42
lines changed

aws-python-sqs-worker/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This template demonstrates how to develop and deploy a simple SQS-based producer
1616

1717
## Anatomy of the template
1818

19-
This template defines two functions, `producer` and `consumer`. First of them, `producer`, is triggered by `http` event type, accepts JSON payload and sends it to a corresponding SQS queue for further processing. To learn more about `http` event configuration options, please refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/). Second function, `consumer`, is responsible for processing messages from SQS queue thanks to its `sqs` trigger definition. To learn more about `sqs` event configuration options, please refer to [sqs event docs](https://www.serverless.com/framework/docs/providers/aws/events/sqs/). Additionally, the template takes care of provisioning underlying SQS queue along with corresponding SQS dead-letter queue, which are defined in `resources` section. The dead-letter queue is defined in order to prevent processing invalid messages over and over. In our case, if message is delivered to the source queue more than 5 times, it will be moved to dead-letter queue. For more details, please refer to official [AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html). To learn more about `resources`, please refer to [our docs](https://www.serverless.com/framework/docs/providers/aws/guide/resources/).
19+
This template defines one function `producer` and one Lift construct - `jobs`. Producer function is triggered by `http` event type, accepts JSON payload and sends it to a corresponding SQS queue for further processing. To learn more about `http` event configuration options, please refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/). Jobs construct is responsible for two things. It creates SQS queue along with corresponding "dead-letter queue" and defines `worker` function that is responsible for processing events from created SQS queue. For more details about `queue` construct type and Serverless Lift plugin in general, please refer to [docs](https://github.com/getlift/lift/blob/master/docs/queue.md). For more details about SQS processing with AWS Lambda, please refer to official [AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html).
2020

2121
## Usage
2222

@@ -38,7 +38,7 @@ serverless deploy
3838

3939
After running deploy, you should see output similar to:
4040

41-
```bash
41+
``` bash
4242
Serverless: Packaging service...
4343
Serverless: Excluding development dependencies...
4444
Serverless: Creating Stack...
@@ -47,7 +47,7 @@ Serverless: Checking Stack create progress...
4747
Serverless: Stack create finished...
4848
Serverless: Uploading CloudFormation file to S3...
4949
Serverless: Uploading artifacts...
50-
Serverless: Uploading service aws-python-sqs-worker.zip file to S3 (1.04 KB)...
50+
Serverless: Uploading service aws-python-sqs-worker.zip file to S3 (21.44 MB)...
5151
Serverless: Validating template...
5252
Serverless: Updating Stack...
5353
Serverless: Checking Stack update progress...
@@ -62,12 +62,14 @@ resources: 17
6262
api keys:
6363
None
6464
endpoints:
65-
POST - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/produce
65+
POST - https://xxx.execute-api.us-east-1.amazonaws.com/dev/produce
6666
functions:
6767
producer: aws-python-sqs-worker-dev-producer
68-
consumer: aws-python-sqs-worker-dev-consumer
68+
jobsWorker: aws-python-sqs-worker-dev-jobsWorker
6969
layers:
7070
None
71+
jobs:
72+
queueUrl: https://sqs.us-east-1.amazonaws.com/xxxx/aws-python-sqs-worker-dev-jobs
7173
```
7274

7375
_Note_: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/).

aws-python-sqs-worker/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"version": "1.0.0",
44
"description": "Serverless Framework Python SQS Producer-Consumer on AWS",
55
"author": "",
6-
"license": "MIT"
6+
"license": "MIT",
7+
"dependencies": {
8+
"serverless-lift": "^1.1.2"
9+
}
710
}

aws-python-sqs-worker/serverless.yml

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ provider:
88
runtime: python3.8
99
lambdaHashingVersion: '20201221'
1010
stage: dev
11-
iam:
12-
role:
13-
statements:
14-
- Effect: Allow
15-
Action:
16-
- sqs:SendMessage
17-
Resource:
18-
- Fn::GetAtt: [ WorkerQueue, Arn ]
11+
12+
constructs:
13+
jobs:
14+
type: queue
15+
worker:
16+
handler: handler.consumer
1917

2018
functions:
2119
producer:
@@ -25,32 +23,7 @@ functions:
2523
method: post
2624
path: produce
2725
environment:
28-
QUEUE_URL:
29-
Ref: WorkerQueue
30-
31-
consumer:
32-
handler: handler.consumer
33-
events:
34-
- sqs:
35-
batchSize: 1
36-
arn:
37-
Fn::GetAtt:
38-
- WorkerQueue
39-
- Arn
26+
QUEUE_URL: ${construct:jobs.queueUrl}
4027

41-
resources:
42-
Resources:
43-
WorkerQueue:
44-
Type: AWS::SQS::Queue
45-
Properties:
46-
QueueName: workerQueue-${self:provider.stage}
47-
RedrivePolicy:
48-
deadLetterTargetArn:
49-
Fn::GetAtt:
50-
- WorkerDLQ
51-
- Arn
52-
maxReceiveCount: 5
53-
WorkerDLQ:
54-
Type: AWS::SQS::Queue
55-
Properties:
56-
QueueName: workerDlq-${self:provider.stage}
28+
plugins:
29+
- serverless-lift

0 commit comments

Comments
 (0)