AWS API Gateway and EC2 Service Proxy

11,161

I think you confused AWS Service Proxy and HTTP Service proxy.

API Gateway can forward API calls to different type of backends:
- a lambda function
- an AWS Service (see http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-s3.html for an example)
- an existing API, running on AWS or on premises (your use case)

When defining you API, be sure to define a POST verb and point the Endpoint URL to your EC2 instance URL

I just made a test using the JSON POST service available online at http://gurujsonrpc.appspot.com/ and it works as expected.

Here is the Swagger export of my test 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"
    }
  }
}
Share:
11,161
wmfrancia
Author by

wmfrancia

Full Stack Developer Knowledgeable in PHP/JS/CSS/HTML/SQL/NginX/Ubuntu https://github.com/wmfrancia/Encryptor

Updated on July 02, 2022

Comments

  • wmfrancia
    wmfrancia almost 2 years

    I am trying to POST a json string to API Gateway and in turn have API Gateway send the JSON to an EC2 server.

    My issue is I can't find good documentation from Amazon on how to accomplish this.

    When I test the setup I get this

    "<?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>"
    

    Which means very little to me. I assume it is an issue with API Gateway trying to send the request to EC2 and it can't so it generates this error. So perhaps I am setting up the EC2 AWS Service Proxy in API Gateway incorrectly. Which is likely because I have no idea what I am supposed to set 'Action' to right now I have it pointing to the EC2 instance, only cause i don't see any other place to put that info.

    This really shouldn't be that hard I have successfully done this thing connecting to Lambda and have looked through all the documentation and all I can find is this: http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-aws-proxy.html#getting-started-aws-proxy-add-resources

    Which is less than helpful for this scenario. Any Ideas?

  • wmfrancia
    wmfrancia about 8 years
    Yep that is exactly what I was confusing. Thanks.
  • The Onin
    The Onin over 6 years
    @wmfrancia So, are you guys saying that, if I want to use API Gateway to invoke a PHP file on my EC2, I actually have to choose "Integration Type: -> "HTTP", and not "AWS Service" ?
  • RichVel
    RichVel almost 6 years
    @NinoŠkopac - that's correct, the "AWS Service" means built-in services such as AWS S3.
  • Shakedk
    Shakedk over 5 years
    @RichVel, that means that if I restart my ec2 instance I need to manually insert a new IP to the API integration request. Is there any way around that?
  • RichVel
    RichVel over 5 years
    @Shakedk - best to ask a new question for that, most likely the answers will include creating a domain name that maps to an elastic IP allocated to the EC2 instance