Node.js 我想在AWS lambda的帮助下更改AWS CloudFront的URL

Node.js 我想在AWS lambda的帮助下更改AWS CloudFront的URL,node.js,amazon-web-services,aws-lambda,amazon-cloudfront,aws-lambda-edge,Node.js,Amazon Web Services,Aws Lambda,Amazon Cloudfront,Aws Lambda Edge,我有一个URL,假设是www.sample.com/hello。现在,我已经根据查看器请求触发了lambda函数,我只需要将url更改为www.sample.com/hello2。我是用lambda边函数做的,但它给我带来了一个错误 这是我用lamda写的代码 const path = require('path'); exports.handler = (event, context, callback) => { const cf = event.Records[

我有一个URL,假设是www.sample.com/hello。现在,我已经根据查看器请求触发了lambda函数,我只需要将url更改为www.sample.com/hello2。我是用lambda边函数做的,但它给我带来了一个错误

这是我用lamda写的代码

const path = require('path');



exports.handler = (event, context, callback) => {
        const cf = event.Records[0].cf;
    const request = cf.request;
    const response = cf.response;
    const statusCode = response.status;
    const path = request.uri;
    const afterpath = path.substring(path.indexOf("/")+1);

if (afterpath == 'sample') {
    request.uri = request.uri
        .replace(afterpath,'samplepathitis')
}

    return callback(null, request);
};
我得到了这个错误

503 ERROR
The request could not be satisfied.
The Lambda function associated with the CloudFront distribution is invalid 
or doesn't have the required permissions. 
If you received this error while trying to use an app or access a website, 
please contact the provider or website owner for assistance. 
If you provide content to customers through CloudFront, you can find steps 
to troubleshoot and help prevent this error by following steps in the 
CloudFront documentation 
(http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/http- 
503-service-unavailable.html). 
Generated by cloudfront (CloudFront)
Request ID: MsN6aG8qvI9ttt3_VLhQAqpY8kF2pHk3V095lAFVU_sWmDvF3IfqAA==

正如错误消息明确指出的:要么函数无效,要么没有调用函数的权限

要检查函数是否有效:请尝试从Lamba控制台调用它。使用测试按钮。您需要将请求作为输入传递。控制台将向您提出示例请求,您可以对其进行调整以模拟您的用例

在文档中,函数的返回值也非常重要。请求是否为Cloudfront期望的正确返回值


一旦确定了以上两个,请验证调用该函数的权限。触发因素是什么?Cloudfront是否有权调用您的函数?

不幸的是,此错误消息不能总是从字面上理解。它在某些情况下抛出,与函数无效完全无关,不管这究竟意味着什么;也许这并不意味着我们所假设的或函数没有正确的权限。我将寻找一个示例;对于没有尝试停止CloudFront处理并发出自发响应的查看器请求触发器来说是正确的。此处,请求是event.Records[0].cf.request的原始版本或正确修改的版本。