This is a utility project designed to cater neccessities for warming up Lambda functions to prevent cold starts.
Table Of Contents
- Table Of Contents
- Installing Warmer
- Using Warmer
- Setting up Event Bridge Notifications
- Working on enhancements
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>
This does not specify
boto3library as hard core requirements while installing as it expects lambda environment to already have it in order to reduce the layer size created. This would not be the case with development. You will have to explicitely install dependencies usingdev_requirements.txtfile. ( Changes made since release 0.1.5 )`
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
warmerwill help you cater the custom events that are coming for warming Lambda function. Though_concurrencyis 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_concurrencyto number of handlers running.
Warmeruses 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.
Note: Make sure your function execution role has required permission to invoke your
Lambdafunction else concurrent call executions might fail.
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
Policies:
- LambdaInvokePolicy:
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.
Note: Using concurrency would also require you to add sufficient permissions to the role to call
lambda:invokeFunctionaction on theLambdafunction. Below code snippet includes policy to grant action to the role.
TransactionCompsAPI:
Type: "AWS::Serverless::Function"
Properties:
FunctionName: fake-function
Policies:
- LambdaInvokePolicy:
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.
Metadata
Release files for py-lambda-warmer 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| py_lambda_warmer-0.1.6.tar.gz | 5.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_lambda_warmer-0.1.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.8 kB
Release files / py_lambda_warmer-0.1.6.tar.gz
| Download URL | py_lambda_warmer-0.1.6.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
feebd50af37d7c8fb9e94cbad949691ad62c05b992b1663695955bc71aad3eb2
|
|
BLAKE2b-256 checksum How to use checksums |
d767cc5e66f8f69542f40481c6ad745faf549011a43344d1153b0a9da3922fac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Release files / py_lambda_warmer-0.1.6-py3-none-any.whl
| Download URL | py_lambda_warmer-0.1.6-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3184e7847bd5ed9377637cc0895452be6666b6dff478120c05398b286f610b2b
|
|
BLAKE2b-256 checksum How to use checksums |
9cd6c57df7bead49094c8c65d63178b53ce4a7a9eb0fa5dab4b1c50fefcd884d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|