I have a simple C # Aws Lambda function that successfully passes the test from the Lambda console test, but does not work with 502 (Bad Gateway) if called from the API gateway (which I created from the Lambda trigger option), and also if I use postman . (this initial function has open access (without security))
// request header Content-Type: application/json // request body { "userid":22, "files":["File1","File2","File3","File4"] }
The error I get in the logs is:
Wed Feb 08 14:14:54 UTC 2017 : Endpoint response body before transformations: { "errorType": "NullReferenceException", "errorMessage": "Object reference not set to an instance of an object.", "stackTrace": [ "at blahblahmynamespace.Function.FunctionHandler(ZipRequest input, ILambdaContext context)", "at lambda_method(Closure , Stream , Stream , ContextInfo )" ] }
It seems that the published object is not being passed to the lambda input argument.
Code below
// Lambda function public LambdaResponse FunctionHandler(ZipRequest input, ILambdaContext context) { try { var logger = context.Logger; var headers = new Dictionary<string, string>(); if (input == null || input.files.Count == 0) { logger.LogLine($"input was null"); headers.Add("testheader", "ohdear"); return new LambdaResponse { body = "fail", headers = headers, statusCode = HttpStatusCode.BadRequest }; } else { logger.LogLine($"recieved request from user{input?.userid}"); logger.LogLine($"recieved {input?.files?.Count} items to zip"); headers.Add("testheader", "yeah"); return new LambdaResponse { body = "hurrah", headers = headers, statusCode = HttpStatusCode.OK }; } } catch (Exception ex) { throw ex; } }
// Lambda response / ZipRequest class
public class LambdaResponse { public HttpStatusCode statusCode { get; set; } public Dictionary<string, string> headers { get; set; } public string body { get; set; } } public class ZipRequest { public int userid { get; set; } public IList<string> files { get; set; } }