Package: py-lambda-warmer
This is a utility project designed to cater neccessities for warming upLambdafunctions to prevent cold starts.Table Of ContentsTable Of ContentsInstalling WarmerUsing WarmerSetting up Event Bridge NotificationsWorking on enhancementsInstalling WarmerTo install module, run the below command:python3-mpipinstallpy_lambda_warmer# orpython3-mpipinstallpy_lambda_warmer==<release-version>This does not specifyboto3library as hard core requirements while installing as it expects
lambda environment to already have it in orderto reduce the layer sizecreated. 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 WarmerThis is very easy to incorporate in your existing Python Lambda Handlers. Follow the below code.fromwarmerimportwarmer@warmer(flag="custom_event_key",_concurrency=1)defhandler(event,context):passParameters:flag(type: str)- name of the event flag to look for_concurrency(type: int)- (optional) Number of concurrent handlers to warm up, default: 1If your handler is a Flask/FastApi application, you may follow below steps:fromwarmerimportwarmerfromflaskimportFlaskapp=Flask()@warmer(flag="custom_event_key",_concurrency=1)defapplication(event,context):returnapp(event,context)# orapplication=warmer(flag="custom_event_key",_concurrency=1)(app)# you may now use application as your handlerwarmerwill help you cater the custom events that are coming for warmingLambdafunction.
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_concurrencytonumber of handlers running.Warmeruses threading mechnism to ensure that the warming calls are actually happening concurrently and not serially.Setting up Event Bridge NotificationsYou can also setup you custom event bridge schedule for Lambda function using theTerraform Resourcecode mentioned in
the repository.Simply download theTerraformcode attached in the release and unzip it.Note:Make sure your function execution role has required permission to invoke yourLambdafunction else concurrent call executions might fail.wgethttps://github.com/satyamsoni2211/LambdaWarmerPy/releases/download/${release}/terraform_code.zip
unzipterraform_code.zip-dterraform_code/cdterraform_code/# creating variable file required by terraformcat<< EOF > .auto.tfvarsarn = <arn of your lambda function>profile = <profile alias for aws>region = <region for aws lambda>EOF# initiating and applyingterraforminit
terraformplan-outtfplan
terraformapplytfplanYou may also modify resource names as per your requirements in the code.You may also useAWS SAMtemplate to createEvent Bridge Notificationsto warm upLambdafunction.TransactionCompsAPI:Type:"AWS::Serverless::Function"Properties:FunctionName:fake-functionPolicies:-LambdaInvokePolicy:FunctionName:"fake-function"Events:WarmerSchedule:# add this event to the same templateType:ScheduleProperties:Schedule:cron(*/5 * ? * 2-6 *)Name:fake-function-warmer-eventDescription:Warmer Event for Lambda FunctionEnabled:trueInput:'{"warmer":true}'# this refers to the warmer flagIn 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 calllambda:invokeFunctionaction on theLambdafunction. Below code snippet includes policy to grant action to the role.TransactionCompsAPI:Type:"AWS::Serverless::Function"Properties:FunctionName:fake-functionPolicies:-LambdaInvokePolicy:FunctionName:"fake-function"Events:WarmerSchedule:# add this event to the same templateType:ScheduleProperties:Schedule:cron(*/5 * ? * 2-6 *)Name:fake-function-warmer-eventDescription:Warmer Event for Lambda FunctionEnabled:trueInput:'{"warmer":true,"concurrency":5}'# this refers to the warmer flag and concurrencyWorking on enhancementsIf you want to work on enhancements or development, you may clone the project and run the below commands to setup environment:python-mpipinstallpipenv
pipenvshell# orpython-mpipinstallvirtualenv
virtualenvvenvsourcevenv/bin/activate
python-mpipinstall-rdev_requirements.txtYou may also raise aPRto get merged into existing project.Happy Warming.