首页 文章

AWS API网关和EC2服务代理

提问于
浏览
8

我正在尝试将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服务代理 . 这可能是因为我不知道我应该将'Action'设置为现在我指向EC2实例,只是因为我没有看到任何其他地方放置该信息 .

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

这对于这种情况不太有帮助 . 有任何想法吗?

1 回答

  • 13

    我认为您混淆了AWS Service Proxy和HTTP Service代理 .

    API网关可以将API调用转发到不同类型的后端:

    定义API时,请务必定义POST谓词并将 endpoints 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"
        }
      }
    }
    

相关问题