AWS API Gateway Lambda Error Regex

天涯浪子 提交于 2019-12-07 12:20:53

问题


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:

..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:

any advice on fix please?


回答1:


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:

..and it works as expected:



来源:https://stackoverflow.com/questions/50152220/aws-api-gateway-lambda-error-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!