1
votes

I have a lambda setup with the proxy integration in API Gateway.

I can call my lambda with a URL like https://7kbw9fcfa4.execute-api.us-east-1.amazonaws.com/dev/foo The path here is /dev/foo.

However if I set up CloudFront in front of that, the URL becomes https://a45ex7tnds5r5o.cloudfront.net/foo and the path is /foo.

In both cases I have the same keys in the event variable:

  • path contains /foo
  • requestContext.path contains /dev/foo

How can I get reliably the actual URL that the client sees in the browser? (both when using Cloudfront and when using API Gateway directly)

1
Have you tried looking at origin header? Also Did you enable headers from CloudFront to API Gateway. If you don't whitelist the headers, you will not get them from CloudFront. - Kannaiyan
@Kannaiyan I whitelisted all headers except the Host header because it is recommended not to forward it (else it messes up the Cloudfront -> API Gateway connection). The Origin header doesn't seem to be useful as it contains only the domain name, not the path (see developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin). - Matthieu Napoli

1 Answers

1
votes

You could configure your CloudFront distribution to use a custom header that would be passed to the Lambda function. Then you could determine whether to use path or requestContext.path based on if that CloudFront-only header is present.