Hamed Minaee
Hamed Minaee

Reputation: 2560

how to pass the variable from api gateway to the url of another service running on EC2

I am new to AWS. I am asked to use the services that is written in java and already exist in java EC2 to serve the UI. I need to use API gateway to call the services so the UI will first call the API gate way and then API gate way will call my service. Also I know that in Select Integration Request if I choose http proxy and add the url of my services on Ec2 I can achieve this. However the problem is that I have a service like this:

http://domainname/article/{id}

As you can see the id is a variable and the above service should be called via API Gateway so API gate way should be able to pass the id to the service url. Lets say the following is the url of API gateway:

https://my-api-id.execute-api.region-id.amazonaws.com/stage-name/article?id=1

How can I handle the above scenario? is it possible to do that?

Upvotes: 0

Views: 3651

Answers (2)

MikeD at AWS
MikeD at AWS

Reputation: 3745

While you can use a Lambda function for this, you can also do it directly from API Gateway using basic request mapping.

Define your API in API Gateway with a resource having path /article

Add a GET method.

In the Method Execution pane, choose Method Request. Expand "URL Query String Parameters" Hit "Add query string" Type id as the name and hit the checkbox on the right to save

Go back to the Method Execution pane, choose Integration Request. Edit your endpoint URL to add the path parameter, if you have not already done so. The URL should be: http://domainname/article/{id}

Choose the arrow next to "URL Path Parameters" to expand that section. Click "Add path". Enter id as the name Under "Mapped from" enter: method.request.querystring.id Click the check box on the right to save.

Go back to Method Execution and click Test. Add an id value under query strings and click test.

There are some similar examples in the documentation here

Upvotes: 3

Shimon Tolts
Shimon Tolts

Reputation: 1692

I would suggest using Lambda to re-assemble the request. API Gateway will invoke the Lambda function that would receive the request

http://domainname/article/1

and will perform an HTTP request to your service

https://my-api-id.execute-api.region-id.amazonaws.com/stage-name/article?id=1

Upvotes: 3

Related Questions