Search code examples
amazon-s3terraform

Terraform aws_s3_bucket_lifecycle_configuration failing to delete contents of S3 folder


I have an S3 bucket that is created via Terraform, as well as a lifecycle policy to expire everything under athena_results/ after 3 days. My code is below. The problem I'm experiencing is that this lifecycle policy isn't deleting the data under athena_results/ after 3 days. I'm curious what I'm doing. Is there something wrong or missing from my config? I've checked the docs, Google, and SO and I'm not finding anything to help.

I'm confused why this lifecycle rule wouldn't be cleaning up my Athena results. Thank you in advance if you have any ideas!

resource "aws_s3_bucket" "arapbi" {
  bucket = "arapbi"

  tags = {
    Name = "ARAPBI"
  }
}

resource "aws_s3_bucket_lifecycle_configuration" "arapbi" {
  bucket = aws_s3_bucket.arapbi.id

  rule {
    id     = "Lifecycle"
    status = "Enabled"

    filter {
      object_size_greater_than = "0"
      prefix                   = "athena_results/"
    }


    expiration {
      days = 3
    }

    noncurrent_version_expiration {
      noncurrent_days = 1
    }
  }
}

Solution

  • If you look at the examples you need to use the and to filter on more than one attribute. This is documented in the Examples

    resource "aws_s3_bucket_lifecycle_configuration" "arapbi" {
      bucket = aws_s3_bucket.arapbi.id
    
      rule {
        id     = "Lifecycle"
        status = "Enabled"
    
        filter {
          and {
            prefix                   = "athena_results/"
            object_size_greater_than = "0"
          }
        }
    
    
        expiration {
          days = 3
        }
    
        noncurrent_version_expiration {
          noncurrent_days = 1
        }
      }
    }
    

    As documented in the filter block config. You must specify exactly one filter

    The filter configuration block must either be specified as the empty configuration block (filter {}) or with exactly one of prefix, tag, and, object_size_greater_than or object_size_less_than specified.