微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何触发 lambda 函数事件类型:ObjectCreatedByPut

如何解决如何触发 lambda 函数事件类型:ObjectCreatedByPut

我下面有 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
                    

解决方法

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

,

您可以将 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          
              

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。