Skip to content

A simple middleware framework for AWS lambda written in Nodejs. This is not published as a library but it is intended to serve as an example if you want create your own middlewares.

License

Notifications You must be signed in to change notification settings

siroop-ch/nodejs-lambda-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nodejs-lambda-middleware

A simple middleware framework for AWS lambda written in Nodejs. This is not published as a library but it is intended to serve as an example if you want create your own middlewares.

More details can be found here

Purpose

  • Ability to define lambdas as functions which return promises instead of invoking callbacks.
  • Provide a way to add reusable before/after code across lambdas.

An AWS Lambda in Nodejs is defined as a function with the below signature:

function (event, context, callback) {
}

The Lambda is expected to call callback with either error or the result after it completes. Callbacks can make the code very unreadable if there is lot of nesting.

We also have some common requirements across lambdas like Sentry integration, Datadog tracing etc which we would like to define once and reuse them.

Usage

const handlerFunction = (event, context) => {
  return new Promise()
}

withMiddlewares(handlerFunction, [Middleware1(), Middleware2()])

Order of execution

When calling withMiddlewares(handlerFunction, [Middleware1(), Middleware2()]), the middlewares are executed in order, left to right. This means that if a middleware changes the payload, it will be passed on to the following middlewares with the change.

Call: withMiddlewares(handlerFunction, [Middleware1(), Middleware2()]), no errors, no change to payload:

Middleware1+
           |
           +->Middleware2+-+
                           |
                           v
                        handlerFunction
                           +
                           |
              Middleware2<-+
              +
Middleware1<--+

Additionally, if a middleware encounters an error, the chain of middlewares -> handler will be short-circuited and the following middlewares and the final handlerFunction will not be called.

Call: withMiddlewares(handlerFunction, [Middleware1(), Middleware2()]), error in Middleware2, no change to payload:

Middleware1+
           |
           +->Middleware2
                  +
                  |
                *err*!  handlerFunction  (not called)
                  |
                  v
              Middleware2
              +
Middleware1<--+

Middleware template:

const Middleware1 = () => {
  return (event, context, next) => {
    // Before Logic
    return next(event, context)
      .then(result => {
        // After Logic
        return result
      })
      .catch(error => {
        // Error Handling
        return Promise.reject(error)
      })
  }
}

Refer examples for more information.

About

A simple middleware framework for AWS lambda written in Nodejs. This is not published as a library but it is intended to serve as an example if you want create your own middlewares.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published