aws / aws-lambda-dotnet

Libraries, samples and tools to help .NET Core developers develop AWS Lambda functions.
Apache License 2.0
1.58k stars 478 forks source link

Using Amazon.Lambda.Core 2.4.0 breaks running locally with Amazon.Lambda.TestTool-8.0 0.15.3 #1869

Open thomaswr opened 1 day ago

thomaswr commented 1 day ago

Describe the bug

I want to update Amazon.Lambda.Core in order to use JSON logging but this currently breaks running the lambda locally via LambdaTestTool.

Regression Issue

Expected Behavior

I want to update Amazon.Lambda.Core in order to use JSON logging but this currently breaks running the lambda locally via LambdaTestTool invokes lambda and I get JSON formatted as described in the blog post: https://aws.amazon.com/blogs/developer/structured-logging-for-net-lambda/

Current Behavior

System.MissingMethodException: Method not found: 'Void Amazon.Lambda.Core.ILambdaLogger.LogInformation(System.String, System.Object[])'.
   at unzip_ftp_upload.Function.FunctionHandler(CloudWatchEvent`1 input, ILambdaContext context)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
   at unzip_ftp_upload.Function.FunctionHandler(CloudWatchEvent`1 input, ILambdaContext context)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)

Press any key to exit
Unknown error occurred causing process exit: Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.
   at System.ConsolePal.ReadKey(Boolean intercept)
   at Amazon.Lambda.TestTool.TestToolStartup.ExecuteWithNoUi(LocalLambdaOptions localLambdaOptions, CommandLineOptions commandOptions, String lambdaAssemblyDirectory, RunConfiguration runConfiguration) in C:\build\Tools\LambdaTestTool\src\Amazon.Lambda.TestTool\TestToolStartup.cs:line 175
   at Amazon.Lambda.TestTool.TestToolStartup.Startup(String productName, Action`2 uiStartup, String[] args, RunConfiguration runConfiguration) in C:\build\Tools\LambdaTestTool\src\Amazon.Lambda.TestTool\TestToolStartup.cs:line 79

Reproduction Steps

Implement Lambda using Amazon.Lambda.Core 2.4.0, making use of the ILambdaContext.Logger and using the second parameter of e.g. LogInformation. Example: context.Logger.LogInformation("User name is: {user}", user); Then run it with the LambdaTestTool

Possible Solution

Update Amazon.Lambda.Core Dependency in LambdaTestTool to 2.4.0

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

Amazon.Lambda.TestTool-8.0 0.15.3

Targeted .NET Platform

.NET 8

Operating System and version

Ubuntu

ashishdhingra commented 1 day ago

Issue reproducible using Lambda Test Tool 8.0 version 0.15.3 when we use structured logging as shown below: Function.cs

using Amazon.Lambda.Core;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace TestNet8Lambda;

public class Function
{
    public string FunctionHandler(string input, ILambdaContext context)
    {
        context.Logger.LogInformation("Input is: {input}", input);
        return input.ToUpper();
    }
}

.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
    <AWSProjectType>Lambda</AWSProjectType>
    <!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <!-- Generate ready to run images during publishing to improve cold start time. -->
    <PublishReadyToRun>true</PublishReadyToRun>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Amazon.Lambda.Core" Version="2.4.0" />
    <PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" />
  </ItemGroup>
</Project>

Amazon.Lambda.Core (2.4.0) had RequiresPreviewFeatures removed as part of the commit. Amazon.Lambda.TestTool.csproj has package reference to Amazon.Lambda.Core (2.2.0). We might need to release new Lambda Test Tool version which references the latest Amazon.Lambda.Core (2.4.0).

@thomaswr Please confirm if it was working with previous version Amazon.Lambda.Core (2.3.0)

thomaswr commented 1 day ago

It works with Amazon.Lambda.Core 2.3.0, if not using structured logging. Build fails with Amazon.Lambda.Core 2.3.0, if using structured logging with:

error CA2252: Parameterized logging is in preview till a new version of .NET Lambda runtime client that supports parameterized logging has been deployed to the .NET Lambda managed runtime.
Till deployment has been made the feature can be used by deploying as an executable including the latest version of Amazon.Lambda.RuntimeSupport and setting the "EnablePreviewFeatures" in the Lambda project file to "true" Using 'LogInformation' requires opting into preview features.
See https://aka.ms/dotnet-warnings/preview-features for more information. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2252)
normj commented 7 hours ago

Version 0.16.0 of the test tool has been released that fixes the issue. Thanks for reporting the issue.