Home > front end >  Terraform & S3 lifecycle: How to expire specific objects?
Terraform & S3 lifecycle: How to expire specific objects?

Time:11-18

I am writing this S3 bucket lifecycle rule:

 lifecycle_rules = [
    {
      id      = "log"
      enabled = true
      prefix  = "log/"

      tags = {
        "rule"      = "log"
        "autoclean" = "true"
      }
      expiration = {
        days = 365
      }
    }
  ]

An ASCII mock diagram of the bucket is provided below: Logs are dynamically written everyday in this bucket. Some in the folder logs/, some in the subfolder failed_logs/ and some on the root folder of the bucket.

BUCKET/
|
  nonlogfile.exe
  log_file_that_i_DONT_wanna_delete.log
  logs/ 
      |
       file1     (Created 2021-01-01)
       file2     (Created 2021-01-02)
       failed_logs/
                  |
                   file3 (Created 2021-01-03) 
                   file4 (Created 2021-01-04)

My target is to expire everyfile in the log/ folder (and its subfolder failed_logs after 365 days.

`file1` would expire at 2022-01-01
`file2` would expire at 2022-01-02
`file3` would expire at 2022-01-03
`file4` would expire at 2022-01-04

whereas

nonlogfile.exe
log_file_that_i_DONT_wanna_delete.log

would never expire.

Terraform's aws_s3_bucket documentation states that:

The lifecycle_rule object supports the following:

prefix - (Optional) Object key prefix identifying one or more objects to which the rule applies.

tags - (Optional) Specifies object tags key and value.

  • I don't understand the prefix attribute
  • My files are not tagged

How can I target them as explained above, based on their location on the bucket?

CodePudding user response:

S3 Structure Explained

The prefix attribute is simply the path to your s3 object minus the bucket name. If you had the following S3 "directory" s3://ec-integration-files/logs/ the prefix value would simply be logs/.

The attribute is named prefix as opposed to path because while s3 looks like a directory tree structure technically it is flat storage. However the Management Console displays the objects as if they are stored in a directory structure, and it uses the prefixes to do this.

One other point of clarification, you can have two objects at the root ( or at any depth ) named logs and logs/. One of these will be displayed as a directory in the management console and the other as a file object, but they are really just two objects with different names

  • Related