15

I'm a beginner in Amazon's Lambda-API implementations.

I'm just deploying a very simple API: a very simple lambda function with Python 2.7 printing "Hello World" that I trigger with API Gateway. However, when I click on the Invoke URL link, it tells me "{"message": "Internal server error"}".

Thus, I'm trying to see what is wrong here, so I click on the API itself and I can see the following being grey in my Method Execution: "Integration Response: Proxy integrations cannot be configured to transform responses."

enter image description here

I have tested many different configurations but I still face the same error. I have no idea why this step is grey.

sammtt
  • 401
  • 1
  • 6
  • 14
  • On the Integration Request, you have a checkbox next to "Proxy Integration." Response transformation is not supported with proxy integrations -- your Lambda function needs to render the response the way you want it. – Michael - sqlbot Dec 04 '17 at 17:50
  • In the other word, you don't need to set up the integration response for your Lambda proxy integration, but you can control the response inside your Lambda function with the API Gateway specific response format. – Ka Hou Ieong Dec 04 '17 at 23:23
  • That is correct. – Michael - sqlbot Dec 05 '17 at 00:31

4 Answers4

18

I had the same problem when trying to integrate API gateway and lambda function. Basically, after spending a couple of hours, I figure out. So when you were creating a new resource or method the Use Lambda Proxy integration was set by default.

So you need to remove this. Follow to Integration Request and untick the Use Lambda Proxy integration enter image description here

you will see the following picture enter image description here

Then in you Resources, Atction tab, choose Enable CORS enter image description here enter image description hereenter image description here

Once this done Deploy your API once again and test function. Also, this topic will explain what's happening under the hood.

Good luck...

hasskell
  • 441
  • 3
  • 7
7

The Lambda response should be in a specific format for API gateway to process. You could find details in the post. https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/

exports.handler = (event, context, callback) => {

var responseBody = {
    "key3": "value3",
    "key2": "value2",
    "key1": "value1"
};

var response = {
    "statusCode": 200,
    "headers": {
        "my_header": "my_value"
    },
    "body": JSON.stringify(responseBody),
    "isBase64Encoded": false
};
callback(null, response);
aditya
  • 101
  • 1
  • 7
1

My API was working in Postman but not locally when I was developing the front end. I was getting the same errors when trying to enable CORS on my resources for GET, POST and OPTIONS and after searching all over @aditya answer got me on the right track but I had to tweak my code slightly.

I needed to add the res.statusCodeand the two headers and it started working.

// GET
// get all myModel
app.get('/models/', (req, res) => {
  const query = 'SELECT * FROM MyTable'
  pool.query(query, (err, results, fields) => {
    //...

    const models = [...results]
    const response = {
      data: models,
      message: 'All models successfully retrieved.',
    }
    //****** needed to add the next 3 lines
    res.statusCode = 200;
    res.setHeader('content-type', 'application/json');
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.send(response)
  })
})
Ju66ernaut
  • 2,592
  • 3
  • 23
  • 36
1

If you re using terraform for aws resource provision you can set the "aws_api_gateway_integration" type = "AWS" instead of "AWS_PROXY" and that should resolve your problem.