.NET 6 introduced top-level statements, which simplify the entry point of the application code. Unlike the previous style of Lambda definitions, this project creates an executable instead of an assembly. That means we have to provide our own Lambda host implementation. Fortunately, AWS already provides one in the Amazon.Lambda.RuntimeSupport package.
The MinimalTopLevel project defined a Lambda function that takes a stream and returns an empty response. It has no business logic and only includes required libraries. There is also no deserialization of a payload. This is the Lambda function using top-level statements with the least amount of overhead.
using Amazon.Lambda.Core; using Amazon.Lambda.RuntimeSupport; using Amazon.Lambda.Serialization.SystemTextJson; await LambdaBootstrapBuilder.Create(Handler, new DefaultLambdaJsonSerializer()) .Build() .RunAsync(); Task Handler(Stream request, ILambdaContext context) => Task.CompletedTask;
Again, we see that the duration of the INIT phase is not impacted until we exceed the 3,008 MB threshold, which also drives up cost.
However, compared to the Minimal baseline project, this Lambda function has 20% to 100% longer cold start durations.
|Memory Size||Init||Cold Used||Total Cold Start||Total Warm Used (100)||Cost (µ$)|
Fortunately, the situation improves quite a bit once we look at the optimal configuration for minimum cold start duration. Using top-level statements shift some of the overhead from the INIT phase to the first INVOKE phase, but otherwise the total duration is very close to what was measured for the Minimal baseline project.
|Architecture||Memory Size||Tiered||Ready2Run||PreJIT||Init||Cold Used||Total Cold Start|
Again, the ARM64 architecture is the most cost-effective approach. ReadyToRun, Tiered Compilation, and the PreJIT settings all contribute to reduce cost a bit further for the minimal top-level project. That said, the minimum execution cost is ~8.5% higher when using top-level statements. This increased cost is most likely due to the higher memory configuration, which is required to compensate for the increased overhead of the INIT and first INVOKE phases.
|Architecture||Memory Size||Tiered||Ready2Run||PreJIT||Init||Cold Used||Total Warm Used (100)||Cost (µ$)|
It's been fun diving into the fundamentals of AWS Lambda for .NET functions. The post is summarizing my findings and thoughts on future projects that might be interesting to explore.