Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ serverless deploy
| [AWS Simple HTTP Endpoint example in NodeJS](https://github.com/serverless/examples/tree/v4/aws-node-simple-http-endpoint) <br/> This example demonstrates how to setup a simple HTTP GET endpoint. Once you ping it, it will reply with the current time. | nodeJS |
| [Simple AWS Transcribe example in NodeJS](https://github.com/serverless/examples/tree/v4/aws-node-simple-transcribe-s3) <br/> This example demonstrates how to setup a lambda function to transcribe your audio file (.wav format) into a text transcription. The lambda will be triggered whenever a new audio file is uploaded to S3 and the transcription (JSON format) will be saved to a S3 bucket. | nodeJS |
| [AWS Single Page Application example in NodeJS](https://github.com/serverless/examples/tree/v4/aws-node-single-page-app-via-cloudfront) <br/> This example demonstrates how to setup a Single Page Application. | nodeJS |
| [Node SQS Producer Consumer on AWS](https://github.com/serverless/examples/tree/v4/aws-node-sqs-worker) <br/> This template demonstrates how to develop and deploy a simple SQS-based producer-consumer service running on AWS Lambda using the traditional Serverless Framework. | nodeJS |
| [Node SQS Producer Consumer on AWS](https://github.com/serverless/examples/tree/v4/aws-node-sqs-worker) <br/> This example shows how a Lambda function behind an HTTP API puts jobs on an Amazon SQS queue and a worker function processes them in batches, reporting failed messages so they retry and then move to a dead-letter queue. | nodeJS |
| [AWS Stripe Integration example in NodeJS](https://github.com/serverless/examples/tree/v4/aws-node-stripe-integration) <br/> This example for Stripe integration using AWS Lambda and API Gateway. | nodeJS |
| [Simple Telegram bot](https://github.com/serverless/examples/tree/v4/aws-node-telegram-echo-bot) <br/> This is a simple echo bot on Telegram. | nodeJS |
| [AWS Data Processing example in NodeJS](https://github.com/serverless/examples/tree/v4/aws-node-text-analysis-via-sns-post-processing) <br/> This example shows how a Lambda function behind an HTTP API publishes a note to an Amazon SNS topic and a second function subscribed to the topic analyzes it. | nodeJS |
Expand Down
202 changes: 160 additions & 42 deletions aws-node-sqs-worker/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--
title: 'Serverless Framework Node SQS Producer-Consumer on AWS'
description: 'This template demonstrates how to develop and deploy a simple SQS-based producer-consumer service running on AWS Lambda using the Serverless Framework.'
title: 'Node SQS Producer Consumer on AWS'
description: 'This example shows how a Lambda function behind an HTTP API puts jobs on an Amazon SQS queue and a worker function processes them in batches, reporting failed messages so they retry and then move to a dead-letter queue.'
layout: Doc
framework: v4
platform: AWS
Expand All @@ -11,88 +11,206 @@ authorName: 'Serverless, Inc.'
authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4'
-->

# Serverless Framework Node SQS Producer-Consumer on AWS
# Process a queue with Amazon SQS and Lambda

This template demonstrates how to build a producer-consumer service on AWS Lambda using the Serverless Framework. A `producer` function is exposed over HTTP API and accepts JSON payloads, which it forwards to an SQS queue for asynchronous processing. A `consumer` function is subscribed to that same queue and processes each message as it arrives.
[![Verified](https://raw.githubusercontent.com/serverless/examples/verified/badges/aws-node-sqs-worker.svg)](https://github.com/serverless/examples/blob/verified/results.json)

This template does not include any kind of persistence (database). For more advanced examples, check out the [serverless/examples repository](https://github.com/serverless/examples/) which includes Typescript, Mongo, DynamoDB and other examples.
An HTTP endpoint can accept work and leave it for later: it puts each job on an Amazon SQS queue and answers at once, and a worker function processes the queue in batches. The [`sqs` event](https://www.serverless.com/framework/docs/providers/aws/events/sqs) of the Serverless Framework connects the worker to the queue. With partial batch responses the worker reports only the messages it couldn't process, so the rest of the batch is done, and a dead-letter queue takes a message that keeps failing, so it doesn't come back forever. This example accepts jobs at `POST /jobs`, processes the ones that name a task, and moves the others to the dead-letter queue after three tries.

## Anatomy of the template
## What it deploys

This template defines two functions, `producer` and `consumer`, and one SQS queue, `JobsQueue`.
- An HTTP API with one route, `POST /jobs`, and a Lambda function, `producer`, that puts each job on the jobs queue.
- An SQS queue named `sqs-worker-<stage>-jobs`, and a dead-letter queue named `sqs-worker-<stage>-jobs-dlq` that keeps a failed job for 14 days.
- A Lambda function, `worker`, that Lambda invokes with batches of up to 10 messages from the jobs queue.
- An IAM role that lets the functions write their logs, lets `producer` send to the jobs queue only and lets `worker` read and delete its messages, and a CloudWatch Logs log group for each function.

- `producer` is triggered by an `httpApi` event on `POST /produce`. It accepts any request body and sends it as a message to `JobsQueue`, then responds immediately with a `202` status so the caller doesn't have to wait for the message to be processed.
- `consumer` is triggered by an `sqs` event sourced from `JobsQueue` with a batch size of 10. It logs each message it receives; this is where you would add your own background processing logic.
## Prerequisites

To learn more:
- The Serverless Framework CLI: `npm i -g serverless`, which needs [Node.js](https://nodejs.org/). See [Getting started](https://www.serverless.com/framework/docs/getting-started).
- An AWS account, with credentials configured on your machine. See [AWS credentials](https://www.serverless.com/framework/docs/providers/aws/guide/credentials).
- A Serverless account, or a license key in `SERVERLESS_LICENSE_KEY`. The CLI asks you to sign in the first time you run it.
- `curl`, to send jobs, and the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html), to read the dead-letter queue. The commands in Try it are written for Bash.

- about `httpApi` event configuration options, refer to the [HTTP API (API Gateway V2) event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api)
- about `sqs` event configuration options, refer to the [SQS event docs](https://www.serverless.com/framework/docs/providers/aws/events/sqs)
- about SQS processing with AWS Lambda, refer to the official [AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html)
## Deploy

## Usage
Deploy from this directory:

### Deployment
```bash
serverless deploy
```

Install dependencies with:
Without options, the deploy uses the stage `dev` and the Region `us-east-1`. To change them, pass `--stage` or `--region`, and pass the same options to every later command, including `serverless remove`.

You should see output like this:

```text
Deploying "sqs-worker" to stage "dev" (us-east-1)
✔ Service deployed to stack sqs-worker-dev (119s)
endpoint: POST - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/jobs

functions:
producer: sqs-worker-dev-producer (8.1 kB)
worker: sqs-worker-dev-worker (8.1 kB)
```
npm install
```

Then deploy with:
## Try it

Copy the endpoint from the deploy output into a shell variable:

```bash
URL=https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/jobs
```
serverless deploy

Send a job with a task, and a job without one:

```bash
curl -X POST "$URL" -H 'content-type: application/json' --data '{"task": "resize photo-1.png"}'
curl -X POST "$URL" -H 'content-type: application/json' --data '{"name": "photo-2.png"}'
```

After running deploy, you should see output similar to:
The API answers each with status 202 and the ID of the SQS message, before the job is processed:

```json
{"messageId":"ec7e4da2-61be-4b48-a044-75f90574a751"}
{"messageId":"7ab82ef6-12ea-4e01-9204-15c5b6ad7059"}
```
Deploying "sqs-worker" to stage "dev" (us-east-1)

✔ Service deployed to stack sqs-worker-dev (78s)
Lambda polls the queue and invokes `worker` a moment later. Its log shows the first job processed. The second job fails, goes back to the queue, and is received again each time the queue's 36-second visibility timeout ends:

endpoint: POST - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/produce
functions:
producer: sqs-worker-dev-producer (1.8 kB)
consumer: sqs-worker-dev-consumer (1.8 kB)
```bash
serverless logs --function worker
```

_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 the [HTTP API (API Gateway V2) event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api).
Among Lambda's own `START`, `END` and `REPORT` lines, the worker's lines are:

```text
INFO Processed job ec7e4da2-61be-4b48-a044-75f90574a751: resize photo-1.png
ERROR Job 7ab82ef6-12ea-4e01-9204-15c5b6ad7059 failed on receive 1: the job has no "task" string
ERROR Job 7ab82ef6-12ea-4e01-9204-15c5b6ad7059 failed on receive 2: the job has no "task" string
ERROR Job 7ab82ef6-12ea-4e01-9204-15c5b6ad7059 failed on receive 3: the job has no "task" string
```

### Invocation
If the command prints `No existing streams for the function` or only an `INIT_START` line, or a line isn't there yet, the worker hasn't run yet: wait a few seconds and run the command again.

After successful deployment, you can submit a job by sending a `POST` request to the `/produce` endpoint:
After the third failure, SQS moves the job to the dead-letter queue, about two minutes after it was sent. Show the stack outputs to find the dead-letter queue's URL:

```bash
serverless info --verbose
```
curl -X POST https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/produce -d '{"job":"hello"}'

Among the other outputs, the list shows:

```text
Stack Outputs:
DeadLetterQueueUrl: https://sqs.us-east-1.amazonaws.com/123456789012/sqs-worker-dev-jobs-dlq
```

Which should result in a response similar to:
Read the job from the dead-letter queue with the AWS CLI:

```json
{ "status": "queued" }
```bash
aws sqs receive-message --region us-east-1 \
--queue-url https://sqs.us-east-1.amazonaws.com/123456789012/sqs-worker-dev-jobs-dlq \
--query 'Messages[].[MessageId,Body]' --output text
```

```text
7ab82ef6-12ea-4e01-9204-15c5b6ad7059 {"name":"photo-2.png"}
```

with an HTTP status code of `202`.
A body that isn't a JSON object gets status 400 and `{"error":"Send a JSON object as the body."}`, and nothing is queued.

## How it works

The message is placed on the `JobsQueue` SQS queue, and the `consumer` function picks it up and processes it. You can view its logs with:
The `sqs` event in [`serverless.yml`](https://github.com/serverless/examples/blob/v4/aws-node-sqs-worker/serverless.yml) creates the event source mapping: Lambda polls the queue, invokes `worker` with up to 10 messages, and deletes the messages the worker processed. `functionResponseType: ReportBatchItemFailures` lets the worker name the messages it couldn't process, so only those return to the queue:

```yaml
functions:
worker:
handler: handler.worker
events:
- sqs:
arn: !GetAtt JobsQueue.Arn
batchSize: 10
functionResponseType: ReportBatchItemFailures
```
serverless logs -f consumer

The `sqs` event doesn't create queues, so both queues are declared under `resources`. The jobs queue's `RedrivePolicy` moves a message that was received three times without being deleted to the dead-letter queue. Its visibility timeout is at least the worker's timeout (6 seconds by default), as Lambda requires, and six times it, as AWS recommends:

```yaml
resources:
Resources:
JobsQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:service}-${sls:stage}-jobs
VisibilityTimeout: 36
RedrivePolicy:
deadLetterTargetArn: !GetAtt DeadLetterQueue.Arn
maxReceiveCount: 3

DeadLetterQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:service}-${sls:stage}-jobs-dlq
MessageRetentionPeriod: 1209600
```

Which should print output similar to:
Queue names are unique per account and Region, so the names include the stage: two stages get two pairs of queues. The `producer` function gets the jobs queue's URL in an environment variable, and its role may send to that queue only:

```yaml
provider:
iam:
role:
statements:
- Effect: Allow
Action:
- sqs:SendMessage
Resource: !GetAtt JobsQueue.Arn

functions:
producer:
handler: handler.producer
environment:
QUEUE_URL: !Ref JobsQueue
events:
- httpApi:
path: /jobs
method: post
```
processing job 9d1f2e3a-... {"job":"hello"}

[`handler.mjs`](https://github.com/serverless/examples/blob/v4/aws-node-sqs-worker/handler.mjs) holds both functions. `producer` sends the job with the AWS SDK for JavaScript v3, which the Node.js runtime includes, and returns the message ID. `worker` processes each message of the batch on its own, and returns the IDs of the messages that failed in `batchItemFailures`:

```javascript
export const worker = async (event) => {
const batchItemFailures = [];
for (const record of event.Records) {
try {
const job = JSON.parse(record.body);
if (typeof job.task !== 'string' || job.task.trim() === '') throw new Error('the job has no "task" string');
console.log(`Processed job ${record.messageId}: ${job.task}`);
} catch (err) {
console.error(`Job ${record.messageId} failed on receive ${record.attributes.ApproximateReceiveCount}: ${err.message}`);
batchItemFailures.push({ itemIdentifier: record.messageId });
}
}
return { batchItemFailures };
};
```

### Removal
If the worker threw instead, Lambda would return every message of the batch to the queue, and the jobs that succeeded would run again. See [Handling errors for an SQS event source in Lambda](https://docs.aws.amazon.com/lambda/latest/dg/services-sqs-errorhandling.html).

When you are done, you can remove the service and all of its resources with:
## Cost and cleanup

```
While the example is deployed, API Gateway bills each request to the HTTP API, Amazon SQS bills each request to the queues (sending, Lambda's polling, and receiving and deleting messages), Lambda bills each invocation and the functions' compute time, and CloudWatch Logs bills the functions' stored log data. See [API Gateway pricing](https://aws.amazon.com/api-gateway/pricing/), [Amazon SQS pricing](https://aws.amazon.com/sqs/pricing/), [Lambda pricing](https://aws.amazon.com/lambda/pricing/) and [CloudWatch pricing](https://aws.amazon.com/cloudwatch/pricing/).

Remove the API, the queues and the messages in them, the functions and their log groups:

```bash
serverless remove
```

## Next steps

- Read [SQS queue events](https://www.serverless.com/framework/docs/providers/aws/events/sqs) for batching windows, filter patterns that send the worker only some messages, maximum concurrency and FIFO queues.
- Move the jobs from the dead-letter queue back to the jobs queue once the worker can process them, with a [dead-letter queue redrive](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-configure-dead-letter-queue-redrive.html).
- Deliver each message to several subscribers instead of one worker, as the [SNS example](https://github.com/serverless/examples/tree/v4/aws-node-text-analysis-via-sns-post-processing) does.
30 changes: 30 additions & 0 deletions aws-node-sqs-worker/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"title": "Node SQS Producer Consumer on AWS",
"description": "This example shows how a Lambda function behind an HTTP API puts jobs on an Amazon SQS queue and a worker function processes them in batches, reporting failed messages so they retry and then move to a dead-letter queue.",
"language": "node",
"platform": "AWS",
"framework": "v4",
"priority": 1,
"author": {
"name": "Serverless, Inc.",
"link": "https://github.com/serverless",
"avatar": "https://avatars1.githubusercontent.com/u/13742415?s=200&v=4"
},
"services": [
"lambda",
"apigateway",
"sqs",
"iam",
"logs"
],
"test": {
"tier": "fast",
"checks": [
{
"script": {
"path": "test/smoke.mjs"
}
}
]
}
}
20 changes: 0 additions & 20 deletions aws-node-sqs-worker/handler.js

This file was deleted.

38 changes: 38 additions & 0 deletions aws-node-sqs-worker/handler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { SQSClient, SendMessageCommand } from '@aws-sdk/client-sqs';

const sqs = new SQSClient();

// POST /jobs with a JSON object: puts it on the jobs queue and answers right
// away. The worker processes it later.
export const producer = async (event) => {
let job;
try {
const body = event.isBase64Encoded ? Buffer.from(event.body, 'base64').toString('utf8') : event.body;
job = JSON.parse(body ?? '');
} catch {
// Not JSON: answered below.
}
if (job === null || typeof job !== 'object' || Array.isArray(job)) {
return { statusCode: 400, body: JSON.stringify({ error: 'Send a JSON object as the body.' }) };
}

const { MessageId } = await sqs.send(new SendMessageCommand({ QueueUrl: process.env.QUEUE_URL, MessageBody: JSON.stringify(job) }));
return { statusCode: 202, body: JSON.stringify({ messageId: MessageId }) };
};

// Invoked by Lambda with a batch of up to 10 messages from the queue. Returns
// the IDs of the messages it couldn't process, so only those are retried.
export const worker = async (event) => {
const batchItemFailures = [];
for (const record of event.Records) {
try {
const job = JSON.parse(record.body);
if (typeof job.task !== 'string' || job.task.trim() === '') throw new Error('the job has no "task" string');
console.log(`Processed job ${record.messageId}: ${job.task}`);
} catch (err) {
console.error(`Job ${record.messageId} failed on receive ${record.attributes.ApproximateReceiveCount}: ${err.message}`);
batchItemFailures.push({ itemIdentifier: record.messageId });
}
}
return { batchItemFailures };
};
Loading