Xiaoguo Ge
Xiaoguo Ge

Reputation: 2328

JSON serialization error invoking C# lambda from API Gateway

I have an C# AWS lambda with this signature string FunctionHandler(string input, ILambdaContext context). This lambda tested fine from the lambda web console. But when I tried invoking the same labmda from an API gateway, I receive an 500 error and also this error message in the log

Error converting the Lambda event JSON payload to a string. JSON strings must be quoted, for example "Hello World" in order to be converted to a string: Unexpected character encountered while parsing value: {. Path '', line 1, position 1.: JsonSerializerException

I've tried changing the lambda signature to this string FunctionHandler(object input, ILambdaContext context) but it did not solve the problem.

I've tried invoking a JavaScript lambda with the same API Gateway, and it is working fine.

My question is what is so special about the C# lambda that make it fail to execute behind an API Gateway?

The JavaScript lambda looks like below

exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

the C# lambda looks like below

public string FunctionHandler(string input, ILambdaContext context)
{
    return "hello";
}

Upvotes: 0

Views: 2213

Answers (1)

Xiaoguo Ge
Xiaoguo Ge

Reputation: 2328

Still new to AWS lambda but this is what I found out, it turns out that aws lambda should have different signatures for different purposes. An lambda which is intended to be integrated with and API Gateway should have a signature like below

public APIGatewayProxyResponse Handler(APIGatewayProxyRequest apigProxyEvent)

This is documented here. Pretty difficult to figure out with the C# AWS lambda and API Gateway getting started tutorials

Upvotes: 1

Related Questions