首页 文章

AWS API Gateway Lambda错误正则表达式

提问于
浏览
0

我有用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

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

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

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

相关问题