AWS API Gateway Lambda错误正则表达式 [英] AWS API Gateway Lambda Error Regex
问题描述
我有用Python 2.7编写的Lambda函数,该函数是从API网关触发的,
I have Lambda function written in Python 2.7 which is triggered from API Gateway,
我希望当Lambda失败并且我不想使用Lambda代理时,API网关返回400个代码,所以我试图设置API网关集成响应.
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.
Lambda函数失败时,它将返回:
When Lambda function fails it returns:
{
"stackTrace": [
[
"/var/task/lambda_function.py",
12,
"lambda_handler",
"raise Exception('failure')"
]
],
"errorType": "Exception",
"errorMessage": "failure"
}
Lambda错误正则表达式和主体映射模板:
Lambda Error Regex and Body Mapping template:
..与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个代码:
..but it doesn't work correctly and still returns 200 code:
请提供任何修复建议?
推荐答案
答案很简单-从Lambda错误正则表达式中删除双引号就可以了,
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:
这篇关于AWS API Gateway Lambda错误正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!