在API Gateway中映射Lambda输出会导致服务器错误 [英] Mapping Lambda output in API Gateway gives server error

查看:172
本文介绍了在API Gateway中映射Lambda输出会导致服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an AWS API Gateway setup, served by a Python Lambda function. For successful responses the Lambda returns a response of the form:

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

默认情况下,网关控制台中的 Integration Response 设置只有一个规则,该规则配置了 Lambda错误正则表达式().* 映射到响应状态为200.效果很好.

By default, the Integration Response settings in the gateway console have just one rule configured with a Lambda Error Regex of .* mapping to a response status of 200. This works fine.

问题在于,当我尝试将其更改为 200.* 时(以期实现更具体的代码).现在我得到一个

The problem is when I try to change that to 200.* (with a view to enabling more specific codes going forward). Now I get a

{"message": "Internal server error"}

每次我访问任何请求(结果为200或否)时,都要进入网关.

every time I hit the gateway with any request (resulting in a 200 or not).

没有错误日志写入CloudWatch.

No error logs are written to CloudWatch.

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

I want to know how I can map Lambda outputs to HTTP status codes successfully in AWS API Gateway.

推荐答案

如果您在API网关中进行测试运行,则测试运行日志应显示类似于以下内容的错误:

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

2015年11月21日星期六UTC:由于配置,执行失败 错误:没有匹配的输出映射,也没有默认的输出映射 已配置

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

问题在于不再有默认映射来捕获成功的响应.错误正则表达式的目的是捕获错误并将其映射到状态代码.错误正则表达式搜索失败的json响应的errorMessage属性.如果将状态码400设置为默认值(空白),则会发现成功的响应始终映射到状态码400.

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.

最好保留200作为默认(空白)正则表达式.然后设置特定的正则表达式值以检查您的不同错误消息.例如,您可以具有多个特定的错误正则表达式状态代码和一个通用的catch-all以产生500.

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.

这篇关于在API Gateway中映射Lambda输出会导致服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