AWS API 网关和 EC2 服务代理

AWS API Gateway and EC2 Service Proxy

我正在尝试将 POST 一个 json 字符串发送到 API 网关,然后让 API 网关将 JSON 发送到 EC2 服务器。

我的问题是我无法从 Amazon 找到有关如何完成此操作的良好文档。

当我测试设置时,我得到了这个

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response><Errors><Error><Code>InvalidHttpRequest</Code><Message>The HTTP request is invalid. Reason: Unable to parse request</Message></Error></Errors><RequestID>1fa47f52-d75c-4ff8-8992-3eac11a79015</RequestID></Response>"

这对我来说意义不大。我认为这是 API 网关尝试将请求发送到 EC2 的问题,但它不能,所以它会生成此错误。所以也许我在 API 网关中错误地设置了 EC2 AWS 服务代理。这可能是因为我不知道我现在应该将 'Action' 设置为什么我让它指向 EC2 实例,只是因为我看不到任何其他地方可以放置该信息。

这真的不应该那么难我已经成功地完成了连接到 Lambda 的这件事并查看了所有文档,我能找到的是:http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-aws-proxy.html#getting-started-aws-proxy-add-resources

这对这种情况没有帮助。有什么想法吗?

我认为您混淆了 AWS 服务代理和 HTTP 服务代理。

API 网关可以将 API 调用转发到不同类型的后端:
- 一个 lambda 函数
- 一项 AWS 服务(参见 http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-s3.html 示例)
- 现有 API、运行 在 AWS 上或本地(您的用例)

定义您 API 时,请务必定义一个 POST 动词并将端点 URL 指向您的 EC2 实例 URL

我刚刚使用 http://gurujsonrpc.appspot.com/ 上的 JSON POST 在线服务进行了测试,它按预期工作。

这是我测试的 Swagger 导出 API。

{
  "swagger": "2.0",
  "info": {
    "version": "2016-04-11T20:46:13Z",
    "title": "test"
  },
  "host": "c22wfjg4d7.execute-api.eu-west-1.amazonaws.com",
  "basePath": "/prod",
  "schemes": [
    "https"
  ],
  "paths": {
    "/": {
      "post": {
        "produces": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "$ref": "#/definitions/Empty"
            }
          }
        },
        "x-amazon-apigateway-integration": {
          "responses": {
            "default": {
              "statusCode": "200"
            }
          },
          "uri": "http://gurujsonrpc.appspot.com/guru",
          "httpMethod": "POST",
          "type": "http"
        }
      }
    }
  },
  "definitions": {
    "Empty": {
      "type": "object"
    }
  }
}