11

我有一个 AWS API Gateway 设置,由 Python Lambda 函数提供服务。对于成功的响应,Lambda 会返回以下形式的响应:

"200:{\"somekey\": \"somevalue\"}"

默认情况下,网关控制台中的集成响应设置只有一个规则配置有 .* 的Lambda错误正则表达式映射到响应状态 200。这工作正常。

问题是当我尝试将其更改为200.*时(以启用更具体的代码)。现在我得到一个

{"message": "Internal server error"}

每次我用任何请求访问网关(导致 200 或没有)。

没有错误日志写入 CloudWatch。

在此处输入图像描述

我想知道如何在 AWS API Gateway 中成功地将 Lambda 输出映射到 HTTP 状态代码。

4

2 回答 2

17

If you do a test run in API gateway, the test run log should show an error similar to:

Sat Nov 21 07:41:25 UTC 2015 : Execution failed due to configuration error: No match for output mapping and no default output mapping configured

The problem is there is no-longer a default mapping to catch a successful response. The intention of the error regex is to catch errors and map them to status codes. The error regex searches the errorMessage property of the failed json response. If you were to set the status code 400 to be the default (blank) - you would find your successful response always mapping to a status code of 400.

You'll be best off to leave 200 as a default (blank) regex. Then set specific regex values to check for your different error messages. For example, you can have multiple specific error regex status codes and a generic catch-all to result in 500's.

enter image description here

于 2015-11-21T08:16:14.670 回答
2

对于那些在这个问题上尝试了一切但无法完成这项工作的人(比如我),请查看这篇文章的 thedevkit 评论(拯救了我的一天):

https://forums.aws.amazon.com/thread.jspa?threadID=192918

在下面完全复制它:

我自己也遇到过这个问题,我相信换行符是罪魁祸首。

foo.* 将匹配出现的“foo”,后跟除换行符以外的任何字符。通常这是通过添加“/s”标志来解决的,即“foo.*/s”,但 Lambda 错误正则表达式似乎不尊重这一点。

作为替代方案,您可以使用类似: foo(.|\n)*

于 2016-02-17T17:38:26.340 回答