What is AWS SNS?īefore we start coding we need to cover the basics. Makes sense now? Sweet, let’s talk a bit more about SNS. Sending your data through a pub/sub service like SNS or a queue like SQS will make sure you have data integrity. It can result in losing invocations and dropping data. Another issue is hitting Lambda’s concurrency limits pretty fast. Otherwise, it’s fine if you need the response from the second lambda function right away. Now you’re wondering, why all the complication with SNS instead of just invoking the second lambda function from the first one with Lambda’s invoke API?įirst of all, it’s a huge anti-pattern for asynchronous workflows, which is our case. Using the Dead Letter Queue as a pool for your error logs is a smart use-case.
If the calculate function fails, the Dead Letter Queue SNS topic will receive a message and trigger the error function.Įvery function invoked asynchronously will twice retry its execution upon failure. This mimics a heavy computational background task such as data processing, image manipulation or machine learning calculations. This function will perform the calculation and log out the result to the console. The SNS topic will trigger a second function called calculate. It takes a single number parameter which it validates, upon success, it publishes an SNS topic and sends along the number value. The init function is the only exposed function, which is hooked up to API Gateway. Here’s a nice diagram, because diagrams are awesome of course! I’ve chosen a recursive function that calculates the factorial of the number passed to it.
Npm serverless pseudo parameters code#
The code itself will just mimic the behavior of a random complex computation.
Our focus will solely be on the steps to create the infrastructure components our app will need. Note: All the code from this tutorial is already on GitHub if you want to check out the end result right away. Build the API with the Serverless Framework.I’ve covered a few interesting topics regarding serverless architectures and AWS already, but nothing like this yet. Today we’ll take a look at triggering AWS Lambda functions from AWS SNS messages. If you’re like me, a sucker for event-driven programming, you’ll want to continue reading.