You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
# AWS Lambda Native AOT Project with Powertools for AWS Lambda (.NET)
2
+
3
+
This starter project consists of:
4
+
* Function.cs - contains a class with a `Main` method that starts the bootstrap and a single function handler method.
5
+
* aws-lambda-tools-defaults.json - default argument settings for use with Visual Studio and command line deployment tools for AWS.
6
+
7
+
You may also have a test project depending on the options selected.
8
+
9
+
The `Main` function is called once during the Lambda init phase. It initializes the .NET Lambda runtime client passing in the function
10
+
handler to invoke for each Lambda event and the JSON serializer to use for converting Lambda JSON format to the .NET types.
11
+
12
+
The function handler is a simple method accepting a string argument that returns the uppercase equivalent of the input string. Replace the body of this method and its parameters to suit your needs.
13
+
14
+
## Native AOT
15
+
16
+
Native AOT is a feature that compiles .NET assemblies into a single native executable. By using the native executable the .NET runtime
17
+
is not required to be installed on the target platform. Native AOT can significantly improve Lambda cold starts for .NET Lambda functions.
18
+
This project enables Native AOT by setting the .NET `PublishAot` property in the .NET project file to `true`. The `StripSymbols` property is also
19
+
set to `true` to strip debugging symbols from the deployed executable to reduce the executable's size.
20
+
21
+
### Building Native AOT
22
+
23
+
When publishing with Native AOT the build OS and Architecture must match the target platform that the application will run. For AWS Lambda that target
24
+
platform is Amazon Linux 2023. The AWS tooling for Lambda like the AWS Toolkit for Visual Studio, .NET Global Tool Amazon.Lambda.Tools and SAM CLI will
25
+
perform a container build using a .NET 8 Amazon Linux 2023 build image when `PublishAot` is set to `true`. This means **docker is a requirement**
26
+
when packaging .NET Native AOT Lambda functions on non-Amazon Linux 2023 build environments. To install docker go to https://www.docker.com/.
27
+
28
+
### Trimming
29
+
30
+
As part of the Native AOT compilation, .NET assemblies will be trimmed removing types and methods that the compiler does not find a reference to. This is important
31
+
to keep the native executable size small. When types are used through reflection this can go undetected by the compiler causing necessary types and methods to
32
+
be removed. When testing Native AOT Lambda functions in Lambda if a runtime error occurs about missing types or methods the most likely solution will
33
+
be to remove references to trim-unsafe code or configure [trimming options](https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options).
34
+
This sample defaults to partial TrimMode because currently the AWS SDK for .NET does not support trimming. This will result in a larger executable size, and still does not
35
+
guarantee runtime trimming errors won't be hit.
36
+
37
+
For information about trimming see the documentation: <https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trim-self-contained>
38
+
39
+
## Docker requirement
40
+
41
+
Docker is required to be installed and running when building .NET Native AOT Lambda functions on any platform besides Amazon Linux 2023. Information on how acquire Docker can be found here: https://docs.docker.com/get-docker/
42
+
43
+
## Here are some steps to follow from Visual Studio:
44
+
45
+
To deploy your function to AWS Lambda, right click the project in Solution Explorer and select *Publish to AWS Lambda*.
46
+
47
+
To view your deployed function open its Function View window by double-clicking the function name shown beneath the AWS Lambda node in the AWS Explorer tree.
48
+
49
+
To perform testing against your deployed function use the Test Invoke tab in the opened Function View window.
50
+
51
+
To configure event sources for your deployed function, for example to have your function invoked when an object is created in an Amazon S3 bucket, use the Event Sources tab in the opened Function View window.
52
+
53
+
To update the runtime configuration of your deployed function use the Configuration tab in the opened Function View window.
54
+
55
+
To view execution logs of invocations of your function use the Logs tab in the opened Function View window.
56
+
57
+
## Here are some steps to follow to get started from the command line:
58
+
59
+
Once you have edited your template and code you can deploy your application using the [Amazon.Lambda.Tools Global Tool](https://github.com/aws/aws-extensions-for-dotnet-cli#aws-lambda-amazonlambdatools) from the command line. Version 5.6.0
60
+
or later is required to deploy this project.
61
+
62
+
Install Amazon.Lambda.Tools Global Tools if not already installed.
63
+
```
64
+
dotnet tool install -g Amazon.Lambda.Tools
65
+
```
66
+
67
+
If already installed check if new version is available.
0 commit comments