【问题标题】:How to trigger lambda function Event type: ObjectCreatedByPut如何触发 lambda 函数事件类型:ObjectCreatedByPut
【发布时间】:2021-05-24 23:13:34
【问题描述】:

我下面有 CF 模板

我需要为 s3 PUT 事件触发 lambda 函数

Event type: ObjectCreatedByPut

https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-s3-notification-config/

AWSTemplateFormatVersion: "2010-09-09"
Transform:  'AWS::Serverless-2016-10-31'

rData:
        Type: AWS::Serverless::Function
        Properties:
            CodeUri: functions/load_data
            FunctionName: sample-function
            Handler: lambda_function.lambda_handler
            Runtime: python3.8
            MemorySize: 3008
            Timeout: 100
            Role: !Sub arn:aws:iam::${AWS::AccountId}:role/main_service_role
            Environment:
                Variables:
                    bucket_name: sample-bucket
                    file_name: config/test.csv
                    

【问题讨论】:

    标签: amazon-web-services yaml amazon-cloudformation aws-cloudformation-custom-resource


    【解决方案1】:
    AWSTemplateFormatVersion: "2010-09-09"
    Transform:  'AWS::Serverless-2016-10-31'
    
    rData:
            Type: AWS::Serverless::Function
            Properties:
                CodeUri: functions/load_data
                FunctionName: sample-function
                Handler: lambda_function.lambda_handler
                Runtime: python3.8
                MemorySize: 3008
                Timeout: 100
                Role: !Sub arn:aws:iam::${AWS::AccountId}:role/main_service_role
                Environment:
                    Variables:
                        bucket_name: sample-bucket
                        file_name: config/test.csvEvents:
                S3Event:
                    Type: S3
                    Properties:
                    Bucket:
                        Ref: ImagesBucket     # This must be the name of an S3 bucket declared in the same template file
                    Events: s3:ObjectCreated:Put
                    Filter:
                        S3Key:
                        Rules:
                        - Name: prefix      # or "suffix"
                            Value: value      # The value to search for in the S3 object key names
    

    S3 Event

    Amazon S3 Event Notifications

    【讨论】:

    • ImagesBucket 需要添加桶名吗?
    • 我认为@marcin 已经涵盖了所有内容 :)
    【解决方案2】:

    您可以将Events 添加到您的模板中。但是对于S3 event

    S3 存储桶名称。此存储桶必须存在于同一模板中。

    因此您的模板可以修改如下:

    AWSTemplateFormatVersion: "2010-09-09"
    Transform:  'AWS::Serverless-2016-10-31'
    Resources:
    
      MyBucket:
        Type: AWS::S3::Bucket
        Properties:
          BucketName: test-bucket-3422344
    
      rData:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: functions/load_data     
          FunctionName: sample-function
          Handler: lambda_function.lambda_handler
          Runtime: python3.8
          MemorySize: 3008
          Timeout: 100
          Role: !Sub arn:aws:iam::${AWS::AccountId}:role/main_service_role
          Environment:
            Variables:
              bucket_name: sample-bucket
              file_name: config/test.csv
          Events:
            S3Event:
              Type: S3
              Properties:
                Bucket: !Ref MyBucket
                Events: s3:ObjectCreated:Put          
                  
    

    【讨论】:

    • MyBucket 资源中我们还需要添加存储桶名称吗?供参考
    • @sim 您可以使用特定的存储桶名称,也可以像我一样保留它。如果未明确指定,桶名将自动生成。
    • 最后一个问题,如果我想传递存储桶名称“test-bucket”,如何在“MyBucket”资源中传递它
    • @sim 我更新了答案。存储桶名称必须是全局唯一的,因此 test-bucket 可能已被其他人占用。
    • 在bucket中有一个特定的文件夹,“test/sample/”是我添加的目录名称过滤器:S3Key:规则:-名称:前缀值:test/sample/
    猜你喜欢
    • 2018-05-18
    • 2018-05-05
    • 2019-10-18
    • 2020-04-20
    • 2021-12-07
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多