Can't set X-Forwarded-For to sourceIp in AWS API Gateway

I've put Amazon's API gateway in front of an existing HTTP API. That API has its own logging that needs access to the source IP address, however I can't seem to map the source IP to an X-Forwarded-For header in the Integration Request tab of the dashboard.

The API gateway docs mention $context.identity.sourceIp, but that only seems to be available in body templates. In my case I am trying to just directly map queries to the backend HTTP API so I don't have a body template.

The HTTP Headers settings in the Integration Request page won't accept the $context variable, sadly, giving the error:

Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: $context.identity.sourceIp]

and claims to only be able to access HTTP headers from the request, query string parameters from the request, or path parameters from the request.

Is this actually impossible in the API gateway? Seems like this would be a very standard issue when wrapping an existing HTTP API.


Remove $ sign in your request paramters mapping:

requestParameters:
    integration.request.header.X-Forwarded-For: "context.identity.sourceIp"
    integration.request.header.User-Agent: "context.identity.userAgent"