Building .NET Lambda functions with Native AOT compilation in Amazon SAM - Amazon Serverless Application Model
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).

Building .NET Lambda functions with Native AOT compilation in Amazon SAM

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

.NET 8 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 Native AOT compilation, you can improve cold-start times of your Lambda functions. To learn more about Native AOT for .NET 8, see Using Native AOT in the Dotnet GitHub repository.

Using Amazon SAM with your .NET 8 Lambda functions

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

  • Install prerequisites on your development machine.

  • Define .NET 8 Lambda functions in your Amazon SAM template.

  • Build your application with the Amazon SAM CLI.

Install prerequisites

The following are required prerequisites:

  • The Amazon SAM CLI

  • The .NET Core CLI

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

  • Docker

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

    sam --version
  2. To install the Amazon SAM CLI, see Install the Amazon SAM CLI.

  3. To upgrade an installed version of the Amazon SAM CLI, see Upgrading the Amazon 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 Amazon 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 Amazon Extensions for .NET CLI repository on GitHub.

Install Docker

Define .NET 8 Lambda functions in your Amazon SAM template

To define a .NET8 Lambda function in your Amazon SAM template, do the following:

  1. Run the following command from a starting directory of your choice::

    sam init
  2. Select Amazon Quick Start Templates to choose a starting template.

  3. Choose the Hello World Example template.

  4. Choose to not use the most popular runtime and package type by entering n.

  5. For runtime, choose dotnet8.

  6. For package type, choose Zip.

  7. For your starter template, choose Hello World Example using native AOT.

Install Docker
Resources: HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src/HelloWorldAot/ Handler: bootstrap Runtime: dotnet8 Architectures: - x86_64 Events: HelloWorldAot: Type: Api Properties: Path: /hello Method: get
Note

When the Event property of an AWS::Serverless::Function is set to Api, but the RestApiId property is not specified, Amazon SAM generates the AWS::ApiGateway::RestApi Amazon CloudFormation resource.

Build your application with the Amazon 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 8 project file, the Amazon 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 Amazon 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 8 Lambda functions, see Introducing the .NET 8 runtime for Amazon Lambda.

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