title: How to add lifecycle rules to an S3 bucket using terraform?
body: <p>I am using Terraform to create a bucket in S3 and I want to add "folders" and lifecycle rules to it.</p>
  
  <p>I can create the bucket (using an "aws_s3_bucket" resource).</p>
  
  <p>I can create the bucket and define my lifecycle rules within the same "aws_s3_bucket" resource, ie. at creation time.</p>
  
  <p>I can add "folders" to the bucket (I know they aren't really folders, but they are presented to the client systems as if they were... :-) ), using an "aws_s3_bucket_object" resource, ie. after bucket creation.</p>
  
  <p>All good...</p>
  
  <p>But I want to be able to add lifecycle rules AFTER I've created the bucket, but I get an error telling me the bucket already exists. (Actually I want to be able to subsequently add folders and corresponding lifecycle rules as and when required.)</p>
  
  <p>Now, I can add lifecycle rules to an existing bucket in the AWS GUI, so I know it is a reasonable thing to want to do.</p>
  
  <p>But is there a way of doing it with Terraform?</p>
  
  <p>Am I missing something?</p>
  
  <pre><code>resource "aws_s3_bucket" "bucket" {
      bucket      = "${replace(var.tags["Name"],"/_/","-")}"
      region      = "${var.aws_region}"
  
      #tags                = "${merge(var.tags, map("Name", "${var.tags["Name"]}"))}"
      tags                = "${merge(var.tags, map("Name", "${replace(var.tags["Name"],"/_/","-")}"))}"
  }
  
  
  resource "aws_s3_bucket" "bucket_quarterly" {
      bucket      = "${aws_s3_bucket.bucket.id}"
      #region      = "${var.aws_region}"
  
      lifecycle_rule {
          id      = "quarterly_retention"
          prefix  = "quarterly/"
          enabled = true
  
          expiration {
              days = 92
          }
      }
  
  }
  
  
  resource "aws_s3_bucket" "bucket_permanent" {
      bucket      = "${aws_s3_bucket.bucket.id}"
      #region      = "${var.aws_region}"
  
      lifecycle_rule {
          id      = "permanent_retention"
          enabled = true
          prefix  = "permanent/"
  
          transition {
              days            = 1
              storage_class   = "GLACIER"
          }
      }
  
  }
  
  
  resource "aws_s3_bucket_object" "quarterly" {
      bucket  = "${aws_s3_bucket.bucket.id}"
      #bucket  = "${var.bucket_id}"
      acl     = "private"
      key     = "quarterly"
      source  = "/dev/null"
  }
  
  
  resource "aws_s3_bucket_object" "permanent" {
      bucket  = "${aws_s3_bucket.bucket.id}"
      #bucket  = "${var.bucket_id}"
      acl     = "private"
      key     = "permanent"
      source  = "/dev/null"
  }
  </code></pre>
  
  <p>I expect to have a bucket with 2 lifecycle rules, but I get the following error:</p>
  
  <p>Error: Error applying plan:</p>
  
  <pre><code>2 error(s) occurred:
  
  * module.s3.aws_s3_bucket.bucket_quarterly: 1 error(s) occurred:
  
  * aws_s3_bucket.bucket_quarterly: Error creating S3 bucket: BucketAlreadyOwnedByYou: Your previous request to create the named bucket succeeded and you already own it.
      status code: 409, request id: EFE9C62B25341478, host id: hcsCNracNrpTJZ4QdU0AV2wNm/FqhYSEY4KieQ+zSHNsj6AUR69XvPF+0BiW4ZOpfgIoqwFoXkI=
  * module.s3.aws_s3_bucket.bucket_permanent: 1 error(s) occurred:
  
  * aws_s3_bucket.bucket_permanent: Error creating S3 bucket: BucketAlreadyOwnedByYou: Your previous request to create the named bucket succeeded and you already own it.
      status code: 409, request id: 7DE1B1A36138A614, host id: 8jB6l7d6Hc6CZFgQSLQRMJg4wtvnrSL6Yp5R4RScq+GtuMW+6rkN39bcTUwQhzxeI7jRStgLXSc=
  
  Terraform does not automatically rollback in the face of errors.
  Instead, your Terraform state file has been partially updated with
  any resources that successfully completed. Please address the error
  above and apply again to incrementally change your infrastructure.
  </code></pre>