Skip to content

Sqs expressjs example #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 21, 2024
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Please check out [FastAPI with Response Streaming](examples/fastapi-response-str

**AWS_LWA_READINESS_CHECK_MIN_UNHEALTHY_STATUS** - allows you to customize which HTTP status codes are considered healthy and which ones are not

**AWS_LWA_PASS_THROUGH_PATH** - Path to receive events payloads passed through from non-http event triggers. The default is "/events".
**AWS_LWA_PASS_THROUGH_PATH** - Path to receive events payloads passed through from non-http event triggers. The default is "/events".

## Request Context

Expand Down Expand Up @@ -161,7 +161,8 @@ Please note that `sam local` starts a Lambda Runtime Interface Emulator on port

## Non-HTTP Event Triggers

The Lambda Web Adapter also supports all non-HTTP event triggers, such as SQS, SNS, S3, DynamoDB, Kinesis, Kafka, EventBridge, and Bedrock Agents. The adapter forwards the event payload to the web application via a path defined by the `AWS_LWA_PASS_THROUGH_PATH` environment variable. By default, this path is set to `/events`. Upon receiving the event payload from the request body, the web application should processes it and returns the results as a JSON response. Please checkout [SQS Express.js](examples/sqs-expressjs) on how to use it.
The Lambda Web Adapter also supports all non-HTTP event triggers, such as SQS, SNS, S3, DynamoDB, Kinesis, Kafka, EventBridge, and Bedrock Agents. The adapter forwards the event payload to the web application via http post to a path defined by the `AWS_LWA_PASS_THROUGH_PATH` environment variable. By default, this path is set to `/events`. Upon receiving the event payload from the request body, the web application should processes it and returns the results as a JSON response. Please checkout [SQS Express.js](examples/sqs-expressjs) on how to use it.


## Examples

Expand Down
27 changes: 11 additions & 16 deletions examples/sqs-expressjs/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SQS Express.js example

This project demonstrates the integration of Amazon Simple Queue Service (SQS) with an Express.js application. It showcases how to effectively manage and process messages from an SQS queue within an Express.js application environment.
This project demonstrates the integration of Amazon Simple Queue Service (SQS) with an Express.js application on AWS Lambda. It showcases how to effectively manage and process messages from an SQS queue within an serverless Express.js application environment.

In this Express.js application integrated with Amazon SQS, there is no explicit code required to poll the SQS queue. The AWS Lambda handles the polling of the SQS queue and Lambda Web Adapter forwards the event payload to the Express.js application. This simplifies the application code and allows developers to focus on processing the event payload rather than managing the queue polling logic.
In this Express.js application integrated with Amazon SQS, there is no explicit code required to poll the SQS queue. The AWS Lambda handles the polling of the SQS queue and Lambda Web Adapter forwards the event payload to the Express.js application vith a HTTP POST request. This simplifies the application code and allows developers to focus on processing the event payload rather than managing the queue polling logic.

The application can be deployed in an AWS account using the [Serverless Application Model](https://github.com/awslabs/serverless-application-model). The `template.yaml` file in the root folder contains the application definition.

Expand Down Expand Up @@ -83,24 +83,19 @@ sam logs --tail --stack-name <replace with your stack name>

## Local Test

You can also use SAM CLI for local testing.
First, start the express.js server locally.

```shell
sam local invoke SqsExpressFunction -e events/sqs.json
cd app/src
npm install
node index.js
```

Here is a sample output from this command.
Use `curl` to send a POST request to the express.js server.

```shell
Invoking Container created from sqsexpressfunction:v1
Building image.................
Using local image: sqsexpressfunction:rapid-x86_64.

START RequestId: ceaaf9bb-8d8c-42a5-828c-a5d4c8a506f1 Version: $LATEST
Example app listening at http://localhost:8000
Received event: {"Records":[{"messageId":"19dd0b57-b21e-4ac1-bd88-01bbb068cb78","receiptHandle":"MessageReceiptHandle","body":"Hello from SQS!","attributes":{"ApproximateReceiveCount":"1","SentTimestamp":"1523232000000","SenderId":"123456789012","ApproximateFirstReceiveTimestamp":"1523232000001"},"messageAttributes":{},"md5OfBody":"7b270e59b47ff90a553787216d55d91d","eventSource":"aws:sqs","eventSourceARN":"arn:aws:sqs:us-east-1:123456789012:MyQueue","awsRegion":"us-east-1"}]}
processing message: 19dd0b57-b21e-4ac1-bd88-01bbb068cb78 with body: Hello from SQS!
END RequestId: ceaaf9bb-8d8c-42a5-828c-a5d4c8a506f1
REPORT RequestId: ceaaf9bb-8d8c-42a5-828c-a5d4c8a506f1 Init Duration: 0.10 ms Duration: 117.12 ms Billed Duration: 118 ms Memory Size: 1024 MB Max Memory Used: 1024 MB
"success"
curl -X POST -H "Content-Type: application/json" -d @events/sqs.json http://localhost:8080/events
```

You can also use your favorate IDE debugger to debug your application step by step.