Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS API Gateway and EC2 Service Proxy

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?

like image 851
wmfrancia Avatar asked Apr 11 '16 17:04

wmfrancia


People also ask

How do I Connect API gateway to AWS service proxy?

For the HTTP method, choose GET, and then save your choice. In this step, you create an IAM role that your AWS service proxy uses to interact with the AWS service. We call this IAM role an AWS service proxy execution role. Without this role, API Gateway cannot interact with the AWS service.

How to create an API gateway for AWS EC2?

Go to API Gateway console and Click Build in HTTP API 3. Type a name and click Review and Create 4. Click Create 5. Check if your API is available by clicking the invoke URL 6. Click “ Route ” from the left menu Here is the important part, you have to put / {proxy+}, otherwise URL API will not cover all the routes that your EC2 server is serving.

How to implement a reverse proxy pattern using Managed Services from AWS?

With AWS Amplify Console, Amazon API Gateway, and Amazon CloudFront, we have seen three approaches to implement a reverse proxy pattern using managed services from AWS. The easiest approach to start with is AWS Amplify Console. If you run into more complex scenarios consider API Gateway.

How can I expose AWS Lambda through API gateway API?

In addition, you can create an API Gateway API to expose other AWS services, such as Amazon SNS, Amazon S3, Amazon Kinesis, and even AWS Lambda. This is made possible by the AWS integration. The Lambda integration or the Lambda proxy integration is a special case, where the Lambda function invocation is exposed through the API Gateway API.


1 Answers

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"
    }
  }
}
like image 181
Sébastien Stormacq Avatar answered Sep 28 '22 08:09

Sébastien Stormacq