Lambda SnapStart runtime hooks for .NET - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Lambda SnapStart runtime hooks for .NET

You can use runtime hooks to implement code before Lambda creates a snapshot or after Lambda resumes a function from a snapshot. .NET runtime hooks are available as part of the Amazon.Lambda.Core package (version 2.5.0 or later). This library provides two methods that you can use to define your runtime hooks:

  • RegisterBeforeSnapshot(): Code to run before snapshot creation

  • RegisterAfterSnapshot(): Code to run after resuming a function from a snapshot

Note

If you're using the Lambda Annotations framework for .NET, upgrade to Amazon.Lambda.Annotations version 1.6.0 or later to ensure compatibility with SnapStart.

Runtime hook registration and execution

Register your hooks in your initialization code. Consider the following guidelines based on your Lambda function's execution model:

To register runtime hooks for SnapStart in .NET, use the following methods:

Amazon.Lambda.Core.SnapshotRestore.RegisterBeforeSnapshot(BeforeCheckpoint); Amazon.Lambda.Core.SnapshotRestore.RegisterAfterRestore(AfterCheckpoint);

When multiple hook types are registered, the order that Lambda executes your runtime hooks is determined by the order of registration:

  • RegisterBeforeSnapshot(): Executed in the reverse order of registration

  • RegisterAfterSnapshot(): Executed in the order of registration

Note

When Lambda creates a snapshot, your initialization code can run for up to 15 minutes. The time limit is 130 seconds or the configured function timeout (maximum 900 seconds), whichever is higher. Your RegisterBeforeSnapshot() runtime hooks count towards the initialization code time limit. When Lambda restores a snapshot, the runtime must load and RegisterAfterSnapshot() runtime hooks must complete within the timeout limit (10 seconds). Otherwise, you'll get a SnapStartTimeoutException.

Example

The following example function shows how to run code before checkpointing (RegisterBeforeSnapshot) and after restoring (RegisterAfterRestore).

public class SampleClass { public SampleClass() { Amazon.Lambda.Core.SnapshotRestore.RegisterBeforeSnapshot(BeforeCheckpoint); Amazon.Lambda.Core.SnapshotRestore.RegisterAfterRestore(AfterCheckpoint); } private ValueTask BeforeCheckpoint() { // Add logic to be executed before taking the snapshot return ValueTask.CompletedTask; } private ValueTask AfterCheckpoint() { // Add logic to be executed after restoring the snapshot return ValueTask.CompletedTask; } public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context) { // Add business logic return new APIGatewayProxyResponse { StatusCode = 200 }; } }