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
thomas-parikka-milliman opened this issue Oct 23, 2024 · 2 comments
Assignees
Labels
bug This issue is a bug. closing-soon This issue will automatically close in 4 days unless further comments are made. module/lambda-test-tool needs-reproduction This issue needs reproduction. p2 This is a standard priority issue response-requested Waiting on additional info and feedback. Will move to close soon in 7 days.

Comments

@thomas-parikka-milliman

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

@thomas-parikka-milliman thomas-parikka-milliman 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
@ashishdhingra ashishdhingra self-assigned this Nov 13, 2024
@ashishdhingra
Copy link
Contributor

@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:

  • Full stack trace.
  • You mentioned you are using var configurationBuilder = new ConfigurationBuilder().AddEnvironmentVariables(); for Serilog configuration during startup. Could your please elaborate?
  • How does your Mac setup looks like when using Test tool?
    • Are you using JetBrains Rider? If yes, have you followed steps at [Configure for JetBrains Rider](Configure for JetBrains Rider) properly?
    • Same question if using Visual Studio Code.
  • Is the issue still reproducible using latest version of Test tool?

Thanks,
Ashish

@ashishdhingra ashishdhingra added the response-requested Waiting on additional info and feedback. Will move to close soon in 7 days. label Nov 14, 2024
Copy link
Contributor

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 github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Nov 25, 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. closing-soon This issue will automatically close in 4 days unless further comments are made. module/lambda-test-tool needs-reproduction This issue needs reproduction. p2 This is a standard priority issue response-requested Waiting on additional info and feedback. Will move to close soon in 7 days.
Projects
None yet
Development

No branches or pull requests

3 participants