The stylish Node.js middleware engine for AWS Lambda

3 min read Original article ↗

Middy logo

Organise your Lambda code, remove code duplication, focus on business logic. Simple, fast, and extensible.

  • 3.9k

    GitHub Stars

  • 2M+

    Monthly downloads

  • 200+

    Contributors

Everything you need for clean Lambda code

  • Simple but powerful

    A middleware engine makes your code more organised, removes duplication and uniforms the handling of non-functional concerns like authentication, authorization, validation, and serialization.

  • Focus on what matters

    By pushing all the non-functional code to middlewares, you can be productive and focus on what matters the most: the business logic!

  • Small core

    Middy comes with a very small core and an unobtrusive API to add the minimum amount of overhead to your code.

  • Batteries included

    Middy comes with a large set of official middlewares and utilities that can be used out of the box to address the most common non-functional use cases.

  • Blazing fast

    Middy has been engineered to keep your lambda as fast as possible. The minimal core keeps your lambda size small and your cold starts under control. Add only what you need!

  • Extensible

    Do you need to do more? It's really easy to write your own custom middlewares . And, if that's not enough, you can even extend middy itself through its hooks .

Show me the code!

The following abstract example illustrates the difference of style when using Middy:

Without Middy

  export const handler = (event, context) => {
  // BOILERPLATE!
  // E.g. decrypt environment variables with KMS
  // deserialize the content of the event
  // validate input, authentication, authorization

  // REAL BUSINESS LOGIC
  let response = doSomethingUsefulWith(event)

  // MORE BOILERPLATE
  // E.g.
  // validate output
  // serialize response
  // handle errors
  return response
}

Without Middy all your non-functional code is mixed up with the actual business logic in your handler.

The code is hard to understand and maintain. What if you need to reuse some of the non-functional logic in another handler?

With Middy

  import middy from '@middy/core'

const lambdaHandler = (event, context) => {
  // REAL BUSINESS LOGIC
  return doSomethingUsefulWith(event)
}

export const handler = middy(lambdaHandler)
  .use(/* Your own behaviour in a reusable fashion */)
  .use(/* logging and tracing */)
  .use(/* input validation */)
  .use(/* authentication */)
  .use(/* error handling */)
  .use(/* other behaviour */)

Middy helps you to keep all the non-functional code outside from your handler function.

The business logic in your handler remains pure and testable. All the non functional logic is isolated and reusable.

Do you want to see some more realistic examples?

See how Middy works across different AWS Lambda trigger types.

Ready to get started?

Install Middy in seconds and start building cleaner Lambda functions today.

Read the docs

Supported by the community

Middy is open-source and supported by amazing sponsors.