The stylish Node.js middleware engine for AWS Lambda
⚠️ Warning: Middy 0.x is being deprecated ⚠️
Middy 1.x, with support for Node.js 10 & 12 will soon replace Middy 0.x.
You can already use Middy 1.x, check out branch 1.0.0-beta
for documentation and source code. If you have a project running on Middy 0.x that you need to port to Middy 1.x, you can also check out the upgrade guide.
This is the expected timeline for the future releases:
- | January 2020 | March 2020 | August 2020 |
---|---|---|---|
Middy 0.x | Active | Supported | Discontinued |
Middy 1.0 | Beta (recommended) | Active | Active |
TOC
- TOC
- A little appetizer
- Install
- Requirements
- Why?
- Usage
- How it works
- Writing a middleware
- Available middlewares
- Api
- Typescript
- FAQ
- 3rd party middlewares
- Contributing
- License
A little appetizer
Middy is a very simple middleware engine. If you are used to web frameworks like express, than you will be familiar with the concepts adopted in Middy and you will be able to get started very quickly.
But code is better than 10,000 words, so let's jump into an example. Let's assume you are building a JSON API to process a payment:
# handlerjs const middy = const jsonBodyParser validator httpErrorHandler = // This is your common handler, in no way different than what you are used to doing every day// in AWS Lambdaconst processPayment = { // we don't need to deserialize the body ourself as a middleware will be used to do that const creditCardNumber expiryMonth expiryYear cvc nameOnCard amount = eventbody // do stuff with this data // ... return } // Notice that in the handler you only added base business logic (no deserilization,// validation or error handler), we will add the rest with middlewares const inputSchema = type: 'object' properties: body: type: 'object' properties: creditCardNumber: type: 'string' minLength: 12 maxLength: 19 pattern: '\d+' expiryMonth: type: 'integer' minimum: 1 maximum: 12 expiryYear: type: 'integer' minimum: 2017 maximum: 2027 cvc: type: 'string' minLength: 3 maxLength: 4 pattern: '\d+' nameOnCard: type: 'string' amount: type: 'number' required: 'creditCardNumber' // Insert here all required event properties // Let's "middyfy" our handler, then we will be able to attach middlewares to itconst handler = // parses the request body when it's a JSON and converts it to an object // validates the input // handles common http errors and returns proper responses moduleexports = handler
Install
As simple as:
npm install middy
or
yarn add middy
Requirements
Middy has been built to work by default from Node >= 6.10.
If you need to run it in earlier versions of Node (eg. 4.3) then you will have to transpile middy's code yourself using babel or a similar tool.
Why?
One of the main strengths of serverless and AWS Lambda is that, from a developer perspective, your focus is mostly shifted toward implementing business logic.
Anyway, when you are writing a handler, you still have to deal with some common technical concerns outside business logic, like input parsing and validation, output serialization, error handling, etc.
Very often, all this necessary code ends up polluting the pure business logic code in your handlers, making the code harder to read and to maintain.
In other contexts, like generic web frameworks (express, fastify, hapi, etc.), this problem has been solved using the middleware pattern.
This pattern allows developers to isolate these common technical concerns into "steps" that decorate the main business logic code. Middleware functions are generally written as independent modules and then plugged in into the application in a configuration step, thus not polluting the main business logic code that remains clean, readable and easy to maintain.
Since we couldn't find a similar approach for AWS Lambda handlers, we decided to create middy, our own middleware framework for serverless in AWS land.
Usage
As you might have already got from our first example here, using middy is very simple and requires just few steps:
- Write your Lambda handlers as usual, focusing mostly on implementing the bare business logic for them.
- Import
middy
and all the middlewares you want to use - Wrap your handler in the
middy()
factory function. This will return a new enhanced instance of your original handler, to which you will be able to attach the middlewares you need. - Attach all the middlewares you need using the function
.use(somemiddleware())
Example:
const middy = const middleware1 middleware2 middleware3 = const originalHandler = { /* your business logic */ } const handler = handler moduleexports = handler
You can also attach inline middlewares by using the functions .before
, .after
and
.onError
.
For a more detailed use case and examples check the Writing a middleware section and the API section.
How it works
Middy implements the classic onion-like middleware pattern, with some peculiar details.
When you attach a new middleware this will wrap the business logic contained in the handler in two separate steps.
When another middleware is attached this will wrap the handler again and it will be wrapped by all the previously added middlewares in order, creating multiple layers for interacting with the request (event) and the response.
This way the request-response cycle flows through all the middlewares, the handler and all the middlewares again, giving the opportunity within every step to modify or enrich the current request, context or the response.
Execution order
Middlewares have two phases: before
and after
.
The before
phase, happens before the handler is executed. In this code the
response is not created yet, so you will have access only to the request.
The after
phase, happens after the handler is executed. In this code you will
have access to both the request and the response.
If you have three middlewares attached as in the image above this is the expected order of execution:
middleware1
(before)middleware2
(before)middleware3
(before)handler
middleware3
(after)middleware2
(after)middleware1
(after)
Notice that in the after
phase, middlewares are executed in inverted order,
this way the first handler attached is the one with the highest priority as it will
be the first able to change the request and last able to modify the response before
it gets sent to the user.
Interrupt middleware execution early
Some middlewares might need to stop the whole execution flow and return a response immediately.
If you want to do this you can invoke handler.callback
in your middleware and return early without invoking next
.
Note: this will totally stop the execution of successive middlewares in any phase (before
and after
) and returns
an early response (or an error) directly at the Lambda level. If your middlewares do a specific task on every request
like output serialization or error handling, these won't be invoked in this case.
In this example we can use this capability for building a sample caching middleware:
// some function that calculates the cache id based on the current eventconst calculateCacheId = { /* ... */ }const storage = {} // middlewareconst cacheMiddleware = { let cacheKey return { cacheKey = options if optionsstorage // exits early and returns the value from the cache if it's already there return handler return } { // stores the calculated response in the cache optionsstoragecacheKey = handlerresponse } } // sample usageconst handler =
Handling errors
But what happens when there is an error?
When there is an error, the regular control flow is stopped and the execution is
moved back to all the middlewares that implements a special phase called onError
, following
the order they have been attached.
Every onError
middleware can decide to handle the error and create a proper response or
to delegate the error to the next middleware.
When a middleware handles the error and creates a response, the execution is still propagated to all the other error middlewares and they have a chance to update or replace the response as needed. At the end of the error middlewares sequence, the response is returned to the user.
If no middleware manages the error, the Lambda execution fails reporting the unmanaged error.
Promise support
Middy allows you to return promises (or throw errors) from your handlers (instead of calling callback()
) and middlewares
(instead of calling next()
).
Here is an example of a handler that returns a promise:
And here is an example of a middleware that returns a similar promise:
const asyncValidator = { { if handlereventbody return return Promise }} handler
Promises and error handling
onError
middlewares can return promises as well.
Here's how Middy handles return values from promise-enabled error handlers:
- If
onError
promise resolves to a truthy value, this value is treated as an error and passed further down the pipeline.
middleware1 = { Logger; return Promise }middleware2 = { Logger; return Promise }handler;
Here, first middleware1.onError
then middleware2.onError
will be called.
- If the last
onError
in the chain returns a promise which resolves to a value, the lambda fails and reports an unmanaged error In the example above, the lambda will fail and report the error returned bymiddleware2.onError
. - If
onError
promise resolves to a falsy value (null
,undefined
,false
etc.), the error handling pipeline continues and eventually the response is returned without an error.
const middleware1 = { handlerresponse = error: handlererror ; return Promise; // Resolves to a falsy value }const middleware2 = { return Promise }handler;
Here, only middleware1.onError
will be called. The rest of the error handlers will be skipped, and the lambda will finish normally and return the response. middleware2.onError
will not be called.
- If
onError
promise rejects, the error handling pipeline exits early and the lambda execution fails.
const middleware1 = { return Promise; }const middleware2 = { return Promise }handler;
Here, only middleware1.onError
will be called, and the lambda will fail early, reporting an error. middleware2.onError
will not be called.
Using async/await
Node.js 8.10 supports async/await, allowing you to work with promises in a way that makes handling asynchronous logic easier to reason about and asynchronous code easier to read.
You can still use async/await if you're running AWS Lambda on Node.js 6.10, but you will need to transpile your
async/await
code (e.g. using babel).
Take the following code as an example of a handler written with async/await:
And here is an example of a middleware written with async/await:
const asyncValidator = { before: async { if handlereventbody await return foo: bar return }} handler
Writing a middleware
A middleware is an object that should contain at least 1 of 3 possible keys:
before
: a function that is executed in the before phaseafter
: a function that is executed in the after phaseonError
: a function that is executed in case of errors
before
, after
and onError
functions need to have the following signature:
{ // ...}
Where:
handler
: is a reference to the current context and it allows access to (and modification of) the currentevent
(request), theresponse
(in the after phase) anderror
(in case of an error).next
: is a callback function that needs to be invoked when the middleware has finished its job so that the next middleware can be invoked
Configurable middlewares
In order to make middlewares configurable, they are generally exported as a function that accepts
a configuration object. This function should then return the middleware object with before
,
after
and onError
as keys.
E.g.
# myMiddlewarejs const myMiddleware = { // might set default options in config return { // might read options from `config` } { // might read options from `config` } { // might read options from `config` } } moduleexports = myMiddleware
With this convention in mind, using a middleware will always look like the following example:
const middy = const myMiddleware = const handler = handler moduleexports = handler
Inline middlewares
Sometimes you want to create handlers that serve a very small need and that are not
necessarily re-usable. In such cases you probably will need to hook only into one of
the different phases (before
, after
or onError
).
In these cases you can use inline middlewares which are shortcut functions to hook logic into Middy's control flow.
Let's see how inline middlewares work with a simple example:
const middy = const handler = handler handler handler moduleexports = handler
As you can see above, a middy instance also exposes the before
, after
and onError
methods to allow you to quickly hook-in simple inline middlewares.
More details on creating middlewares
Check the code for existing middlewares to see more examples on how to write a middleware.
Available middlewares
Currently available middlewares:
cache
: A simple but flexible caching layercors
: Sets CORS headers on responseNote: functionShield has been removed from core since 0.22.0. UsefunctionShield
: Hardens AWS Lambda execution environment@middy/function-shield
instead.doNotWaitForEmptyEventLoop
: Sets callbackWaitsForEmptyEventLoop property to falsehttpContentNegotiation
: ParsesAccept-*
headers and provides utilities for content negotiation (charset, encoding, language and media type) for HTTP requestshttpErrorHandler
: Creates a proper HTTP response for errors that are created with the http-errors module and represents proper HTTP errors.httpEventNormalizer
: Normalizes HTTP events by adding an empty object forqueryStringParameters
andpathParameters
if they are missing.httpHeaderNormalizer
: Normalizes HTTP header names to their canonical format.httpMultipartBodyParser
: Automatically parses HTTP requests with content typemultipart/form-data
.httpPartialResponse
: Filter response objects attributes based on query string parameters.jsonBodyParser
: Automatically parses HTTP requests with JSON body and converts the body into an object. Also handles gracefully broken JSON if used in combination ofhttpErrorHandler
.s3KeyNormalizer
: Normalizes key names in s3 events.secretsManager
: Fetches parameters from AWS Secrets Manager.ssm
: Fetches parameters from AWS Systems Manager Parameter Store.validator
: Automatically validates incoming events and outgoing responses against custom schemasurlEncodeBodyParser
: Automatically parses HTTP requests with URL encoded body (typically the result of a form submit).warmup
: Warmup middleware that helps to reduce the cold-start issue
For dedicated documentation on available middlewares check out the Middlewares documentation
Api
- middy :
function
- useFunction ⇒
middy
- middlewareAttachFunction ⇒
middy
- middlewareNextFunction :
function
- middlewareFunction ⇒
void
|Promise
- middlewareObject :
Object
middy
middy(handler) ⇒ Middy factory function. Use it to wrap your existing handler to enable middlewares on it.
Kind: global function
Returns: middy
- - a middy
instance
Param | Type | Description |
---|---|---|
handler | function |
your original AWS Lambda function |
function
middy : Kind: global typedef
Param | Type | Description |
---|---|---|
event | Object |
the AWS Lambda event from the original handler |
context | Object |
the AWS Lambda context from the original handler |
callback | function |
the AWS Lambda callback from the original handler |
Properties
Name | Type | Description |
---|---|---|
use | useFunction |
attach a new middleware |
before | middlewareAttachFunction |
attach a new before-only middleware |
after | middlewareAttachFunction |
attach a new after-only middleware |
onError | middlewareAttachFunction |
attach a new error-handler-only middleware |
__middlewares | Object |
contains the list of all the attached middlewares organised by type (before , after , onError ). To be used only for testing and debugging purposes |
middy
useFunction ⇒ Kind: global typedef
Type | Description |
---|---|
middlewareObject |
the middleware object to attach |
middy
middlewareAttachFunction ⇒ Kind: global typedef
Type | Description |
---|---|
middlewareFunction |
the middleware function to attach |
function
middlewareNextFunction : Kind: global typedef
Param | Type | Description |
---|---|---|
error | error |
An optional error object to pass in case an error occurred |
void
| Promise
middlewareFunction ⇒ Kind: global typedef
Returns: void
| Promise
- - A middleware can return a Promise instead of using the next
function as a callback.
In this case middy will wait for the promise to resolve (or reject) and it will automatically
propagate the result to the next middleware.
Param | Type | Description |
---|---|---|
handler | function |
the original handler function. It will expose properties event , context , response , error and callback that can be used to interact with the middleware lifecycle |
next | middlewareNextFunction |
the callback to invoke to pass the control to the next middleware |
Object
middlewareObject : Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
before | middlewareFunction |
the middleware function to attach as before middleware |
after | middlewareFunction |
the middleware function to attach as after middleware |
onError | middlewareFunction |
the middleware function to attach as error middleware |
Typescript
Middy exports Typescript compatible type information. To enable the use of Middy in your Typescript project please make sure tsconfig.json
is configured as follows:
{
"compilerOptions": {
...
/* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"esModuleInterop": true,
...
},
}
After that you can import middy from 'middy';
in your http handler and use it as described above.
FAQ
Q: Lambda timing out
A: If Lambda is timing out even though you are invoking a callback, there may still be some events in an event loop that are
preventing a Lambda to exit. This is common when using ORM to connect to the Database, which may keep connections to the database
alive. To solve this issue, you can use doNotWaitForEmptyEventLoop
middleware, which will force Lambda to exit when you invoke
a callback.
3rd party middlewares
Here's a collection of some 3rd party middlewares and libraries that you can use with Middy:
- middy-redis: Redis connection middleware
- middy-extractor: Extracts data from events using expressions
- @keboola/middy-error-logger: middleware that catches thrown exceptions and rejected promises and logs them comprehensibly to the console
- @keboola/middy-event-validator: Joi powered event validation middleware
- middy-reroute: provides complex redirect, rewrite and proxying capabilities by simply placing a rules file into your S3 bucket
- middytohof: Convert Middy middleware plugins to higher-order functions returning lambda handlers
- wrap-ware: A middleware wrapper which works with promises / async
- middy-jsonapi: JSONAPI middleware for middy
- middy-middleware-warmup: A middy plugin to help keep your Lambdas warm during Winter
- @sharecover-co/middy-aws-xray-tracing: AWS X-Ray Tracing Middleware
- @sharecover-co/middy-http-response-serializer: This middleware serializes the response to JSON and wraps it in a 200 HTTP response
- @seedrs/middyjs-middleware: Collection of useful middlewares
- middy-autoproxyresponse: A middleware that lets you return simple JavaScript objects from Lambda function handlers and converts them into LAMBDA_PROXY responses
jwt-auth
: JSON web token authorization middleware based onexpress-jwt
- middy-env: Fetch, validate and type cast environment variables
Contributing
Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.
License
Licensed under MIT License. Copyright (c) 2017-2018 Luciano Mammino and the Middy team.