https://github.com/adnanrahic/lambda-sns-dlq-error-handling
Sample project for showing the ability to publish an SNS topic and trigger a function from the topic. Code is structured to create a timeout/crash so the dead letter queue SNS topic gets published, in turn triggering the error handler function.
https://github.com/adnanrahic/lambda-sns-dlq-error-handling
aws aws-lambda aws-sns nodejs serverless serverless-framework
Last synced: 9 months ago
JSON representation
Sample project for showing the ability to publish an SNS topic and trigger a function from the topic. Code is structured to create a timeout/crash so the dead letter queue SNS topic gets published, in turn triggering the error handler function.
- Host: GitHub
- URL: https://github.com/adnanrahic/lambda-sns-dlq-error-handling
- Owner: adnanrahic
- Created: 2018-08-26T20:42:10.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-08-29T08:50:38.000Z (almost 8 years ago)
- Last Synced: 2025-04-02T10:51:23.050Z (about 1 year ago)
- Topics: aws, aws-lambda, aws-sns, nodejs, serverless, serverless-framework
- Language: JavaScript
- Homepage:
- Size: 8.79 KB
- Stars: 31
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README

# Trigger AWS Lambda with SNS with DLQ error handling
Sample project for showing the ability to publish an SNS topic and trigger a function from the topic. Code is structured to create a timeout/crash so the dead letter queue SNS topic gets published, in turn triggering the error handler function.
### Explanation
- The `init` function is the only exposed function, which is hooked up to API Gateway. It takes a single `number` parameter which it validates, upon success it publishes an SNS topic and sends along the `number` value.
- The SNS topic will trigger a second function called `calculate`. This function will perform the calculation and log out the result to the console. This mimics a heavy computational background task such as data processing, image manipulation or machine learning calculations.
- If either of the two functions fail the dead letter queue SNS topic will receive a message and trigger the `error` function.
Every function will try to retry its execution upon failure a total of 3 times. Using the dead letter queue as a pool for your error logs is a smart use-case.
### Causing a crash
Adding a crazy huge number will trigger a factorial calculation that'll crash because of a `Maximum call stack size exceeded` rangeError.
```bash
curl -H "Content-Type: application/json" \
-d '{"number":10000000}' \
https://.execute-api..amazonaws.com/dev/init
```
This will trigger the `error` function where you can handle the error as you wish.
---
The service is only a basic proof of concept which shows the use case of SNS triggering lambda functions and dead letter queues. The actual code is trivial and not exciting at all, but it serves the sole purpose of explaining what the *@$# is going on. :smile:
---
### TODO
- add SQS resources
- create setup where DLQ uses SQS
- create stream between `init` and `calculate` with SQS
- add SSM params instead of `secrets.json`