aws-lambda-dotnet icon indicating copy to clipboard operation
aws-lambda-dotnet copied to clipboard

Amazon.Lambda.TestTool-6.0 needs to work with dot net 6 top-level statements

Open aziz-shahed-cko opened this issue 2 years ago • 16 comments

With dot net 6 runtime support for lambda, we want to write lambda function handler as top level statement as described in the document https://aws.amazon.com/blogs/compute/introducing-the-net-6-runtime-for-aws-lambda/

To use Amazon.Lambda.TestTool-6.0 to test lambda written as top level statement, we need the test tool to support that.

Example code for lambda with top level statement for a project called LambdaProject

using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using Amazon.Lambda.S3Events;
using Amazon.S3;

// Code outside of the handler will be executed during Lambda initialization
var s3Client = new AmazonS3Client();

// The function handler that will be called for each Lambda event
var handler = async (S3Event evnt, ILambdaContext context) =>
{
    foreach(var record in evnt.Records)
    {
        using var response = await s3Client.GetObjectAsync(record.S3.Bucket.Name, record.S3.Object.Key);
        using var reader = new StreamReader(response.ResponseStream);
        // Run business logic on the text contexts of the S3 object
    }
};

// Build the Lambda runtime client passing in the handler to call for each
// event and the JSON serializer to use for translating Lambda JSON documents
// to .NET types.
await LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer())
        .Build()
        .RunAsync();

Now the .NET Lambda function handler string is LambdaProject

Currently Amazon.Lambda.TestTool-6.0 does not support this as we can see here https://github.com/aws/aws-lambda-dotnet/blob/0f7b6353466c398487a72264ab241ec04a9c4567/Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Runtime/LocalLambdaRuntime.cs#L96

Code is still trying to find function handler in <assembly>::<type-name>::<method> format.

  • [ ] :wave: I may be able to implement this feature request
  • [ ] :warning: This feature might incur a breaking change

This is a :rocket: Feature Request

aziz-shahed-cko avatar Mar 01 '22 22:03 aziz-shahed-cko

Needs review with the team.

ashishdhingra avatar Mar 02 '22 17:03 ashishdhingra

@ashishdhingra, do you know when this issue will be reviewed?

TheBlueSky avatar Jul 18 '22 09:07 TheBlueSky

This seems like a required feature to be able to use top level statements.

Which is sad, because the set up code for function based lambdas is ugly when you need async configuration. The sample app is calling async methods in initialization code without awaiting the result, which is a solved problem with top level statements.

avalara-stephen-hickey avatar Jul 29 '22 19:07 avalara-stephen-hickey

Is there any update on this issue? It seems a bit misleading that a blog was posted showing you could use top-level statements, but then the actual test tool is not working.

Or is this free to pick up?

rikvandenberg avatar Sep 14 '22 09:09 rikvandenberg

If i am not misunderstanding what you need, what you are talking about is.

The executable assembly test portion?

https://youtu.be/l4_WNjMHDx8?t=954

KoriSeng avatar Oct 13 '22 08:10 KoriSeng

I believe that is exactly what I was looking for, but I could not find any documentation on it in this project. Maybe update this issue to be about documenting how to use the "Executable Assembly" portion of the test tool.

avalara-stephen-hickey avatar Oct 13 '22 12:10 avalara-stephen-hickey

Basically..

  • Run the tool
  • Set environment variable
    • AWS_LAMBDA_RUNTIME_API
    • AWS_LAMBDA_FUNCTION_NAME
    • AWS_REGION

this will tell the lambda rest api middleware to activate and work with the tool

KoriSeng avatar Oct 17 '22 02:10 KoriSeng

I've confirmed I can use the test tool with an executing assembly. I'm still running into an issue but it's covered by #1198.

I think this issue can either be closed or used to say there needs to be documentation on how to use the executing assembly part of the test tool (and not as part of a YouTube video).

avalara-stephen-hickey avatar Oct 27 '22 20:10 avalara-stephen-hickey

@avalara-stephen-hickey Yes, now that we have the API emulation page for executable assemblies we really need to fix the tool to now require to be run in a project directory. That causes problems with the dev cycle.

normj avatar Oct 28 '22 01:10 normj

HI,

I am also trying to debug locally a .NET6 Minimal API lambda. I've followed the video posted here, but the events are not going to active.

I'm on:

  • Windows 10 21H1 Build 19043.2130
  • Visual Studio 2022 (64-bit) 17.4.1

