Internal Server Error/502 Bad Gateway testing a Lambda Function via the Function URL

0

Testing a Lambda Function written in C# .Net using Visual Studio, testing from VS works fine. Set up a Function URL for the Lambda Function and tried calling from PostMan, get a response 'Internal Server Error' with a status of 502 Bad Gateway. The Clouwatch log tells me 'The JSON value could not be converted to System.String'. The function is very basic generated by VS.

public string FunctionHandler(string input, ILambdaContext context)
{
    Console.WriteLine("Hello there");
    Console.WriteLine("Input: " + input);
    return input.ToUpper();
}
posta 2 anni fa3794 visualizzazioni
1 Risposta
0

Invoking a function using the AWS Toolkit in your IDE bypasses the Function URL and thus invokes the Lambda service directly. So, no integrations are involved. For Function URL, there is an integration involved, so you need to develop your function taking that into account.

Please refer to the Developer Guide.

When a client calls your function URL, Lambda maps the request to an event object before passing it to your function.

The request and response event formats follow the same schema as the Amazon API Gateway payload format version 2.0.

That means, you have to change your function signature so that it can accept a request object in the API Gateway payload v2.0 format. The corresponding type is Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyRequest from the assembly Amazon.Lambda.APIGatewayEvents.

Refactor your function in this way:

using Amazon.Lambda.APIGatewayEvents;

//...
public string FunctionHandler(APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context)
{
    Console.WriteLine("Hello there");
    Console.WriteLine("Input: " + request.Body);
    return request.Body.ToUpper();
}

Now you can invoke your Lambda using the Function URL via Postman or any other client. In this case, since the request body is expected to be plain text, don't forget to specify this in the request's header as Content-Type: text/plain. Also note that you will need to change the request payload template in AWS Toolkit in your IDE to API Gateway AWS Proxy when invoking the function - this is because you've changed the handler signature.

profile pictureAWS
con risposta 2 anni fa
  • Many thanks Dmitry, I will try this later today and let you know how I got on.

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande