本文介绍了使用AWS CloudFormation设置S3存储桶级事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让AWS CloudFormation创建一个模板,该模板将允许我将事件附加到现有的S3存储桶,每当将新文件放入存储桶中的特定目录时,该事件就会触发Lambda函数。我使用以下YAML作为CloudFormation模板的基础,但无法使其正常工作。

I am trying to get AWS CloudFormation to create a template that will allow me to attach an event to an existing S3 Bucket that will trigger a Lambda Function whenever a new file is put into a specific directory within the bucket. I am using the following YAML as a base for the CloudFormation template but cannot get it working.

---
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  SETRULE:
    Type: AWS::S3::Bucket
      Properties:
        BucketName: bucket-name
        NotificationConfiguration:
          LambdaConfigurations:
            - Event: s3:ObjectCreated:Put
              Filter:
                S3Key:
                  Rules:
                    - Name: prefix
                      Value: directory/in/bucket
              Function: arn:aws:lambda:us-east-1:XXXXXXXXXX:function:lambda-function-trigger
              Input: '{ CONFIGS_INPUT }'

我尝试用多种方法重写此模板,但均未成功。

I have tried rewriting this template a number of different ways to no success.

推荐答案

您可以编写一个自定义资源来执行此操作,实际上这就是我最终在工作中要做的 同样的问题。在最简单的级别上,定义一个需要放置存储桶通知配置的lambda,然后仅使用传递的数据调用放置存储桶通知api。

You could write a custom resource to do this, in fact that's what I've ended up doing at work for the same problem. At the simplest level, define a lambda that takes a put bucket notification configuration and then just calls the put bucket notification api with the data that was passed it.

如果需要为了能够跨不同的cloudformation模板控制不同的通知,那么它要复杂一些。您的自定义资源lambda将需要从S3中读取现有通知,然后根据从CF传递给它的数据来更新这些通知。

If you want to be able to control different notifications across different cloudformation templates, then it's a bit more complex. Your custom resource lambda will need to read the existing notifications from S3 and then update these based on what data was passed to it from CF.

这篇关于使用AWS CloudFormation设置S3存储桶级事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 08:29