Skip to main content

Warmer Utility for Lambda Function

Project description

This is a utility project designed to cater neccessities for warming up Lambda functions to prevent cold starts.

Table Of Contents

upload-artifacts-and-release-new-version PyPI version

Installing Warmer

To install module, run the below command:

python3 -m pip install py_lambda_warmer

# or

python3 -m pip install py_lambda_warmer==<release-version>

Using Warmer

This is very easy to incorporate in your existing Python Lambda Handlers. Follow the below code.

from warmer import warmer
@warmer(flag="custom_event_key", _concurrency=1)
def handler(event, context):
    pass

Parameters: flag (type: str)- name of the event flag to look for _concurrency (type: int)- (optional) Number of concurrent handlers to warm up, default: 1

If your handler is a Flask/FastApi application, you may follow below steps:

from warmer import warmer
from flask import Flask
app = Flask()
@warmer(flag="custom_event_key",_concurrency=1)
def application(event, context):
    return app(event, context)

# or

application = warmer(flag="custom_event_key",_concurrency=1)(app)

# you may now use application as your handler

warmer will help you cater the custom events that are coming for warming Lambda function. Though _concurrency is optional and by default it only warms up current execution. In case you want to warm up multiple instances of lambda handler, you may need to adjust _concurrency to number of handlers running.

Warmer uses threading mechnism to ensure that the warming calls are actually happening concurrently and not serially.

Setting up Event Bridge Notifications

You can also setup you custom event bridge schedule for Lambda function using the Terraform Resource code mentioned in the repository.

Simply download the Terraform code attached in the release and unzip it.

wget https://github.com/satyamsoni2211/LambdaWarmerPy/releases/download/${release}/terraform_code.zip
unzip terraform_code.zip -d terraform_code/
cd terraform_code/
# creating variable file required by terraform
cat << EOF > .auto.tfvars
arn = <arn of your lambda function>
profile = <profile alias for aws>
region = <region for aws lambda>
EOF
# initiating and applying
terraform init
terraform plan -out tfplan
terraform apply tfplan

You may also modify resource names as per your requirements in the code.

You may also use AWS SAM template to create Event Bridge Notifications to warm up Lambda function.

TransactionCompsAPI:
  Type: "AWS::Serverless::Function"
  Properties:
    FunctionName: fake-function
    Events:
      WarmerSchedule: # add this event to the same template
        Type: Schedule
        Properties:
          Schedule: cron(*/5 * ? * 2-6 *)
          Name: fake-function-warmer-event
          Description: Warmer Event for Lambda Function
          Enabled: true
          Input: '{"warmer": true}' # this refers to the warmer flag

In case you want to include concurrent executions, you may add below to include concurrent invocations.

TransactionCompsAPI:
  Type: "AWS::Serverless::Function"
  Properties:
    FunctionName: fake-function
    Events:
      WarmerSchedule: # add this event to the same template
        Type: Schedule
        Properties:
          Schedule: cron(*/5 * ? * 2-6 *)
          Name: fake-function-warmer-event
          Description: Warmer Event for Lambda Function
          Enabled: true
          Input: '{"warmer": true, "concurrency": 5}' # this refers to the warmer flag and concurrency

Working on enhancements

If you want to work on enhancements or development, you may clone the project and run the below commands to setup environment:

python -m pip install pipenv
pipenv shell

# or

python -m pip install virtualenv
virtualenv venv
source venv/bin/activate
python -m pip install -r dev_requirements.txt

You may also raise a PR to get merged into existing project.

Happy Warming.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_lambda_warmer-0.1.4.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

py_lambda_warmer-0.1.4-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file py_lambda_warmer-0.1.4.tar.gz.

File metadata

  • Download URL: py_lambda_warmer-0.1.4.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for py_lambda_warmer-0.1.4.tar.gz
Algorithm Hash digest
SHA256 a610a27b7ddca708943fdeab1cba53dc41b0140f8d3b75d4316b29392bb9ddb7
MD5 c1cbad63f90f5dfe98f39a15a0680922
BLAKE2b-256 e82713aaa85e48985cf3c512ea18c665769a64323d189e6785c6ab7d122924b2

See more details on using hashes here.

File details

Details for the file py_lambda_warmer-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for py_lambda_warmer-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e495400c676b5e7619ea3a3d2f845ee5e1ec8366d87dde99c68372841613c728
MD5 12ded8a7bb89565fd537305b95800413
BLAKE2b-256 bf0c5a9531c2fe7ed70eab769cecea4a5dcb5b93ee3f9a18435245c65db348da

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page