Matan Kadosh
Matan Kadosh

Reputation: 1781

AWS Api Gateway - Http integration client IP

I am using AWS API Gateway as a http proxy to my rest api application. The thing is, I can't seem to find a way to get the client ip address or to pass it through to my application from the api gateway. The only way I saw was to use a lambda function, but I can't use it because it have to be a http integration.

Is there any other way to do that?

Upvotes: 1

Views: 4417

Answers (4)

Big Pumpkin
Big Pumpkin

Reputation: 4487

If you are working with API Gateway's HTTP APIs (announced in Dec 2019), be aware of the payload format version under Advanced Settings in Integrations. You can pick between 1.0 and 2.0.

Using Kotlin code as examples, for 1.0, you can retrieve the source IP from APIGatewayProxyRequestEvent as follows.

event.requestContext.identity.sourceIp

For 2.0, you can retrieve the source IP from APIGatewayV2HTTPEvent as follows.

event.requestContext.http.sourceIp

Upvotes: 1

alboko
alboko

Reputation: 498

If you are using "proxy"-style integration, the client IP is available inside the request (no template mapping is required). Follow the code example from the link above and dig inside "event" JSON object:

requestContext -> identity -> sourceIp

Upvotes: 0

MikeD at AWS
MikeD at AWS

Reputation: 3745

You can do this in the mapping template of your integration response. Call $context.identity.sourceIp to get the sourceIp.

If you're already using a mapping template, then you can just add this at any convenient point. If you aren't currently using a mapping template and just need to add the sourceIp to an incoming json request body, add a mapping template like so:

#set($allParams = $input.path('$'))
#set($discard=$allParams.put('sourceIp', $context.identity.sourceIp))
$input.json('$'))

Upvotes: 1

Sergey Kovalev
Sergey Kovalev

Reputation: 9431

Just configure request payload-mapping. What you need is $context.identity.sourceIp. According to documentation:

$context.identity.sourceIp

The source IP address of the TCP connection making the request to API Gateway.

Documentation with examples is available at http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

Upvotes: 1

Related Questions