Search code examples
amazon-web-servicesamazon-s3terraformterraform-provider-aws

Terraform: is there a way to combine multiple lifecycle policies in one resource


I am trying to add terraform 2 lifecycle bucket policies, but it looks like I need to create 2 separate resources for 2 policies.

Is there a way to do it within single resource? I want to combine dest1 and dest 2 into single resource

My code

resource "aws_s3_bucket" "artifact" {
  bucket = "jatin-123"
}

resource "aws_s3_bucket_lifecycle_configuration" "dest1" {
  bucket = aws_s3_bucket.artifact.bucket
  rule{
    id     = "expire"
    status = "Enabled"
    expiration {
      days = 30
    }
  }
}

resource "aws_s3_bucket_lifecycle_configuration" "dest2" {
  bucket = aws_s3_bucket.dest.id

  rule {
    id     = "Delete old versions after 3 days"
    status = "Enabled"

    noncurrent_version_expiration {
      noncurrent_days = 3
    }
  }
}

Solution

  • Adding expiration block to rule helped me achieved what I was looking for.

    resource "aws_s3_bucket_lifecycle_configuration" "dest" {
      bucket = aws_s3_bucket.artifact.bucket
      rule {
        id     = "expire"
        status = "Enabled"
        expiration {
          days = 30
        }
        noncurrent_version_expiration {
          noncurrent_days = 3
        }
      }
    }