fastify / aws-lambda-fastify

Insipired by aws-serverless-express to work with Fastify with inject functionality.
MIT License
498 stars 32 forks source link

Remote IP address passed to app.inject() #186

Closed steveatkoan closed 9 months ago

steveatkoan commented 9 months ago

Prerequisites

🚀 Feature Proposal

Hi How possible is it to get the remote IP from the aws lambda environment (somewhere?) and include that in the app.inject() call?

Motivation

I'm using fastify/rate-limit because I want custom control over the rate limits applied. But because I don't know the remote IP address it thinks the whole world is 127.0.0.1 when a request comes from api gateway / lambda

Example

No response

steveatkoan commented 9 months ago

you should be able to look at the event.identity.sourceIp or event.http.sourceIp Depending on if we are using the REST api or HTTP api gateway respectively. https://github.com/awsdocs/aws-lambda-developer-guide/blob/main/sample-apps/nodejs-apig/event.json https://github.com/awsdocs/aws-lambda-developer-guide/blob/main/sample-apps/nodejs-apig/event-v2.json

add near line 42 const remoteAddress = event.requestContext?.http?.sourceIp || event.requestContext?.identity?.sourceIp || undefined

line 102 app.inject({ method, url, query, payload, headers, remoteAddress }

adrai commented 9 months ago

the remote address should be in the x-forwarded-for header

adrai commented 9 months ago

v3.5.0 should also use the sourceIp as remoteAddress

pavarnos commented 9 months ago

So fast! Thanks heaps. Very helpful!

steveatkoan commented 9 months ago

Thank you