Articles

Enabling SnapStart in AWS API Gateway with Lambda

Cold start time was a problem for PieCloud because it was adding extra latency to less frequently used APIs. We enabled SnapStart and it improved start up performance 7x at no extra cost.

Setting Up SnapStart with CDK

We use CDK to set up infrastructure. Code for enabling SnapStart using CDK. Currently, only X86_64 is supported for Java.



this.function = new Function(scope, 'SampleFunction', {
        functionName: 'SampleFunction',
        runtime: Runtime.JAVA_11,
        handler: props.handler,
        code: "Code path",
        architecture: Architecture.X86_64,
        memorySize: 1024,
        timeout: Duration.seconds(15),
        logRetention: RetentionDays.ONE_MONTH
    });


const version = this.function.currentVersion;
const funcAlias = new Alias(scope, 'SampleFunctionAlias', {
    aliasName: 'Prod',
    version,
});


(this.function.node.defaultChild as CfnFunction).addPropertyOverride('SnapStart', {
    ApplyOn: 'PublishedVersions',
  });
const lambdaIntegration = new LambdaIntegration(funcAlias, Integration options);

How To Verify If SnapStart is Working

  1. If CloudWatch logs console shows [$LATEST] in the logs, SnapStart is NOT working because SnapStart only works for Versioned Lambda calls. Logs should have number such as '2024/01/11/[2]', [2] is the version.



  2. If CloudWatch has INIT_START in logs, SnapStart is NOT working



  3. If logs show RESTORE_START, SnapStart is working.



Enabling SnapStart Manually

  1. Create Alias for Lambda



  2. Set SnapStart to PublishedVersions



  3. Point API Gateway to Alias in Lambda Integration