I am trying my serverless compute in aws. I have created and deployed a simple application that return a string as follows
public string FunctionHandler()
{
return "input will be in UPPER case :-> ";
}
This got deployed from visual studio-aws plugin and I am able to invoke it correctly from visual studio test screen. Further, I configured an API gateway that is 'open' end-point. When I call that URL from browser it give internal server error and status 502. Details get response from Aws test windows is below
Execution log for request test-request
Sun Nov 12 11:44:00 UTC 2017 : Starting execution for request: test-invoke-request
Sun Nov 12 11:44:00 UTC 2017 : HTTP Method: GET, Resource Path: /testAnasfn
Sun Nov 12 11:44:00 UTC 2017 : Method request path: {}
Sun Nov 12 11:44:00 UTC 2017 : Method request query string: {}
Sun Nov 12 11:44:00 UTC 2017 : Method request headers: {}
Sun Nov 12 11:44:00 UTC 2017 : Method request body before transformations:
Sun Nov 12 11:44:00 UTC 2017 : Endpoint request URI: https://lambda.ap-south-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-south-1:126893027727:function:testAnasfn/invocations
Sun Nov 12 11:44:00 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=*************************************************************************************************************************************************************************************************************************************************************************************************************************7d591a, X-Amz-Date=20171112T114400Z, x-amzn-apigateway-api-id=vjvpcua830, X-Amz-Source-Arn=arn:aws:execute-api:ap-south-1:126893027727:vjvpcua830/null/GET/testAnasfn, Accept=application/json, User-Agent=AmazonAPIGateway_vjvpcua830, X-Amz-Security-Token=FQoDYXdzEBQaDMyv494SIfdIl+qxLSK5AygkNULmfxDTCe8srTRObtfH8Gycwy22pkJzTD/fp+peUByQbXRD1MTeFFQHn6t+hulEBbAdESX0goX9/fytIFksJnMlT+/E+OlfIx17YwqqRIw+0dieUgDCl80J12UYR2CNPQFdc3yU5F60QXf4EjpgXOVTClSSSbqeXKZQ8M9BgzD3YsRbXnpVsRlpELJaV7i9R1rSeuI6ix66NPgpd+cp4578Fhpuol3xlueqk0JnlJZ9NtwH0N8vLbH3R2ZP0yPY8a9Pp19qHkVz9C2DzNXenChwxEb9seVZR7D92FIg8rBcQQkzDlbDa2D2lVMeSImhdlsvF/ttlpgWnpfeB [TRUNCATED]
Sun Nov 12 11:44:00 UTC 2017 : Endpoint request body after transformations: {"resource":"/testAnasfn","path":"/testAnasfn","httpMethod":"GET","headers":null,"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"path":"/testAnasfn","accountId":"126893027727","resourceId":"v0n0zx","stage":"test-invoke-stage","requestId":"test-invoke-request","identity":{"cognitoIdentityPoolId":null,"accountId":"126893027727","cognitoIdentityId":null,"caller":"126893027727","apiKey":"test-invoke-api-key","sourceIp":"test-invoke-source-ip","accessKey":"ASIAJNBVXURNV3ICQ2YA","cognitoAuthenticationType":null,"cognitoAuthenticationProvider":null,"userArn":"arn:aws:iam::126893027727:root","userAgent":"Apache-HttpClient/4.5.x (Java/1.8.0_144)","user":"126893027727"},"resourcePath":"/testAnasfn","httpMethod":"GET","apiId":"vjvpcua830"},"body":null,"isBase64Encoded":false}
Sun Nov 12 11:44:00 UTC 2017 : Sending request to https://lambda.ap-south-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-south-1:126893027727:function:testAnasfn/invocations
Sun Nov 12 11:44:00 UTC 2017 : Received response. Integration latency: 19 ms
Sun Nov 12 11:44:00 UTC 2017 : Endpoint response body before transformations: "input will be in UPPER case :-> "
Sun Nov 12 11:44:00 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, Connection=keep-alive, x-amzn-RequestId=c65a341b-c79e-11e7-8ef4-17eaf422a581, Content-Length=34, Date=Sun, 12 Nov 2017 11:44:00 GMT, X-Amzn-Trace-Id=root=1-5a083400-b65593a405f54027ad55da5c;sampled=0, Content-Type=application/json}
Sun Nov 12 11:44:00 UTC 2017 : Execution failed due to configuration error: Malformed Lambda proxy response
Sun Nov 12 11:44:00 UTC 2017 : Method completed with status: 502
I tried to call it from jQuery Ajax but failed status received.
I checked Use Lambda Proxy integration for using it from Proxy!
UPDATE 1
When I unchecked Use Lambda Proxy integration on Integration Request in API gateway and test application I get corrected response with status 200.
Execution log for request test-request
Sun Nov 12 19:44:19 UTC 2017 : Starting execution for request: test-invoke-request
Sun Nov 12 19:44:19 UTC 2017 : HTTP Method: GET, Resource Path: /testAnas1
Sun Nov 12 19:44:19 UTC 2017 : Method request path: {}
Sun Nov 12 19:44:19 UTC 2017 : Method request query string: {}
Sun Nov 12 19:44:19 UTC 2017 : Method request headers: {}
Sun Nov 12 19:44:19 UTC 2017 : Method request body before transformations:
Sun Nov 12 19:44:19 UTC 2017 : Endpoint request URI: https://lambda.ap-south-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-south-1:126893027727:function:testAnas1/invocations
Sun Nov 12 19:44:19 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=***************************************************************************************************************************************************************************************************************************************************************************************bd4940, X-Amz-Date=20171112T194419Z, x-amzn-apigateway-api-id=ntn0u9y8gk, X-Amz-Source-Arn=arn:aws:execute-api:ap-south-1:126893027727:ntn0u9y8gk/null/GET/testAnas1, Accept=application/json, User-Agent=AmazonAPIGateway_ntn0u9y8gk, X-Amz-Security-Token=FQoDYXdzEBwaDKFa4kx/RGE/fe8dqyK5A7wMpA/qRGHmUARstMAvEJvFe3rvi32sR0faLB+IUTb8wOMeehIgNJ4O/QD+5CuM26+q6xnCN5Ov5qJvENp63KiqmNuhpub5hoDutE1zAahQsLoKzx5Muqy+/tzJ9yQCXaA1eHklH0XNNG9BZjgz1JANoPNfMGSvY8KBEqARH6fCEdOuSz0/KJeALd9xYbHATO0ml5ucXJLqVEZFPuwbwrfbFHINGXkjgpJCLwc5x4YoY+9bWzkuWc+akkMIeyRDmKvii88Qa3MCnbJz9wUqzG/bjh8QpVdg4M/dFhG3lor0s1tdT6r4w47MeUaJgqxAQXO+7eGTA2PfPWdsoB1JGl2qriDXwamgSQJEkpHCgGaZzk88lyEgabUI [TRUNCATED]
Sun Nov 12 19:44:19 UTC 2017 : Endpoint request body after transformations:
Sun Nov 12 19:44:19 UTC 2017 : Sending request to https://lambda.ap-south-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-south-1:126893027727:function:testAnas1/invocations
Sun Nov 12 19:44:20 UTC 2017 : Received response. Integration latency: 80 ms
Sun Nov 12 19:44:20 UTC 2017 : Endpoint response body before transformations: "I am Ahmad Anas"
Sun Nov 12 19:44:20 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, Connection=keep-alive, x-amzn-RequestId=e0201f5e-c7e1-11e7-8754-61ed0803d92c, Content-Length=17, Date=Sun, 12 Nov 2017 19:44:20 GMT, X-Amzn-Trace-Id=root=1-5a08a493-f27ad9f6b7e304c7ef148949;sampled=0, Content-Type=application/json}
Sun Nov 12 19:44:20 UTC 2017 : Method response body after transformations: "I am Ahmad Anas"
Sun Nov 12 19:44:20 UTC 2017 : Method response headers: {X-Amzn-Trace-Id=sampled=0;root=1-5a08a493-f27ad9f6b7e304c7ef148949, Content-Type=application/json}
Sun Nov 12 19:44:20 UTC 2017 : Successfully completed execution
Sun Nov 12 19:44:20 UTC 2017 : Method completed with status: 200
But my problem persist and when I call Invoke URL status code 502 is returned. Note that I have created ProductionTest1 stage.
ANSWER
I am certainly not allowed for now to answer my own question but I think it's OK to answer [Myself] as update. Short Answer : Frame your own response in C# then deploy on lambda as below in code.
public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
{
//context.Logger.LogLine("This is useful for debugging!");
return new APIGatewayProxyResponse()
{
StatusCode = (int)HttpStatusCode.OK, Body = "Go Serverless v1.0! Your function executed successfully!",
};
}
Now come to a bit detail. To work with AWS Lambda you can use HTTP Proxy Integration with a Proxy Resource (In case)
The HTTP proxy integration, designated by HTTP_PROXY in the API Gateway REST API, is for integrating a method request with a backend HTTP endpoint. With this integration type, API Gateway simply passes the entire request and response between the frontend and the backend, subject to certain restrictions and limitations. here
So, as a possible solution you can create a simple object of your own or use one provided by AWS - APIGatewayProxyResponse. More Use NuGet for this Install-Package Amazon.Lambda.APIGatewayEvents -Version 1.1.0
I hope this will resolve the issue and brief on response object trick!