简体   繁体   English

AWS CDK - 添加 s3 触发器以调用 lambda

[英]AWS CDK - add an s3 trigger to invoke a lambda

I'm trying to build a lambda function with s3 trigger throw the CDK deployment, does somebody knows if it possible to programmatically trigger the CDK code?我正在尝试使用 s3 触发器构建 lambda function 抛出 CDK 部署,有人知道是否可以以编程方式触发 CDK 代码?

I found those links:我找到了这些链接:

  1. Lookup S3 Bucket and add a trigger to invoke a lambda 查找 S3 存储桶并添加触发器以调用 lambda
  2. With CDK, can it be triggered through a lambda to deploy the stack 使用CDK,是否可以通过lambda触发部署堆栈

but they were a few months ago and I wanted to know if anything was renewed但它们是几个月前的,我想知道是否有任何更新

if you want to trigger a lambda after a file was uploaded to S3 you have two ways:如果您想在文件上传到 S3 后触发 lambda,您有两种方法:

S3 Eventnotifications: S3 事件通知:

this is a S3 specific feature and supports lambda as a target and also SQS and SNS.这是 S3 特定的功能,支持 lambda 作为目标以及 SQS 和 SNS。 You can find more info here: https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html您可以在此处找到更多信息: https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html

CloudTrail:云轨迹:

CloudTrail logs pretty much all Events in your account and you can react to them if you want. CloudTrail 记录了您帐户中的几乎所有事件,您可以根据需要对它们做出反应。

  1. create a bucket创建一个桶
  2. Create a trail, you might want to select write only, to reduce the amount of stuff that gets written创建一条线索,您可能希望 select 只写,以减少写入的内容量
  3. add the bucket to the trail with addS3EventSelector使用 addS3EventSelector 将存储桶添加到跟踪
  4. add your target添加你的目标
        uploadBucket.onCloudTrailWriteObject('cwEvent', {
            target: new targets.LambdaFunction()
        })

this will create a CloudWatch Event.这将创建一个 CloudWatch 事件。

On the first step you might need to also log it to cloud watch logs, I'm not sure anymore:在第一步,您可能还需要将其记录到云监视日志中,我不确定了:

        const trail = new cloudtrail.Trail(this, 'CloudTrail', {
            sendToCloudWatchLogs: true,
            managementEvents: cloudtrail.ReadWriteType.WRITE_ONLY,
        });

I prefer version two, because CloudWatch Event supports way more targets than SQS, SNS and Lambda.我更喜欢版本二,因为 CloudWatch Event 支持的目标比 SQS、SNS 和 Lambda 多得多。 I used it to trigger a Step Function for example.例如,我用它来触发 Step Function。

Docs: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-cloudtrail-readme.html https://docs.aws.amazon.com/cdk/api/latest/docs/aws-s3-readme.html#bucket-notifications Docs: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-cloudtrail-readme.html https://docs.aws.amazon.com/cdk/api/latest/docs/aws -s3-readme.html#bucket-notifications

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

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