The profile:

"Mock Lambda Test Tool": {
      "commandName": "Executable",
      "executablePath": "%USERPROFILE%\\.dotnet\\tools\\dotnet-lambda-test-tool-6.0.exe",
      "commandLineArgs": "--port 5050",
      "workingDirectory": ".\\bin\\$(Configuration)\\net6.0",
      "environmentVariables": {
        "AWS_LAMBDA_RUNTIME_API": "localhost:5050",
        "AWS_PROFILE": "default",
        "AWS_REGION": "us-west-2"
      }
    }

My project is just the default template serverless.AspNetCoreMinimalAPI, haven't changed a single line. The only thing I change is making the serverless.template file to be copied to the build output when newer, otherwise the launched page would show a FIleNotFoundException.

Whenever I debug it with the Mock Test Lambda Tool from Visual Studio, the program works and the web is shown. I click on Executable assembly and just write "hello", then click Queue event, and it goes directly into the Queued events list.

I have cleaned and rebuild the solution, manually removed the bin folder, restarted Visual Studio, but the events never go to the Active event list.

Is this the correct way to do it?

Also, how am I supposed to call a a controller method like http://localhost:5050/calculator/add/1/2 from that window? Is there a particular JSON format to be used?

Thanks

JoanComasFdz avatar Nov 30 '22 10:11 JoanComasFdz

I'm getting the following error when attempting to run the Lambda Test Tool with .net 6 Top Level Statements

Invalid format for function handler string <MyProjectName>. Format is <assembly>::<type-name>::<method>.

I added a launch settings as above.

and the sample code

using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;

// The function handler that will be called for each Lambda event
var handler = (string input, ILambdaContext context) =>
{
    return input.ToUpper();
};

// Build the Lambda runtime client passing in the handler to call for each
// event and the JSON serializer to use for translating Lambda JSON documents
// to .NET types.
await LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer())
        .Build()
        .RunAsync();

bcbeatty avatar Jul 11 '23 17:07 bcbeatty

HI,

I am also trying to debug locally a .NET6 Minimal API lambda. I've followed the video posted here, but the events are not going to active.

I'm on:

  • Windows 10 21H1 Build 19043.2130
  • Visual Studio 2022 (64-bit) 17.4.1

The profile:

"Mock Lambda Test Tool": {
      "commandName": "Executable",
      "executablePath": "%USERPROFILE%\\.dotnet\\tools\\dotnet-lambda-test-tool-6.0.exe",
      "commandLineArgs": "--port 5050",
      "workingDirectory": ".\\bin\\$(Configuration)\\net6.0",
      "environmentVariables": {
        "AWS_LAMBDA_RUNTIME_API": "localhost:5050",
        "AWS_PROFILE": "default",
        "AWS_REGION": "us-west-2"
      }
    }

My project is just the default template serverless.AspNetCoreMinimalAPI, haven't changed a single line. The only thing I change is making the serverless.template file to be copied to the build output when newer, otherwise the launched page would show a FIleNotFoundException.

Whenever I debug it with the Mock Test Lambda Tool from Visual Studio, the program works and the web is shown. I click on Executable assembly and just write "hello", then click Queue event, and it goes directly into the Queued events list.

I have cleaned and rebuild the solution, manually removed the bin folder, restarted Visual Studio, but the events never go to the Active event list.

Is this the correct way to do it?

Also, how am I supposed to call a a controller method like http://localhost:5050/calculator/add/1/2 from that window? Is there a particular JSON format to be used?

Thanks

@JoanComasFdz did you ever get a response? I'm seeing the exact same thing. The message goes into the queue, but nothing picks it up.

ncipollina avatar Mar 20 '24 18:03 ncipollina

@ncipollina Unfortunately no, i left it there.

JoanComasFdz avatar Mar 21 '24 06:03 JoanComasFdz

@aziz-shahed-cko Are you able to use guidance mentioned at https://github.com/aws/aws-lambda-dotnet/blob/master/Tools/LambdaTestTool/README.md#testing-executable-assemblies to use Executable Assembly to test top-level statements?

ashishdhingra avatar Apr 22 '24 21:04 ashishdhingra

This issue has not received a response in 5 days. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled.

github-actions[bot] avatar May 03 '24 00:05 github-actions[bot]

Hi @ashishdhingra , sorry for my late reply. I will be able to give your suggestion a try and update you next week

aziz-shahed-cko avatar May 03 '24 09:05 aziz-shahed-cko