1

我一直在尝试使用 lambda@edge 将 CloudFront-viewer-country 标头添加到我的响应中,我设法触发了该功能及其工作,但我在 cloudWatch 中看不到任何日志,cloudWatch 仅记录测试是在功能页面上完成的,不是通过访问网站触发的真正测试,我用谷歌搜索了如何找到日志,发现它在接收请求的最近区域的 cloudWatch 中,但我找不到。

我尝试了两种方法来更改我的响应,其中一种是堆栈溢出答案:https://stackoverflow.com/questions/48633610/how-do-i-get-cloudfront-viewer-country-to-appear-in-response-headers

'use strict';

exports.handler = (event, context, callback) => {
   const request = event.Records[0].cf.request;
   const response = event.Records[0].cf.response;
   if(request.headers['cloudfront-viewer-country'])
   {
      response.headers['cloudfront-viewer-country'] = request.headers['cloudfront-viewer-country'];
   }
   return callback(null,response);
};

此方法在从 lambda 页面进行测试时有效,但是当我通过访问网站进行测试时出现 503 服务器错误:与 CloudFront 分配关联的 Lambda 函数无效或没有所需的权限。

所以有人可以帮助获得这些权限,请注意我的 s3 存储桶上有以下策略,它静态托管我的网站:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadForGetBucketObjects",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::outfityard-app-client/*"
        }
    ]
}

我尝试过的另一种方法是在我的 lambda 中使用以下函数:

'use strict';

exports.handler = (event, context, callback) => {

    const request = event.Records[0].cf.request;
    const headers = request.headers;
    const uri = request.uri === "/index.html" ? "/" : request.uri;
    var countryCode = null;
    if(headers["cloudfront-viewer-country"]) {
        countryCode = headers["cloudfront-viewer-country"][0].value;
    }
    console.log(`Country detected: '${countryCode}'`);

    if (countryCode !== "US") {
        const response = {
            status: "200",
            statusDescription: "Found",
            headers: {
                ["cache-control"]: [
                    {
                        key: "Cache-Control",
                        value: "no-cache, no-store, private"
                    }
                ],
                ["cloudfront-viewer-country"]: [
                    {
                        key: "cloudfront-viewer-country",
                        value: countryCode
                    }
                ]
            }
        };
        callback(null, response);
    } else {
        callback(null, request);
    } 
};

此方法正在返回预期的响应,但我的网站没有加载任何内容,我不明白为什么!这是我收到的响应标头:

Content-Type →text/html
Content-Length →1217
Connection →keep-alive
Server →CloudFront
Date →Fri, 16 Aug 2019 11:37:19 GMT
X-Cache →LambdaExecutionError from cloudfront
Via →1.1 9891f2220bf61a27cb1f26085ab3703d.cloudfront.net (CloudFront)
X-Amz-Cf-Pop →CDG3-C2
X-Amz-Cf-Id →D542SzvAsMYAXIOcBUwHWEB5_lAf41t1wWULh6LcXwagAvjFRvH5nA==

我已经使用了以下内容并且工作正常,但我不知道它是否正确的方法:

'use strict';

exports.handler = (event, context, callback) => {
   const request = event.Records[0].cf.request;
   const response = event.Records[0].cf.response;
   const value = request.headers['cloudfront-viewer-country'][0].value;
   const key = request.headers['cloudfront-viewer-country'][0].key;
   response.headers['cloudfront-viewer-country'] = [{key: key, value: value}];
   return callback(null,response);
};
4

0 回答 0