.netamazon-web-servicesaws-lambdanative-aot

Lambda Mock Test Tool .NET 8.0 Native AOT


I started a project using dotnet new serverless.NativeAOT, so my project is using the annotations framework as well as compiling to Native AOT.

I can deploy my function to an AWS Lambda, but it throws a null reference exception. I have been trying to set up the mock lambda testing tool, so I can set up some breakpoints and diagnose this, but all my attempts have come up dry. I'm using Visual Studio 2022 and can get the mock test tool to launch, but events queued in the Executable Assembly tab just hang there and do not hit the breakpoints I set up in Startup.cs nor Functions.cs.

File launchSettings.json

{
    "profiles": {
      "Lambda Runtime API": {
        "commandName": "Executable",
        "commandLineArgs": "--port 5050",
        "executablePath": "/Users/xxx/.dotnet/tools/dotnet-lambda-test-tool-8.0.exe",
        "workingDirectory": "/Users/xxx/source/repos/SendTunesServerless/SendTunesServerless/src/SendTunesServerless/bin/Debug/net8.0",
        "environmentVariables": {
          "AWS_LAMBDA_RUNTIME_API": "localhost:5050",
          "AWS_PROFILE": "default",
          "AWS_REGION": "us-east-1"
        }
      }
    }
  }

File Startup.cs

 [LambdaStartup]
 public class Startup
 {

     public void ConfigureServices(IServiceCollection services)
     {
         services.AddSingleton<ISpotifyAccessTokenService, SpotifyAccessTokenService>();

         services.AddHttpClient<ISpotifyService, SpotifyService>(client =>
         {
             client.BaseAddress = new Uri("https://api.spotify.com/v1/");
         });
     }
 }

Has anyone been able to successfully test a .NET 8.0 Native AOT using the lambda mock test tool?

I've gone through The AWS .NET Mock Lambda Test Tool, Configure for Visual Studio and the testing Executable Assemblies reference in the tool. I'd expect a breakpoint to be hit or an error to be thrown, but the events just sitting in the queue makes me think my setup is incorrect.


Solution

  • I was able to replicate the issue you described, and I have found a solution. Here are the steps I took:

    1. Created a new project with the same template (as you used) from Visual Studio.

      enter image description here

    2. After that, just follow the steps I mentioned at Step 7. Debug and test Lambda Functions in Visual Studio

    3. Updated launchSettings.json file with a new environment variable as explained below.

    Debugging Executable Assembly .NET Lambda functions (Annotations Framework)

    The handler name for Lambda functions based on Class Libraries follows the syntax ASSEMBLY::TYPE::METHOD. This specifies the assembly, class, and method that will be executed.

    For Lambda functions based on Executable Assemblies, the handler name is simply the assembly name.

    In the case of Executable Assemblies, the method name needs to be specified in an environment variable. This environment variable is then used to pass the appropriate delegate method to the auto-generated Main method.

    Solution

    As a solution, you need to add one more environment variable ANNOTATIONS_HANDLER in launchSettings.json file.

    {
      "profiles": {
        "AWSServerlessNativeAOT.Example": {
          "commandName": "Project",
          "environmentVariables": {
            "AWS_LAMBDA_RUNTIME_API": "localhost:5050",
            "ANNOTATIONS_HANDLER": "GetFunctionHandler"
          }
        }
      }
    }
    

    Because, if you see the auto generated Program.cs file, you will find that it expects this environment variable to determine which Function to execute.

    enter image description here