How to enable S3 bucket versioning using Terraform

This article is a part of my "100 data engineering tutorials in 100 days" challenge. (95/100)

S3 bucket versioning is like the trash function in operating systems. It saves the day when you accidentally remove something that should not be removed. It gets even better! Versioning allows you to restore the previous version when you overwrite a file that should not be overwritten.

In this article, I show you how to enable versioning of an S3 bucket using Terraform. Of course, keeping the old version and removed files costs money and, most likely, is unnecessary, so we should remove the old versions after some time. I will show you how to do it too!

We will need two things:

  • the name of the bucket
  • the number of days after which we want to remove the old versions

When we have all of that, we can define the bucket in Terraform configuration. In this example, I enable versioning of bucket called my_lovely_bucket. I want to remove the old versions after seven days:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
resource "aws_s3_bucket" "my_lovely_bucket" {
    bucket = "my_lovely_bucket"
    acl = "private"

    versioning {
        enabled = true
    }

    lifecycle_rule {
        enabled = true

        noncurrent_version_expiration {
            days = 7
        }
    }
}

Did you enjoy reading this article?
Would you like to learn more about software craft in data engineering and MLOps?

Subscribe to the newsletter or add this blog to your RSS reader (does anyone still use them?) to get a notification when I publish a new essay!

Newsletter

Do you enjoy reading my articles?
Subscribe to the newsletter if you don't want to miss the new content, business offers, and free training materials.

Bartosz Mikulski

Bartosz Mikulski

  • Data/MLOps engineer by day
  • DevRel/copywriter by night
  • Python and data engineering trainer
  • Conference speaker
  • Contributed a chapter to the book "97 Things Every Data Engineer Should Know"
  • Twitter: @mikulskibartosz
Newsletter

Do you enjoy reading my articles?
Subscribe to the newsletter if you don't want to miss the new content, business offers, and free training materials.