Building .NET 7 Lambda functions with Native AOT compilation - AWS Serverless Application Model

Building .NET 7 Lambda functions with Native AOT compilation

Build and package your .NET 7 AWS Lambda functions with the AWS Serverless Application Model (AWS SAM), utilizing Native Ahead-of-Time (AOT) compilation to improve AWS Lambda cold-start times.

.NET 7 Native AOT overview

Historically, .NET Lambda functions have cold-start times which impact user experience, system latency, and usage costs of your serverless applications. With .NET 7, Microsoft adds support for Native AOT compilation, which speeds up performance by compiling .NET code directly to machine and operating system native code, eliminating Just-in-Time (JIT) compiling at runtime. With .NET 7 Native AOT compilation, you can improve cold-start times of your Lambda functions. To learn more about Native AOT for .NET 7, see Using Native AOT in the Dotnet GitHub repository.

Using AWS SAM with your .NET 7 Lambda functions

Do the following to configure your .NET 7 Lambda functions with the AWS Serverless Application Model (AWS SAM):

  • Install prerequisites on your development machine.

  • Define .NET 7 Lambda functions in your AWS SAM template.

  • Build your application with the AWS SAM CLI.

Install prerequisites

The following are required prerequisites:

  • The AWS SAM CLI

  • The .NET Core CLI

  • The Amazon.Lambda.Tools .NET Core Global Tool

  • Docker

Install the AWS SAM CLI
  1. To check if you already have the AWS SAM CLI installed, run the following:

    sam --version
  2. To install the AWS SAM CLI, see Installing the AWS SAM CLI.

  3. To upgrade an installed version of the AWS SAM CLI, see Upgrading the AWS SAM CLI.

Install the .NET Core CLI
  1. To download and install the .NET Core CLI, see Download .NET from Microsoft's website.

  2. For more information on the .NET Core CLI, see .NET Core CLI in the AWS Lambda Developer Guide.

Install the Amazon.Lambda.Tools .NET Core Global Tool
  1. Run the following command:

    dotnet tool install -g Amazon.Lambda.Tools
  2. If you already have the tool installed, you can make sure that it is the latest version using the following command:

    dotnet tool update -g Amazon.Lambda.Tools
  3. For more information about the Amazon.Lambda.Tools .NET Core Global Tool, see the AWS Extensions for .NET CLI repository on GitHub.

Install Docker

Define .NET 7 Lambda functions in your AWS SAM template

To define a .NET7 Lambda function in your AWS SAM template, do the following:

  • Define a custom runtime by setting Runtime to provided.al2. We define a custom runtime because a .NET 7 runtime option is not available.

  • Add a Metadata object to your AWS::Serverless::Function resource and specify dotnet7 for the BuildMethod.

Resources: HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: function-source-folder Handler: app.handler Runtime: provided.al2 Architectures: - x86_64 Events: HelloWorld: Type: Api Properties: Path: /hello Method: get Metadata: BuildMethod: dotnet7
Note

For the Architectures property, x86_64 is the only supported option because .NET 7 cannot run on arm64. For more information, see the Reconsider .NET 7 build environment GitHub issue.

Build your application with the AWS SAM CLI

From your project's root directory, run sam build to begin building your application. If the PublishAot property has been defined in your .NET 7 project file, the AWS SAM CLI will build with Native AOT compilation. To learn more about the PublishAot property, see Native AOT Deployment in Microsoft's .NET documentation.

To build your function, the AWS SAM CLI invokes the .NET Core CLI which uses the Amazon.Lambda.Tools .NET Core Global Tool.

Note

When building, if a .sln file exists in the same or parent directory of your project, the directory containing the .sln file will be mounted to the container. If a .sln file is not found, only the project folder is mounted. Therefore, if you are building a multi-project application, ensure the .sln file is property located.

Learn more

For more information on building .NET 7 Lambda functions, see Building serverless applications on Lambda using .NET 7.

For a reference of the sam build command, see sam build.