【问题标题】:AWS API Gateway Lambda error regex not working?AWS API Gateway Lambda 错误正则表达式不起作用?
【发布时间】:2020-10-10 11:16:51
【问题描述】:

我正在尝试处理来自 API 网关 -> Lambda -> API 网关交换的响应,但无法正确处理集成响应。

我有 2 个使用以下 Lambda 错误正则表达式定义的 API Gateway 集成响应:

.*e.* -> method response status 200 (for testing only)
.*500.* -> method response status 500
(no default mapping)

我的 Lambda 返回以下内容:

if (err)
      callback(null, {
        statusCode: 500,
        message: "commit ID failed - ID already present"
      });

    else
      callback(null, {
        statusCode: 200,
        message: "new ID committed"
      });

因此,来自 Lambda 的响应中应该始终包含一些数据。

当我运行 API Gateway 测试时,它未能达到任何一条规则:

Sat Jun 20 07:49:56 UTC 2020 : Received response. Status: 200, Integration latency: 122 ms
Sat Jun 20 07:49:56 UTC 2020 : Endpoint response headers: {Date=Sat, 20 Jun 2020 07:49:56 GMT, Content-Type=application/json, Content-Length=68, Connection=keep-alive, x-amzn-RequestId=d7556a95-2ff9-4cf2-9e61-cc89484f6548, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5eedbfa4-0ddb3f5e0882fbf1cd7e4674;sampled=0}
Sat Jun 20 07:49:56 UTC 2020 : Endpoint response body before transformations: {"statusCode":500,"message":"commit ID failed - ID already present"}
Sat Jun 20 07:49:56 UTC 2020 : Execution failed due to configuration error: No match for output mapping and no default output mapping configured. Endpoint Response Status Code: 200

如果我更改 Lambda 错误正则表达式:

.* matches responses from lambda
.+ doesn't match any responses from lambda

所以我认为这表明传递给 Lambda 错误正则表达式处理的任何字段都是空的 - 我是否正确,如果正确,如何在 Lambda 响应中为其设置值?

提前致谢,

吉姆

【问题讨论】:

    标签: aws-lambda aws-api-gateway


    【解决方案1】:

    解决了这个问题 - 我的错误出在 Lambda 函数中 - 使用 callback("error message") 表示错误,使用 callback(null, "success message") 表示成功。 API 集成响应 Lambda Error Regex 只处理错误(线索就是我猜的名字)。所以这行得通:

    if (err)
       callback("commit ID failed - ID already present");
    else
       callback(null, {
          statusCode: 200,
          message: "new ID committed"
       });
    

    现在可以使用 Lambda 错误正则表达式,例如 .failed.

    【讨论】:

      猜你喜欢
      • 2018-10-13
      • 2020-01-03
      • 2020-03-24
      • 1970-01-01
      • 2016-06-02
      • 2018-03-22
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多