AWS API Gateway Lambda错误正则表达式

时间:2018-05-03 10:00:40

标签: python aws-lambda aws-api-gateway

我有用Python 2.7编写的Lambda函数,它是从API网关触发的,

我希望API网关在Lambda失败时返回400代码并且我不想使用Lambda代理,因此我尝试设置API网关集成响应。

当Lambda函数失败时,它返回:

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

Lambda Error Regex和Body Mapping模板: enter image description here

..来自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"
    }
}

..但它没有正常工作,仍然返回200代码: enter image description here

有任何关于修复的建议吗?

1 个答案:

答案 0 :(得分:0)

答案结果非常简单 - 从Lambda Error Regex删除双引号就行了,

此外,还可以删除Body Mapping模板: enter image description here

..它按预期工作: enter image description here