API Gateway new (beta) http api
Asked Answered
F

1

0

I have created a sample HTTP API (which is currently in a beta release) using the API gateway. This API does not use any authentication and has a lambda as an integration. The route accepts any HTTP method and I have confirmed that the lambda has the proper API gateway permission. This permission was added when I created the API.

However, when I call the API I receive an HTTP status of 500 and a body of: {"message":"Internal Server Error"}.

This same lambda and API will work if I set it up as a REST API rather than an HTTP API.

Any ideas why this isn't working in the HTTP API?

Formyl answered 3/1, 2020 at 19:34 Comment(0)
R
0

I had a similar issue with Internal Server Error. I was using the new API Gateway HTTP API (beta) with a nodejs lambda integration. The lambda on its own worked and the REST API worked fine, like you noted.

The 2 issues that cost me 8 hours:

  1. I had to add an async prefix to the handler or use a callback, but I couldn't just return a response without async. See https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

  2. The response structure has to be correct, see this page: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-create-lambda-backend, scroll to the example function code and you'll see this:

    // The output from a Lambda proxy integration must be 
    // in the following JSON object. The 'headers' property 
    // is for custom response headers in addition to standard 
    // ones. The 'body' property  must be a JSON string. For 
    // base64-encoded payload, you must also set the 'isBase64Encoded'
    // property to 'true'.

So my function ended up looking like this, note the async and the proper response structure:

exports.handler = async function(event, context) {
  const response = {
    statusCode: 200,
    body: JSON.stringify('Hello from Lambda!')
  };
  return response;
}
Rahm answered 27/1, 2020 at 0:6 Comment(1)
Ahmad Khatib - thank you! This worked well in the new http api.Formyl

© 2022 - 2025 — McMap. All rights reserved.