Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task
tparikkaatmilliman opened this issue Oct 23, 2024 · 0 comments
Labels
bug This issue is a bug. module/lambda-test-tool needs-reproduction This issue needs reproduction. p2 This is a standard priority issue

Comments

@tparikkaatmilliman
Copy link

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

  • Select this option if this issue appears to be a regression.

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

@tparikkaatmilliman tparikkaatmilliman added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 23, 2024
@bhoradc bhoradc added needs-reproduction This issue needs reproduction. module/lambda-test-tool p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. module/lambda-test-tool needs-reproduction This issue needs reproduction. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

2 participants