0

我有一个 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==
4

1 回答 1

1

正如错误消息明确指出的那样:该函数无效或无权调用该函数。

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

在文档中也非常有函数的返回值。Cloudfrontrequest是否期望正确的返回值?

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

于 2019-08-03T11:37:02.287 回答