通过API网关从Lambda返回HTTP状态代码

时间:2015-10-28 06:51:23

标签: amazon-web-services aws-lambda aws-api-gateway

我有一个带有Python处理程序的Lambda设置,我创建了一个API端点,只接收POST方法。我无法弄清楚我的Lambda应如何告诉API网关事情已成功完成并返回HTTP 200状态代码。有没有人冒险走这条路?

3 个答案:

答案 0 :(得分:1)

在python lambda中,只需执行

raise Exception('notfound')

而不是使用任何其他关键字。

然后在apigateway中,您需要将'notfound'映射到某些响应状态代码。我使用了swagger,所以这里是示例如何跟踪'notfound'

       "/tags/getById": {
          "get": {
            "summary": "Find the tag by it's ID",
            "produces": [
              "application/json"
            ],
            "responses": {
              "200": {
                "description": "successful operation",
                "schema": {
                  "$ref": "#/definitions/Tag"
                }
              },
              "404": {
                "description": "No tag found"
              }
            },
            "parameters": [xxxxxxx],
            "x-amazon-apigateway-integration": {
              "requestTemplates": {xxxxx},
              "uri": {xxxxxx},
              "responses": {
                "default": {
                  "statusCode": "200"
                },
                "notfound": {
                  "statusCode": "404"
                }

              },
              "httpMethod": "POST",
              "type": "aws"
            }
          }
       }

答案 1 :(得分:0)

您应该能够通过使用处理程序的返回值来实现目标。见官方AWS Lambda Python Model Handler Types

您还需要API Gateway正确配置Integration Response。您可以在官方API Gateway Introductory Blog post找到有关如何完成此操作的更多信息,最重要的是Integration Response部分。

答案 2 :(得分:0)

我想要其他http错误代码,如400&也是500,在这里写了一个答案:https://stackoverflow.com/a/41343990/984471