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.
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);
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.
If CloudWatch has INIT_START in logs, SnapStart is NOT working
If logs show RESTORE_START, SnapStart is working.
Create Alias for Lambda
Set SnapStart to PublishedVersions
Point API Gateway to Alias in Lambda Integration