Search code examples
amazon-web-servicesyamlaws-cloudformationaws-cloudformation-custom-resource

How to trigger lambda function Event type: ObjectCreatedByPut


I have CF template below

I need to trigger the lambda function for s3 PUT event

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
                    

Solution

  • You can add Events to your template. But for S3 event:

    S3 bucket name. This bucket must exist in the same template.

    Thus your template could be modified as follows:

    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