Skip to content

λ Collecting our internal Lambda learnings into one reusable repo.

Notifications You must be signed in to change notification settings

joemccann/lambda

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Codeship Status for smallwins/lambda


λ @smallwins/lambda

  • Author your AWS Lambda functions as pure node style callbacks (aka errbacks)
  • Familiar middleware pattern for composition
  • Event sources like DynamoDB triggers and SNS topics too
  • Helpful npm scripts lambda-create, lambda-list and lambda-deploy

return a result to api gateway

Lets look at a vanilla AWS Lambda example. Here is a Lambda for performing a sum. Given event.query.x = 1 it will return {count:2}.

exports.handler = function sum(event, callback) {
  var errors = []
  if (typeof event.query === 'undefined') {
    errors.push(ReferenceError('missing event.query'))
  }
  if (event.query && typeof event.query != 'object') {
    errors.push(TypeError('event.query not an object'))
  }
  if (typeof event.query.x === 'undefined') {
    errors.push(ReferenceError('event.query not an object'))
  }
  if (event.query.x && typeof event.query.x != 'number') {
    errors.push(TypeError('event.query not an object'))
  }
  if (errors.length) {
    // otherwise Error would return [{}, {}, {}, {}]
    var err = errors.map(function(e) {return e.message})
    context.fail(err) 
  }
  else {
    context.succeed({count:event.query.x + 1})
  }
}

A huge amount of this code is working around quirky parameter validations. Builtin Error needs manual serialization (and you still lose the stack trace). The latter part of the code uses the funky AWS context object.

We can do better:

var validate = require('@smallwins/validate')
var lambda = require('@smallwins/lambda')

function sum(event, callback) {
  var schema = {
    'query':   {required:true, type:Object},
    'query.x': {required:true, type:Number}
  }
  var errors = validate(event, schema)
  if (errors) {
    callback(errors)
  }
  else {
    var result = {count:event.query.x + 1}
    callback(null, result)
  }
}

exports.handler = lambda(sum)

The validate library above takes care of builtin parameter validations. It can also handle custom types. The callback style above enjoys symmetry with the rest of Node and will automatically serialize Errors into JSON friendly objects including any stack trace. Finally we wrap our function using lambda which will return a function with an AWS Lambda friendly signature.

easily chain dependant actions ala middleware

Building on this foundation we can compose multiple errbacks into a Lambda. Lets compose a Lambda that:

  • Validates parameters
  • Checks for an authorized account
  • And then either returns data safely
  • If anything fails return JSON serialized Error array
var validate = require('@smallwins/validate')
var lambda = require('@smallwins/lambda')

function valid(event, callback) {
  var schema = {
    'body':          {required:true, type:Object},
    'body.username': {required:true, type:String},
    'body.password': {required:true, type:String}
  }
  validate(event, schema, callback)
}

function authorized(event, callback) {
  var loggedIn = event.body.username === 'sutro' && event.body.password === 'cat'
  if (!loggedIn) {
    callback(Error('not found'))
  }
  else {
    event.account = {
      loggedIn: loggedIn,
      name: 'sutro furry pants'
    }
    callback(null, event)
  }
}

function safe(event, callback) {
  callback(null, {account:event.account})
}

exports.handler = lambda(valid, authorized, safe)

In the example above our functions are executed in series. Any Error returns immediately so if we make it the last function we just send back the resulting account data. Clean!

save a record from a dynamodb trigger

AWS DynamoDB can invoke a Lambda function if anything happens to a table.

var lambda = require('@smallwins/lambda')

function save(record, callback) {
  console.log('save a version ', record)
  callback(null, record)
}

exports.handler = lambda.sources.dynamo.save(save)

api

  • lambda(...fns)
  • lambda.sources.dynamo.all(...fns)
  • lambda.sources.dynamo.save(...fns)
  • lambda.sources.dynamo.insert(...fns)
  • lambda.sources.dynamo.modify(...fns)
  • lambda.sources.dynamo.remove(...fns)
  • lambda.sources.sns(...fns)

A handler looks something like this

function handler(event, callback) {
  // process event, use to pass data
  var result = {ok:true, event:event}
  callback(null, result)
}

errors

Always use Error type as the first parameter to callback:

function fails(event, callback) {
  callback(Error('something went wrong')
}

Or an array of Errors:

function fails(event, callback) {
  callback([
    Error('missing email'), 
    Error('missing password')
  ])
}

@smallwins/lambda serializes error into slack-rpc style JSON making them easy to work from API Gateway:

{
  ok: false, 
  errors: [
    {name:'Error', message:'missing email', stack'...'},
    {name:'Error', message:'missing password', stack'...'}
  ]
}

scripting api

@smallwins/lambda includes some helpful automation code perfect for npm scripts. If you have a project that looks like this:

project-of-lambdas/
 |-test/
 |-src/
 |  '-lambdas/
 |     |-signup/
 |     |  |-index.js
 |     |  |-test.js
 |     |  '-package.json
 |     |-login/
 |     '-logout/
 '-package.json

And a package.json like this:

{
  "name":"project-of-lambdas",
  "scripts": {
    "create":"AWS_PROFILE=smallwins lambda-create",
    "list":"AWS_PROFILE=smallwins lambda-list",
    "deploy":"AWS_PROFILE=smallwins lambda-deploy"
    "invoke":"AWS_PROFILE=smallwins lambda-invoke"
  }
}
  • npm run create src/lambdas/forgot creates a new lambda
  • npm run list lists all deployed lambdas
  • npm run deploy src/lambdas/signup brian deploys the lambda with the alias brian
  • npm run invoke src/lambdas/login brian '{"email":"[email protected]", "pswd":"..."}' to invoke a lambda

The ./scripts/invoke.js is also a module and useful for testing.

var invoke = require('@smallwins/lambda/scripts/invoke')
invoke('path/to/lambda', alias, payload, (err, response)=> {
  console.log(err, response)
})

About

λ Collecting our internal Lambda learnings into one reusable repo.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%