View on GitHub

aws-serverless-retry

Simple library to retry or poll your payload using SNS, SQS and Lambda

Simple library to retry or poll your payload using SNS, SQS and Lambda

Build Status codecov

NPM

Use SNS and SQS services to hook your application for retrying your message with Lambda (recommended) or NodeJS app.

SNS service can send payload to topic (topic is decided based on configuration) which then gets delivered to queue (if subscribed). Configuration is fully customizable. User can specify which topic to send it to based on the status codes: retryStatusCodes, failureStatusCodes, successStatusCodes.

Messages from SQS can be consumed by Lambda (scheduled with interval) using SQS service. Specify no. of messages to read, queue name, and destination topic and leave the rest to service to read, process, delete, and send it to destination topic

If you are also looking for aws promise libraries? this library will nicely integrate with your code…

Example solution diagram of how you can use this library to retry or poll your message

alt tag

Installing

In Node.js

The preferred way to install the AWS Serverless Retry for Node.js is to use the npm package manager for Node.js. Simply type the following into a terminal window:

npm install aws-serverless-retry

Usage and Getting Started

const ASR = require("aws-serverless-retry");
const SNSService = ASR.SNS;

//Create SNSService
let region = "us-west-2";
let snsService = new SNSService(region);

//Params
let statusCode = 200;
let payload = {
  "data": "Test"
};
let snsConfig = {    
    retryStatusCodes: [500],
    failureStatusCodes: [400],
    successStatusCodes: [200, 201],
    maxRetryAttempts: 2,            
    retryTopicName: "retry-topic",
    successTopicName: "success-topic",
    failureTopicName: "failure-topic"
};

//Now call sendToTopicByStatusCode
snsService.sendToTopicByStatusCode(statusCode, payload, snsConfig, subject)
           .then(response => {
                //Success     
           })
           .catch(err => {
                //Error
           });

Logging

Set DEBUG environment variable to true to enable logging

Actions you can perform:

SNS Service:

SQS Service: