Skip to content

Feature request: Support correct cold start detection for non-on-demand invocation types #2113

@phipag

Description

@phipag

Use case

When using provisioned concurrency or other non-on-demand invocation types in Lambda functions, the first invocation is not necessarily a cold start. For instance, SnapStart doesn't have cold starts at all (only restores).

Currently, Powertools incorrectly identifies these scenarios as cold starts, leading to:

  • Cold start metrics being emitted when they shouldn't be
  • JSON logs showing cold_start: true field incorrectly
  • Traces having ColdStart: true annotation when inappropriate

This creates inaccurate observability data for Lambda functions using provisioned concurrency or SnapStart.

Solution/User Experience

All function executions which do not have AWS_LAMBDA_INITIALIZATION_TYPE="on-demand" should:

  • NOT emit a cold start metric
  • NOT set cold_start: true in the structured JSON logs
  • NOT set the ColdStart annotation in traces

Implementation approach:
Use the AWS_LAMBDA_INITIALIZATION_TYPE environment variable within the utilities to suppress cold start behavior if the init type is not on-demand. Consider all init types from the AWS docs: https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime

Update the global LambdaHandlerProcessor to have more nuanced cold start detection logic:

Code example:

@Logging(logEvent = true)
@Tracing
@FlushMetrics(namespace = "TriagingTemplate", service = "triage", captureColdStart = true)
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
    // With provisioned concurrency, this should NOT be marked as cold start
    logger.info("Processing triage request");
    metrics.addMetric("TriageRequests", 1, MetricUnit.COUNT);
    // ... rest of handler
}

Reproduction steps:
Deploy a Lambda function with provisioned concurrency using this SAM template. You can use the Powertools triaging template for this: https://github.com/phipag/powertools-triaging-template-java

TriageFunction:
  Type: AWS::Serverless::Function
  Properties:
    CodeUri: .
    Handler: triage.TriageHandler::handleRequest
    AutoPublishAlias: live
    ProvisionedConcurrencyConfig:
      ProvisionedConcurrentExecutions: 1

Alternative solutions

Using AWS_LAMBDA_INITIALIZATION_TYPE is the most reliable and AWS-native approach.

Acknowledgment


Future readers: Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions