2

I am trying to send a API Gateway Response code of 401 if the user is missing required fields in header. The response from my lambda is as follows:

{
    "status": "Unauthorized user",
    "body": ["username"]
}

In the AWS API Gateway -> myAPI -> resource -> PUT operation -> Integration Response, I have 2 entries.
1. For all success with default RegEx Pattern (blank) --> Method response status of 201
2. For regex matching, if I use above JSON and below regex using regex or online regex101.com, there is a full match.

.*\"Unauthorized user\".*

enter image description here enter image description here However, the same fails during Test of API with below message

<date_time in UTC> : Endpoint response body before transformations: {"status": "Unauthorized user", "body": ["username"] }
<date_time in UTC> : Execution failed due to configuration error: No match for output mapping and no default output mapping configured. Endpoint Response Status Code: 200
<date_time in UTC> : Method completed with status: 500

Has anybody faced this? I end up getting a 201 if I have 201 with blank regex... irrespective of Unauthorized or internal error or bad requests.

1
  • Yes; After re-reading the docs I even tried setting the regex to .* which it warns will essentially make it the default response. And I still get 200s instead of error codes. Commented Jun 26, 2020 at 12:29

1 Answer 1

3
+50

This is the image from your question:

enter image description here

Notice how the header of the first column says Lambda Error Regex? That means that API Gateway will only try to match that regex with an error returned by the Lambda. But how does the API Gateway know whether the Lambda returned a genuine response or an error? It knows this based on whether the Lambda returned a response or threw an exception.

Your regex isn't matching because you returned the response instead of throwing it as an exception!

Check this out:

When my Node.js Lambda does this:

exports.handler = async (event) => {
    const response = {
        "status": "Unauthorized user",
        "body": ["username"]
    }
    return response
}

My API gateway does this:

enter image description here

But when the same Lambda does this:

exports.handler = async (event) => {
    const error = {
        "status": "Unauthorized user",
        "body": ["username"]
    }
    throw JSON.stringify(error)
}

The API Gateway does this:

enter image description here

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

1 Comment

This definitely was part of the answer for me. Ultimately there appeared to be something else as .* didn't work as expected, but when I used a regex that would find the whole line of an error message it started working as expected.

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.