Using CloudFormation with an existing S3 bucket

amazon s3amazon-cloudformation

Using CloudFormation, I want to set some of the properties in AWS::S3::Bucket on an existing bucket. In other words, I don't want to create the bucket, I just want to enforce some of the settings. Here's an example of my CloudFormation JSON:

    "websitePreviewBucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "AccessControl": "PublicRead",
        "VersioningConfiguration": {
          "Status": "Suspended"
        },
        "BucketName": "preview.website.com",
        "WebsiteConfiguration": {
          "IndexDocument": "index.html",
          "ErrorDocument": "error.html"
        }
      }
    },

Not surprisingly, this fails in the CloudFormation console:

The following resource(s) failed to create: [websitePreviewBucket].
preview.website.com already exists

I've created the bucket preview.website.com. I mean, this account owns that bucket. How can I set things like AccessControl and WebsiteConfiguration on an existing bucket with CloudFormation?

I've seen another question asking something similar, but it doesn't have a suitable answer.

Best Answer

I believe you are mistaken in using CloudFormation to modify your AWS infrastructure. CloudFormation's goal is to create AWS infrastructure in a templated fashion. It has been extended to allow for some management of the resources it creates, but managing existing infrastructure is not it's goal. From the welcome page:

AWS CloudFormation enables you to create and provision AWS infrastructure deployments predictably and repeatedly.

I believe the closest you will be able to get is to set a bucket policy on an existing bucket using AWS::S3::BucketPolicy. Beyond that you can use the AWS CLI S3 API to modify your bucket: