1

My setup:

I have a Cloudfront Origin Group where Bucket A is a primary bucket and Bucket B is a secondary bucket. Lambda@Edge is added on origin-request to do a certain process.

Whenever a request comes to Cloudfront, my Lambda@Edge modifies it to match the folder structure of my bucket and returns file accordingly.

If Bucket A doesn't have a certain file it throws an error and Cloudfront failover requests the file from Bucket B. Bucket B doesn't have the same structure as Bucket it, it should return the file from unmodified file path in the bucket.

Example:

My Original Request: /somefile.html Lambda@Edge modifies this request to get the file from Bucket A to: /en/somefile.html

If Bucket A doesn't have this somefile.html then this request goes to Bucket B. It should return file from the originally requested path: /somefile.html and not /en/somefile.html

The above scenario is very simple, my original scenario is much complex. Basically Bucket A file path is processed path while Bucket B should return file from an originally requested path.

What I want:

Using Lambda@Edge how can I detect if the request is on Bucket A or bucket B?

What I have tried:

  • I tried adding certain header in request headers and check if the header exists then its request to Bucket B. But this doesn't seem to be working.
4

1 回答 1

1

在源请求触发器返回控制权后 CloudFront 将尝试联系的源的主机名可以在两个位置之一找到。

如果您使用 CloudFront 所称的S3 源(S3 的 REST 接口),它将位于此处:

event.Records[0].cf.request.origin.s3.domainName 

如果您使用 CloudFront 所称的自定义源——其中包括 S3 网站托管端点以及任何其他非 S3 REST 的源服务器,它就在这里:

event.Records[0].cf.request.origin.custom.domainName

这些可用于确定源组中的两个源中的哪一个将接收请求,接下来。

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html#lambda-event-structure-request

于 2019-12-05T04:06:36.330 回答