Skip to main content

PyPI version License: MIT Build Status

lambda-warmer-py: taking care of aws lambda cold starts

The lambda-warmer-py package contains a single decorator that makes it easy to minimize the drag of aws lambda cold starts. Just ...

  1. wrap your lambdas in the @lambdawarmer.warmer decorator and
  2. ping your lambda once every 5 minutes

and you'll cut your cold starts way down.

Configuration options are also available that ...

  • allow for keeping many concurrent lambdas warm
  • sending CloudWatch metrics tracking the number of cold and warm starts by lambda function name

The warming logic is a python adaption* of the js package, lambda-warmer. Read more about the background to this approach on his site here and some best practices on lambda optimization here.

* In addition to supporting CloudWatch Metrics, there are some small differences in parameterization. See configuration.

Install

pip install lambda-warmer-py

Using the lambda warmer

The basics

Incorporating the lambda warmer into your existing lambdas only requires adding a single decorator.

import lambdawarmer


@lambdawarmer.warmer
def your_lambda_function(event, context):
    pass

Concurrent warming

To leverage the concurrency options, the package will invoke your lambda multiple times. This means that the deployed lambda will need the following permissions

- Effect: Allow
  Action: lambda:InvokeFunction
  Resource: [your-lambdas-arn]

Enabling ColdStart/WarmStart CloudWatch Metrics

In order for the lambda warmer to track cold and warm start metrics, the lambda execution role will need permissions to send metric data to CloudWatch. The required policy action is

- Effect: Allow
  Action: cloudwatch:PutMetricData

Warming your lambdas

Create a CloudWatch Rule that periodically invokes your lambda directly and passes the following json as the event

{
    "warmer": true,
    "concurrency": (int, defaults to 1)
}

It is possible to change the warmer and concurrency names by overriding parameters in the warmer decorator. See configuration for details.

Configuration

The lambda warmer is configured via the function parameters for the @warmer decorator. It takes the following ...

flag (string, default = 'warmer')

Name of the field used to indicate that it is a warm up event.

concurrency (string, default = 'concurrency')

Name of the field used to set the number of concurrent lambdas to invoke and keep warm.

delay (int, default = 75)

Number of millis a concurrent warm up invocation should sleep. This helps avoid under delivering on the concurrency target.

send_metric (bool, default = False)

Whether or not CloudWatch Metrics for the number of cold/warm starts will be sent at each invocation. The metrics names are ColdStart and WarmStart, are recorded under LambdaWarmer namespace, and can be filtered by lambda function name.

Example: warmer configuration overrides

Using alternative event and delay configurations is straightforward.

@lambdawarmer.warmer(flag='am_i_a_warmer', concurrency='how_many_lambdas', delay=150)
def your_lambda_function(event, context):
    pass

This implementation will expect events of the form

{"am_i_a_warmer": true, "how_many_lambdas": (int)}

and all concurrent executions will delay for 150 milliseconds.

Example: sending metrics

If you want to track metrics on number of warm/cold starts activate that functionality in the decorator.

@lambdawarmer.warmer(send_metric=True)
def your_lambda_function(event, context):
    pass

Note: Configuration options that are excluded from this implementation but can be found in the js version are

  • test: Testing is handled in the unittests using mocks/fakes instead of flagged invocations
  • log: Logging levels of imported python packages should be handled via the stdlib logging module.
  • correlationId. This has been made into the snake cased correlation_id since we're in python and is always set to the current lambda's aws_request_id field as is recommended in the original lambda-warmer package.

Metadata

Release files for lambda-warmer-py 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for lambda-warmer-py 0.6.0
File Interpreter ABI Platform
lambda_warmer_py-0.6.0-py3-none-any.whl Python 3 none any Details

Release files / lambda_warmer_py-0.6.0-py3-none-any.whl

Download URL lambda_warmer_py-0.6.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
881ca23f2678abf22a56edd8328d7b2a0423f5e391052fdf931067cd1430512b
BLAKE2b-256 checksum
How to use checksums
b13fcf6c59d2392a7a2e52feda0fbef2832a2c95cf92cb01e3c46925281e1147
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.16

Release history Release notifications | RSS feed

This release

0.6.0 This release

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page