6
votes

I am trying to write a cloudformation template which involves adding an event to a bucket to trigger a lambda function.

I know i can use code such as the below to create a bucket and an event at the same time but my bucket already exists and i don't want another one so is there a way of creating an event for an existing bucket within cloud formation?

"EncryptionServiceBucket" : {
  "Type" : "AWS::S3::Bucket",
  "Properties" : {
    "BucketName" : { "Fn::Sub" : "${User}-encryption-service" },
    "NotificationConfiguration" : {
      "LambdaConfigurations" : [{
        "Function" : { "Ref" : "LambdaDeploymentArn" },
        "Event" : "s3:ObjectCreated:*",
        "Filter" : {
          "S3Key" : {
            "Rules" : [{
              "Name" : "suffix",
              "Value" : "zip"
            }]
          }
        }
      }]
    }
  }
}
1

1 Answers

3
votes

I have not found a way to manage existing resources with CloudFormation. Also, using the BucketName property on a bucket limits CloudFormation's ability to manage your bucket significantly. For example, it cannot replace the resource, or create it again in another stack in your account. I suggest instead to leave out this property, let Cloudformation create bucket names and reference the bucket's ARNs in your with via environment variables set in the same stack.