为什么Lambda@Edge没有转发到正确的桶?



我有3个S3桶:

  • my-routing-test-ap-southeast-2
  • my-routing-test-eu-west-2
  • my-routing-test-us-east-1

它们都被配置为静态网站,关闭block all public access,并且(例如)此策略:

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Demo",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject","s3:GetObjectVersion"],
"Resource": "arn:aws:s3:::my-routing-test-us-east-1/*"
}
]
}

我已经配置了一个cloudfront发行与一个源:

  • 我的-路由-测试-我们-东- 1. - s3.us -东- 1. amazonaws.com

为上述原点配置behaviour,Legacy cache settings标头选项设置为CloudFront-Viewer-Country值。

我应该在这里指出,基于请求头的缓存文档状态:

指定是否希望CloudFront根据指定头的值缓存对象:

Whitelist - CloudFront只根据指定头的值缓存对象。使用白名单头来选择你希望CloudFront基于缓存的头。

然而,CloudFront控制台的Edit behaviour部分显示了"缓存键和原始请求";选项:

Legacy cache settings > Headers > Include the following headers > CloudFront-Viewer-Country

当然,它似乎不包括"白名单"。选择。

该发行版还将Origin request设置为Lambda@Edge函数(其中的代码来自本文档页面):

'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const countryToRegion = {
'US': 'us-east-1',
'AU': 'ap-southeast-2',
'GB': 'eu-west-2'
};
if (request.headers['cloudfront-viewer-country']) {
const countryCode = request.headers['cloudfront-viewer-country'][0].value;
const region = countryToRegion[countryCode];

console.log('countryCode: '+countryCode+' region: '+region);

if (region) {
console.log('region: '+region);
request.origin.s3.region = region;
const domainName = `my-routing-test-${region}.s3.${region}.amazonaws.com`;
request.origin.s3.domainName = domainName;
console.log('request.origin.s3.domainName: '+domainName);
request.headers['host'] = [{ key: 'host', value: domainName }];
}
}
callback(null, request);
};

当我调用cloudfront URL来检索我的区域(eu-west-2)的测试文件时,我在我的区域的日志组中看到以下内容:

countryCode: GB region: eu-west-2
region: eu-west-2
request.origin.s3.domainName: origin-routing-eu-west-2.s3.eu-west-2.amazonaws.com

但是该文件始终是来自us-east-1区域的相同图像。这是不应该的,因为每个桶包含不同的图像为每个区域。

这个配置中缺少或不正确的是什么?

结果是"缓存键和初始值过期";对象缓存设置为默认值(1年),当我测试初始文件检索时,基本上存储在缓存中的主要原始文件不能被区域版本取代。检查你的缓存设置,伙计们!

最新更新