Sitemap
Tom Wilson

Tom Wilson

Software Engineer @ PieCloud

Enabling SnapStart in AWS API Gateway with Lambda

2 min readJan 12, 2024

--

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 setup 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 SnapStart is Working

  1. If CloudWatch logs console shows [$LATEST] in the logs, SnapStart is NOT working because SnapStart only works for Versioned Lamda calls.
Press enter or click to view image in full size

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

Get Tom Wilson’s stories in your inbox

Join Medium for free to get updates from this writer.

Press enter or click to view image in full size

3. If logs show RESTORE_START, SnapStart is working.

Press enter or click to view image in full size

Enabling SnapStart Manually

  1. Create Alias for Lambda
Press enter or click to view image in full size

2. Set SnapStart to PublishedVersions

Press enter or click to view image in full size

3. Point API Gateway to Alias in Lambda Integration

Press enter or click to view image in full size

--

--

Tom Wilson
Tom Wilson

Written by Tom Wilson

Software Engineer @ PieCloud