Improving Lambda Custom Runtime Cold Start and Deployment Speeds with .NET 6

Want to learn more about AWS Lambda and .NET? Check out my A Cloud Guru course on ASP.NET Web API and Lambda.

Full source code available here

I have a few other posts on running .NET 6 on AWS Lambda.

This post builds on the previous one where I showed how to deploy .NET 6 application on AWS Lambda using a custom runtime. That one walked through all the steps to create the .NET 6 project, and deploy the Lambda, so I won’t repeat them here.

In this post I’m going to briefly talk about two features in .NET that help with Lambda cold start and deployment speeds, these are Publish Trimmed, and ReadyToRun. Other features will impact the size of your deployment package, so it is important to read the above documents and experiment with your application to find the settings that are best for you.

Publish Trimmed reduces the size of the package that is deployed by removing unused parts of libraries that would otherwise be included. ReadyToRun performs an ahead of time compilation, reducing startup delays, but increasing the size of the package.

These changes are most beneficial when using the custom runtime. When the managed runtime for .NET 6 is available on Lambda, they won’t be as important.

Changes to the .csproj file

To enable these features open the .csproj file, and add the following to the PropertyGroup -

<PublishReadyToRun>true</PublishReadyToRun>
<PublishTrimmed>true</PublishTrimmed>

Then redeploy the Lambda by running dotnet lambda deploy-function YourLambda.

In the case of my .NET 6 application from the previous post, my deployed package size was 31.4 MB, with the PublishTrimmed option enabled, it is down to 14.4 MB. In a few, very unscientific tests my cold start times improved by about 30%, but don’t take this number to the bank.

Not bad for two lines of configuration, and of course all the work of the .NET team, thank you!

Full source code available here

comments powered by Disqus

Related