2

I have an AWS Lambda integrated into API Gateway. The lambda is current just returning an error:

exports.handler = (event, context, callback) => {
  context.fail(JSON.stringify({
    status: 500,
    errors: 'This is definitely the same endpoint.'
  }));
};

I have added an HTTP status 500 response in method response of API Gateway, and I have mapped an integration response using regex *"status":500.* to the 500 response.

When I test this using the Method Test functionality in AWS, I get the 500 response I expect:

AWS Method Test Response

But when send command to the endpoint with Postman, I get a 200 status:

Postman response

How can this be? The Method Test seems to suggest my Integration Response and Method Response setups are correct, and I have my regex set up correctly, but what am I missing between the API Gateway and the rest of the world that would produce this different result?

2 Answers 2

3

Have you tried Deploying the API? Maybe a redeployment might fix the issues.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that was it.
This helped me as well after creating an API through CDK
0

did you stumble across the Handle Custom Lambda Errors in API Gateway guide already?

The format suggested there is:

{
   "isBase64Encoded" : "boolean",
   "statusCode": "number",
   "headers": { ... },
   "body": "JSON string"
}

So you basically need to return this JSON structure. I check this with AWS serverless express library (line 100-105) - which we use - and it's returning this structure.

The major challenge is, that sending the HTTP response to the client is a task for the API Gateway and to do so, you must return a valid JSON response from the lambda in a format that the API gateway unterstands. The execution status of the lambda will be also 200 (because it worked A-Okay) but the API Gateway will transform your response into a 500. This is also why you nedd this double/triple JSON encoding (JSON-as-string in JSON-attributes).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.