Search code examples
amazon-s3aws-lambdaaws-cloudformation

Adding S3 trigger to Lambda function using CloudFormation


I'm trying to add an S3 trigger to a lambda function using CloudFormation. From what I've read about circular references the lambda function and S3 bucket needs to be created first, which I've done with a template and they get created successfully.

Then I go into "Update Stack" and enter the template:

 "Resources": {
        "MyBucket": {
            "Type": "AWS::S3::Bucket",
            "NotificationConfiguration": {
                "LambdaConfigurations": [
                    {
                        "Event": "s3:ObjectCreated:*",
                        "Function": "arn:aws:lambda:ap-southeast-2:newlyCreatedLambda"
                    }
                ]
            },
            "Properties": {
                "BucketName": "MyBucket"
....
....

But when I try to deploy it gives the error:

Template is not valid: Invalid template resource property 'NotificationConfiguration'

Any idea how to get the trigger added or what I'm doing wrong?


Solution

  • Here's what we use:

    "BucketForFunctionsAcesImportNewFileUploaded": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "NotificationConfiguration": {
          "TopicConfigurations": [],
          "QueueConfigurations": [],
          "LambdaConfigurations": [
            {
              "Function": {
                "Fn::GetAtt": [
                  "FunctionsAcesImportNewFileUploaded",
                  "Arn"
                ]
              },
              "Event": "s3:ObjectCreated:*"
            }
          ]
        },
        "VersioningConfiguration": {
          "Status": "Suspended"
        }
      },
      "DeletionPolicy": "Delete"
    }