Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda and Gateway API Integration, returns status code 500

First of all, I am new to whole AWS itself. I am trying to solve a problem using AWS lambda and API Gateway.

  1. I created a python lambda function. It takes some data and returns a string based on some conditions. Something like below:
def lambda_function(event, context):
    if event['some_property']:
        return "SUCCESS: Operation performed successfully."
    else
        return "ERROR: Operation failed."
  1. Under the triggers , I set up a gateway API.

  2. In API gateway service for the created resource, the integration request is configured to pass through to the lambda function. I have tested lambda function and it returns values properly.

  3. But if i curl the API making a post request, it returns a server error. The message is this.

{"message": "internal server error"}

  1. I tried to play around. If i return an empty json from function and create a body mapping template with Content Type as application/json and use empty model template for it, then it works fine.

I guess , I am missing something in Integration Response part. What i want is to return a 200 status code along with the success message, if the operation is performed successfully.

like image 481
Gaurav Kumar Avatar asked Dec 12 '16 15:12

Gaurav Kumar


2 Answers

You need to pass the statusCode after executing the Lambda function. If you don't pass it the API Gateway will trigger 502 Bad Gateway by default.

message = {
   'message': 'Execution started successfully!'
}
return {
    'statusCode': 200,
    'headers': {'Content-Type': 'application/json'},
    'body': json.dumps(message)
}
like image 136
ljmocic Avatar answered Oct 27 '22 13:10

ljmocic


I'd first suggest using the 'proxy' Lambda integration. It is much easier to configure and use. Here are docs for that http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html and http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html

But it sounds like you don't have the responses mapped correctly. Hard to say from your description what the issue is, but you can use the example "Petstore API" to see what the responses should look like. That may be helpful. You can see that option in the 'Create API' screen.

like image 37
jackko Avatar answered Oct 27 '22 13:10

jackko