无法将云端作为触发器添加到 lambda 函数

Unable to add cloudfront as trigger to lambda function

您好,我已经按照这个 instruction 尝试使用 Cloudfront 和 lambda@edge 调整图像大小。当我尝试测试调整大小后的图像时,我不断收到以下错误消息:

The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions.

所以我查看了开头提到的那篇文章提供的cloud formation创建的lambda函数,发现里面没有trigger。

我尝试手动设置它,但收到以下错误消息:

CloudFront events cannot be associated with $LATEST or Alias. Choose Actions to publish a new version of your function, and then retry association.

我按照错误信息中的说明进行操作;发布,并将 Cloudfront 添加为触发器,但似乎无法应用它。它仍然是 运行 没有 Cloudfront 作为触发器的那个。

有什么方法可以将 Cloudfront 设置为触发器并使其正常工作吗?

致其他受 aws 博客开发文章质量低劣困扰的人;我发现这是由于错误的 S3 存储桶策略。文章说:

ImageBucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref ImageBucket
      PolicyDocument:
        Statement:
            - Action:
                - s3:GetObject
              Effect: Allow
              Principal: "*"
              Resource: !Sub arn:aws:s3:::${ImageBucket}/*
            - Action:
                - s3:PutObject
              Effect: Allow
              Principal:
                AWS: !GetAtt EdgeLambdaRole.Arn
              Resource: !Sub arn:aws:s3:::${ImageBucket}/*
            - Action:
                - s3:GetObject
              Effect: Allow
              Principal:
                AWS: !GetAtt EdgeLambdaRole.Arn
              Resource: !Sub arn:aws:s3:::${ImageBucket}/*

原来你必须授予权限才能允许除了GetObject和PutObject之外的其他操作,因为它需要在存​​储桶中创建文件夹。 只需将其更改为 s3:*

即可解决问题

对我来说,lambda 屏幕上缺少云前端触发器是因为我不在 us-east-1 区域

如果您通过谷歌搜索 "The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions" 找到了这个答案,这可能是因为您的函数未从 cloudformation 正确连接。例如给定 yaml:

Code: ./src/ # or CodeUri ./src/
Handler: foo.bar

仔细检查 ./src/foo.js 是否有 exports.bar = function...

对于谷歌搜索 "The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions" 的人:

我遇到了那个错误并且很难调试它。事实证明,我的 Lambda 中存在一些我必须解决的编程错误。但是,如果在访问 Cloudfront 时您不断收到 "The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions",您将如何调试它。那,而且 Cloudwatch 日志中没有任何内容。

我的 Lambda 是在 AWS::Lambda::Function's ZipFile attribute. I ended up going to the Lambda service inside AWS and creating a Lambda test payload corresponding to my Cloudfront event as documented here: Lambda@Edge Event Structure 中的 Cloudformation 中定义的。然后,我可以在 Lambda 控制台中调试 Lambda,而无需点击 Cloudfront 或导航到 Cloudwatch 日志。

我 运行 在没有登录 CloudWatch 的情况下进入相同的错误消息。我终于注意到我的 Python 运行时处理程序是 index.handler 而我的 index.py 定义了 lambda_handler。将我的 Python 运行时处理程序更改为 index.lambda_handler 后,错误消失了。 HTH.

我看到你们中的一些人说问题的根本原因不是权限问题,而是您的代码问题。这可能是正确的根本原因。 Cloud Front 倾向于对所有内容使用 403 错误,即使是基本的 404 在大多数情况下也会显示为 403。

我还看到上面的一些评论指出您找不到与 lambda 中的错误相关的任何日志。我认为这很可能是因为你们正在寻找 us-east-1 上的日志并且不住在美国东海岸。日志将位于执行它们的本地区域。所以选择离你坐的地方最近的区域,你很可能会在那里找到日志组。