简体   繁体   English

如何获取在AWS Lambda中调用的原始URL?

[英]How to get the original URL called in AWS Lambda?

I have a lambda setup with the proxy integration in API Gateway. 我在API Gateway中使用代理集成进行了lambda设置。

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 . 我可以使用网址https://7kbw9fcfa4.execute-api.us-east-1.amazonaws.com/dev/foo调用我的lambda,这里的路径是/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 . 但是,如果在其前面设置CloudFront,则URL变为https://a45ex7tnds5r5o.cloudfront.net/foo ,路径为/foo

In both cases I have the same keys in the event variable: 在这两种情况下,我在event变量中都有相同的键:

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

How can I get reliably the actual URL that the client sees in the browser? 如何可靠地获得客户端在浏览器中看到的实际URL? (both when using Cloudfront and when using API Gateway directly) (在使用Cloudfront和直接使用API​​ Gateway时)

You could configure your CloudFront distribution to use a custom header that would be passed to the Lambda function. 您可以将CloudFront分配配置为使用自定义标头 ,该标头将传递给Lambda函数。 Then you could determine whether to use path or requestContext.path based on if that CloudFront-only header is present. 然后,您可以根据是否存在仅CloudFront标头确定使用path还是requestContext.path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM