@middy/warmup

Warmup (cold start mitigation) middleware for the middy framework

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@middy/warmup
3,04183.5.02 days ago4 years agoMinified + gzip package size for @middy/warmup in KB

Readme

Middy warmup middleware
Middy logo

Warmup middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda

<img src="https://badge.fury.io/js/%40middy%2Fwarmup.svg" alt="npm version" style="max-width:100%;">
<img src="https://packagephobia.com/badge?p=@middy/warmup" alt="npm install size" style="max-width:100%;">
<img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">

<a href="https://standardjs.com/">
 <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style"  style="max-width:100%;">
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices"  style="max-width:100%;">

<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">

You can read the documentation at: https://middy.js.org/docs/middlewares/warmup


Warmup middleware that helps to reduce the cold-start issue. Compatible by default with serverless-plugin-warmup, but it can be configured to suit your implementation.
This middleware allows you to specify a schedule to keep Lambdas that always need to be very responsive warmed-up. It does this by regularly invoking the Lambda, but will terminate early to avoid the actual handler logic from being run.
If you use serverless-plugin-warmup the scheduling part is done by the plugin and you just have to attach the middleware to your "middyfied" handler. If you don't want to use the plugin you have to create the schedule yourself and define the isWarmingUp function to define whether the current event is a warmup event or an actual business logic execution.
Important: AWS recently announced Lambda Provisioned Concurrency. If you have this enabled, you do not need this middleware.
To update your code to use Provisioned Concurrency see:

Install

To install this middleware you can use NPM:
npm install --save @middy/warmup

Options

- isWarmingUp: (function) a function that accepts the event object as a parameter
and returns `true` if the current event is a warmup event and `false` if it's a regular execution. The default function will check if the `event` object has a `source` property set to `serverless-plugin-warmup`.

Sample usage

import middy from '@middy/core'
import warmup from '@middy/warmup'

const isWarmingUp = (event) => event.isWarmingUp === true

const originalHandler = (event, context, cb) => {
  /* ... */
}

const handler = middy(originalHandler)
  .use(warmup({ isWarmingUp }))

Middy documentation and examples

For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.

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-2022 Luciano Mammino, will Farrell, and the Middy team.
FOSSA Status