Lambda function with SQS trigger / Lambda Event JSON payload

0

I am receiving the error below when I am testing with lambda function via the AWS Console > Lambda > Specific Lambda Function > Test tab

Or within my IDE, VS Comm 2022 when I am invoking/testing the lambda function there as well.

What is perplexing is when I use 'Mock Lambda Test Tool' and pass in that same JSON there is no error and I get the expected output....

The error is listed below. I have also listed my code and the JSON that I am sending to it.

Amazon.Lambda.Serialization.SystemTextJson.JsonSerializerException: Error converting the Lambda event JSON payload to type System.String: The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1.

JSON:

{ "FirstName": "JTran"
}

Function Code

using Amazon.Lambda.Core;
using Amazon.Lambda.SQSEvents;

// 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 Test_function
{
    public class Function
    {

        public string FunctionHandler(ApiHeader input, ILambdaContext context)
        {           

            return $"Additional text added as a test {input.FirstName}";

        }
    }
}

Class to be associated with it when the JSON object is deserialized:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test_function
{
    public class ApiHeader
    {
        public string ApiID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Body { get; set; }

    }
}
1 Answer
0

Hello,

I deployed your Lambda and it worked with the testevent you provided using the Lambda-Test-tab in the console:

{ "FirstName": "JTran" }

Result: "Additional text added as a test JTran"

Are you sure that you not accidently saved a faulty testevent and lambda is using that always instead of your input?

Handlername differs in my case (because I used hello-world-sam application as a template): HelloWorld::HelloWorld.Function::FunctionHandler

Using: .NET 6 (C#/PowerShell) & x86_64

using Amazon.Lambda.Core;
//using Amazon.Lambda.SQSEvents;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// 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 HelloWorld
{
    public class Function
    {

        public string FunctionHandler(ApiHeader input, ILambdaContext context)
        {           

            return $"Additional text added as a test {input.FirstName}";

        }
    }
}

namespace HelloWorld
{
    public class ApiHeader
    {
        public string ApiID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Body { get; set; }

    }
}
profile picture
HeikoMR
answered 2 months ago
  • Hello! Thank you for responding, I don't believe I saved a faulty testevent. I do have to inquire, the lambda is part of a CDK CloudFormation stack. With the Lambda function being in a separate file, if I were to update the function code itself, can I just save it or do I need to run cdk deploy again?

  • Hey, you can just change/update the lambda directly. And as long as you don't change the lambdaname, cloudformation itself shouldn't detect a drift so its ok imo.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions