Keeping Lambda Warm

Reducing cold starts with Provisioned Concurrency

Understanding how functions warmers work

However, this is not a guaranteed way to reduce cold starts. It does not help in production environments when functions scale up to meet traffic. It also does not work if the Lambda service runs your function in another Availability Zone as part of normal load-balancing operations. Additionally, the Lambda service reaps execution environments regularly to keep these fresh, so it’s possible to invoke a function in between pings. In all of these cases, you experience cold starts despite using a warming library. This approach might be adequate for development and test environments, or low-traffic or low-priority workloads.

Calculate cost of provisioned concurrency

Example cloudformation code (use logical name for reference):

Resources:
  function:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler
      Role: arn:aws:iam::123456789012:role/lambda-role
      Code:
        ZipFile: |
          exports.handler = async (event) => {
              console.log(JSON.stringify(event, null, 2));
              const response = {
                  statusCode: 200,
                  body: JSON.stringify('Hello from Lambda!'),
              };
              return response;
          };
      Runtime: nodejs18.x
      TracingConfig:
        Mode: Active
  version:
    Type: AWS::Lambda::Version
    Properties:
      FunctionName: !Ref function
      Description: v1
      ProvisionedConcurrencyConfig:
        ProvisionedConcurrentExecutions: 20

 

You must specify a version of lambda to use when using povisioned concurrency. Here’s a post on how to connection lambda verions involving API Gateway.

Using latest with provisioned concurrency from AWS

You cannot use provisioned concurrency with the $LATEST version of any function.

In addition, if you’re using an event source with your Lambda function, make sure that event source points to the correct alias or version. Otherwise, your function won’t use provisioned concurrency environments.