lawos

1.1.5 • Public • Published

lawos - Lambda Worker SQS

npm license CircleCI Coveralls

Library to process messages from an Amazon SQS queue with an AWS Lambda worker function or your favorite other JavaScript environment. Works fine with Serverless

Install

> npm install lawos

Example

See lawos-serverless for an example of Lawos using Serverless.

Usage

Promise for every message

const AWS = require('aws-sdk');
const SQS = new AWS.SQS({apiVersion: '2012-11-05'});
 
const Lawos = require('lawos');
const Q = new Lawos('https://sqs.eu-west-1.amazonaws.com …', SQS);
 
Q.item(
  item => new Promise(done => {
    done();
  })
);
 
module.exports.handler = function(event, context, callback) {
  Q.work(
    () = Promise.resolve(context.getRemainingTimeInMillis() < 500)
  ).then(
    data => {
      callback(null, data);
    }
  );
};

Promise for a batch of messages

const AWS = require('aws-sdk');
const SQS = new AWS.SQS({apiVersion: '2012-11-05'});
 
const Lawos = require('lawos');
const Q = new Lawos('https://sqs.eu-west-1.amazonaws.com …', SQS);
 
Q.list(
  list => new Promise(done => {
    done();
  })
);
 
module.exports.handler = function(event, context, callback) {
  Q.work(
    () = Promise.resolve(context.getRemainingTimeInMillis() < 500)
  ).then(
    data => {
      callback(null, data);
    }
  );
};

Use AWS Lambda instead of Promise

const AWS = require('aws-sdk');
const Lawos = require('lawos');
 
const Lambda = new AWS.Lambda({apiVersion: '2015-03-31'});
const SQS = new AWS.SQS({apiVersion: '2012-11-05'});
 
const Q = new Lawos('https://sqs.eu-west-1.amazonaws.com …', SQS, Lambda);
 
Q.item('fake-function-name');
// Q.list('fake-function-name');
 
module.exports.handler = function(event, context, callback) {
  Q.work(
    () = Promise.resolve(context.getRemainingTimeInMillis() < 500)
  ).then(
    data => {
      callback(null, data);
    }
  );
};

License

Feel free to use the code, it's released using the MIT license.

Contributors

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.1.5
    106
    • latest

Version History

Package Sidebar

Install

npm i lawos

Weekly Downloads

106

Version

1.1.5

License

MIT

Last publish

Collaborators

  • sbstjn