AWS API网关和EC2服务代理

时间:2016-04-11 17:11:29

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

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

我的问题是我无法从亚马逊那里找到关于如何实现这一目标的好文档。

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

"<?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服务代理。这可能是因为我不知道我应该设置什么&#39;行动&#39;现在我指向EC2实例,只是因为我没有看到任何其他地方放置该信息。

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

这对这种情况不太有帮助。任何想法?

1 个答案:

答案 0 :(得分:13)

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

API Gateway可以将API调用转发给不同类型的后端:
- lambda函数
- AWS服务(例如,请参阅http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-s3.html
- 在AWS或内部运行的现有API(您的用例)

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

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

以下是我的测试API的Swagger导出。

{
  "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"
    }
  }
}
相关问题