aws / aws-lambda-dotnet

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

System.ArrayTypeMismatchException When Using ConfigurationBuilder In Lambda With Lambda Test Tool #1846

Open thomas-parikka-milliman opened 1 month ago

thomas-parikka-milliman commented 1 month ago

Describe the bug

I am attempting to write a Lambda that requires Serilog to be configured during startup. In order to do this I am using the following code in my main Function class relying on environment variables:

var configurationBuilder = new ConfigurationBuilder()
    .AddEnvironmentVariables();

var configuration = configurationBuilder.Build();

When I step over the first line, the ConfigurationBuilder returned has thrown a System.ArgumentException. When I step over the second line the call to Build() returns a System.ArrayTypeMismatchException. For testing, I removed even the call to AddEnvironmentVariables() with no improvement. I reproduced the example in a brand new Lambda project using the SQS template.

I have tested a similar config setup in a basic .NET BackgroundService with no issues.

Regression Issue

Expected Behavior

I expect the ConfigurationBuilder to be able to load environment variables into memory for use later in the application.

Current Behavior

The newly created ConfigurationBuilder throws System.ArgumentException on all properties. Building the ConfigurationBuilder produces a System.ArrayTypeMismatchException.

Reproduction Steps

Function.cs:

using Amazon.Lambda.Core;
using Amazon.Lambda.SQSEvents;
using Microsoft.Extensions.Configuration;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace LambdaTest;

public class Function
{
    /// <summary>
    /// Default constructor. This constructor is used by Lambda to construct the instance. When invoked in a Lambda environment
    /// the AWS credentials will come from the IAM role associated with the function and the AWS region will be set to the
    /// region the Lambda function is executed in.
    /// </summary>
    public Function()
    {
    }

    /// <summary>
    /// This method is called for every Lambda invocation. This method takes in an SQS event object and can be used 
    /// to respond to SQS messages.
    /// </summary>
    /// <param name="evnt">The event for the Lambda function handler to process.</param>
    /// <param name="context">The ILambdaContext that provides methods for logging and describing the Lambda environment.</param>
    /// <returns></returns>
    public async Task FunctionHandler(SQSEvent evnt, ILambdaContext context)
    {
        var configurationBuilder = new ConfigurationBuilder()
            .AddEnvironmentVariables();
        var configuration = configurationBuilder
            .Build();

        foreach (var message in evnt.Records)
        {
            await ProcessMessageAsync(message, context);
        }
    }

    private async Task ProcessMessageAsync(SQSEvent.SQSMessage message, ILambdaContext context)
    {
        context.Logger.LogInformation($"Processed message {message.Body}");

        // TODO: Do interesting work based on the new message
        await Task.CompletedTask;
    }
}

launchSettings.json

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "LambdaTest:MacOS": {
      "commandName": "Executable",
      "commandLineArgs": "/Users/thomas.parikka/.dotnet/tools/.store/amazon.lambda.testtool-8.0/0.15.2/amazon.lambda.testtool-8.0/0.15.2/tools/net8.0/any/Amazon.Lambda.TestTool.BlazorTester.dll",
      "workingDirectory": "$(ProjectDir)",
      "executablePath": "dotnet",
      "environmentVariables": {
        "ENVIRONMENT": "local"
      }
    }
  }
}

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.3.0" />
        <PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.1"/>
        <PackageReference Include="Amazon.Lambda.SQSEvents" Version="2.2.0"/>
        <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
        <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
    </ItemGroup>
</Project>

aws-lambda-tools-defaults

{
  "Information": [
    "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
    "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
    "dotnet lambda help",
    "All the command line options for the Lambda command can be specified in this file."
  ],
  "profile": "",
  "region": "",
  "configuration": "Release",
  "function-runtime": "dotnet8",
  "function-memory-size": 512,
  "function-timeout": 30,
  "function-handler": "LambdaTest::LambdaTest.Function::FunctionHandler"
}

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

Amazon.Lambda.Serialization.SystemTextJson 2.4.3 Amazon.Lambda.Core 2.3.0 Amazon.Lambda.SQSEvents 2.2.0 AWSSDK.Extensions.NETCore.Setup 3.7.301

Targeted .NET Platform

.NET 8

Operating System and version

MacOS Sequoia 15.0.1

ashishdhingra commented 1 week ago

@thomas-parikka-milliman Good afternoon. Apologies for the delayed reply. I wasn't able to reproduce the issue on Windows (even though your environment say Mac) using the similar code as above, but with below package references and AWS .NET 8.0 Mock Lambda Test Tool (0.15.3):

<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.2.0" />
    <PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.1" />
    <PackageReference Include="Amazon.Lambda.SQSEvents" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.0" />
  </ItemGroup>
</Project>

Below is launchSettings.json in my Windows environment:

{
  "profiles": {
    "Mock Lambda Test Tool": {
      "commandName": "Executable",
      "commandLineArgs": "--port 5050",
      "workingDirectory": ".\\bin\\$(Configuration)\\net8.0",
      "executablePath": "%USERPROFILE%\\.dotnet\\tools\\dotnet-lambda-test-tool-8.0.exe",
      "environmentVariables": {
        "ENVIRONMENT": "local"
      }
    }
  }
}

The error is thrown by var configurationBuilder = new ConfigurationBuilder().AddEnvironmentVariables();, it appears unrelated to test tool.

Please share the following:

Thanks, Ashish

github-actions[bot] commented 1 day ago

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.