Package: lambda-warmer-py
lambda-warmer-py: taking care of aws lambda cold startsThelambda-warmer-pypackage contains a single decorator that makes it easy to minimize the drag of aws lambda cold
starts. Just ...wrap your lambdas in the@lambdawarmer.warmerdecorator andping your lambda once every 5 minutesand you'll cut your cold starts way down.Configuration options are also available that ...allow for keeping manyconcurrentlambdas warmsendingCloudWatch metricstracking the number of cold and warm starts by lambda function nameThe warming logic is a python adaption* of thejspackage,lambda-warmer. Read more about the background to this approach on his sitehereand some best practices on lambda optimizationhere.* In addition to supporting CloudWatch Metrics, there are some small differences in parameterization. Seeconfiguration.Installpipinstalllambda-warmer-pyUsing the lambda warmerThe basicsIncorporating the lambda warmer into your existing lambdas only requires adding a single decorator.importlambdawarmer@lambdawarmer.warmerdefyour_lambda_function(event,context):passConcurrent warmingTo leverage the concurrency options, the package will invoke your lambda multiple times. This means that the deployed
lambda will need the following permissions-Effect:AllowAction:lambda:InvokeFunctionResource:[your-lambdas-arn]Enabling ColdStart/WarmStart CloudWatch MetricsIn 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:AllowAction:cloudwatch:PutMetricDataWarming your lambdasCreate aCloudWatch Rulethat
periodically invokes your lambda directly and passes the following json as the event{"warmer":true,"concurrency":(int,defaultsto1)}It is possible to change thewarmerandconcurrencynames by overriding parameters in thewarmerdecorator. Seeconfigurationfor details.ConfigurationThe lambda warmer is configured via the function parameters for the@warmerdecorator. 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
areColdStartandWarmStart, are recorded underLambdaWarmernamespace, and can be filtered by lambda function name.Example: warmer configuration overridesUsing alternative event and delay configurations is straightforward.@lambdawarmer.warmer(flag='am_i_a_warmer',concurrency='how_many_lambdas',delay=150)defyour_lambda_function(event,context):passThis 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 metricsIf you want to track metrics on number of warm/cold starts activate that functionality in the decorator.@lambdawarmer.warmer(send_metric=True)defyour_lambda_function(event,context):passNote: Configuration options that areexcludedfrom this implementation but can be found in thejsversion aretest: Testing is handled in the unittests using mocks/fakes instead of flagged invocationslog: Logging levels of imported python  packages should be handled via the stdlibloggingmodule.correlationId. This has been made into the snake casedcorrelation_idsince we're in python and is always set to
the current lambda'saws_request_idfield as is recommended in the originallambda-warmerpackage.