5

I have Lambda function written in Python 2.7 which is triggered from API Gateway,

I want API Gateway to return 400 code when Lambda fails and I don't want to use Lambda Proxy so I'm trying to set API Gateway Integration Response.

When Lambda function fails it returns:

{
    "stackTrace": [
        [
            "/var/task/lambda_function.py",
            12,
            "lambda_handler",
            "raise Exception('failure')"
        ]
    ],
    "errorType": "Exception",
    "errorMessage": "failure"
}

Lambda Error Regex and Body Mapping template: enter image description here

..the same from CLI:

aws apigateway get-integration-response --rest-api-id bz47krygwa --resource-id 788q0w --http-method ANY --status-code 400 --region us-west-1
{
    "statusCode": "400",
    "selectionPattern": ".*\"failure\".*",
    "responseTemplates": {
        "application/json": "#set($inputRoot = $input.path('$.errorMessage'))\n$inputRoot"
    }
}

..but it doesn't work correctly and still returns 200 code: enter image description here

any advice on fix please?

1 Answer 1

6

Answer turned out to be very simple - removing double quotes from Lambda Error Regex did the trick,

moreover Body Mapping template could also be removed: enter image description here

..and it works as expected: enter image description here

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

1 Comment

Is there a way to mach the statusCode field returned from the lambda and not having to have an errorMessage field?

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.