简体   繁体   English

如何在 S3 文件上传时触发 Lambda

[英]How to trigger a Lambda on S3 file upload

I can't seem to get my lambda to trigger as I would expect.我似乎无法让我的 lambda 像我预期的那样触发。 In AWS EventBridge, I created a rule with the following custom event pattern:在 AWS EventBridge 中,我使用以下自定义事件模式创建了一个规则:

{
  "source": ["aws.s3"],
  "detail-type": ["AWS API Call via CloudTrail"],
  "detail": {
    "eventSource": ["s3.amazonaws.com"],
    "eventName": [
      "CopyObject",
      "CompleteMultipartUpload",
      "PutObject"
    ],
    "requestParameters": {
      "bucketName": ["my-bucket"],
      "key": [{"prefix": "folder1/folder2/"}]
    }
  }
}

but when I upload a file to the "directory" the rule does not trigger the lambda.但是当我将文件上传到“目录”时,规则不会触发 lambda。

Can someone tell me where I've gone wrong?有人可以告诉我哪里出错了吗?

You can use event notification in S3.您可以在 S3 中使用事件通知。 It has direct integration with Lambda, SNS, SQS它与 Lambda、SNS、SQS 直接集成

  • Goto properties tab in S3转到 S3 中的属性选项卡

  • Navigate to Event notification.导航到事件通知。 click on create event notification.单击创建事件通知。 在此处输入图像描述

  • Then add the event name.然后添加事件名称。

  • If you want to restrict the event to a specific folder or file type, you can fill in prefix or suffix fields or if you want it for entire bucket leave those blank.如果您想将事件限制为特定的文件夹或文件类型,您可以填写前缀或后缀字段,或者如果您希望将其用于整个存储桶,请将其留空。 在此处输入图像描述

  • Then select a list of events.然后是 select 事件列表。 like put, post.像把,张贴。 在此处输入图像描述

  • Then choose the destination and click on save.然后选择目的地并点击保存。 You will be notified.您将收到通知。 在此处输入图像描述

You can use both S3 events or cloud watch event rule to invoke the lambda function for any event on S3.您可以同时使用 S3 事件或云观察事件规则来为 S3 上的任何事件调用 lambda function。

  • Below Image shows how to configure cloud watch event rule for s3 putObject operation.下图显示了如何为 s3 putObject 操作配置云观察事件规则。
  • Make sure you enabled cloud trail for data events in the respective region.确保为相应区域中的数据事件启用云跟踪。
  • Make sure you create a rule for the specific bucket.确保为特定存储桶创建规则。
  • if you mention is for all the buckets.如果你提到的是所有的桶。 You will get unnecessary invocations, as cloud watch event rules works on cloud trail, where it will store all the logs to s3.您将获得不必要的调用,因为云监视事件规则适用于云跟踪,它将所有日志存储到 s3。 在此处输入图像描述

As mentioned in my comment to your question before, you might not have the proper permissions to allow EventBridge to Invoke your Lambda.正如我之前对您的问题的评论中提到的,您可能没有适当的权限来允许EventBridge Invoke您的 Lambda。

You can add the following Resource-based policy to your Lambda :您可以将以下Resource-based policy添加到Lambda

{
  "Version": "2012-10-17",
  "Id": "default",
  "Statement": [
    {
      "Sid": "AllowExecutionFromEventBridge",
      "Effect": "Allow",
      "Principal": {
        "Service": "events.amazonaws.com"
      },
      "Action": "lambda:InvokeFunction",
      "Resource": "<lambda-arn>",
      "Condition": {
        "ArnLike": {
          "AWS:SourceArn": "<eventbridge-rule-arn>"
        }
      }
    }
  ]
}

You need to replace <lambda-arn> and <eventbridge-rule-arn> with the respective values.您需要将<lambda-arn><eventbridge-rule-arn>替换为各自的值。

You can read more about resource-based policy here: Using resource-based policies for AWS Lambda .您可以在此处阅读有关resource-based policy更多信息: 为 AWS Lambda 使用基于资源的策略

If you use tools like Terraform, you can use the following snippet:如果您使用 Terraform 之类的工具,则可以使用以下代码段:

resource "aws_lambda_permission" "example" {
  statement_id  = "AllowExecutionFromCloudWatch"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.example.function_name
  principal     = "events.amazonaws.com"
  source_arn    = aws_cloudwatch_event_rule.example.arn
}

You need to replace function_name and source_arn here as well, with your specific references.您还需要将function_namesource_arn替换为您的具体引用。

Thanks to all for the help and suggestions - all good resources should someone come across this post in the future.感谢所有人的帮助和建议——如果有人在未来看到这篇文章,所有好的资源。 Turns out in my case @Balu Vyamajala was correct in the comments - I had incorrectly configured CloudTrail.事实证明,@Balu Vyamajala 在评论中是正确的——我错误地配置了 CloudTrail。

As of 2021-11-29, Amazon Event Bridge now supports Amazon S3 Event Notifications without having to enable CloudTrial .自 2021 年 11 月 29 日起,Amazon Event Bridge 现在支持 Amazon S3 事件通知,而无需启用 CloudTrial

In the properties of the S3 bucket you want to monitor, you have to enable Amazon EventBridge.在您要监控的 S3 存储桶的属性中,您必须启用 Amazon EventBridge。

在此处输入图像描述

This will allow you to send messages to EventBridge using Rules.这将允许您使用规则向 EventBridge 发送消息。 在此处输入图像描述

In your eventbridge rule, you can configure lambda as a "Target".在您的 eventbridge 规则中,您可以将 lambda 配置为“目标”。 I made a step-by-step tutorial on how to configure eventbridge in AWS if you want to follow along: https://youtu.be/k-jEuNb_KBM如果您想继续学习,我制作了有关如何在 AWS 中配置 eventbridge 的分步教程: https://youtu.be/k-jEuNb_KBM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM