Resource: aws_s3_bucket_versioning

Provides a resource for controlling versioning on an S3 bucket. Deleting this resource will either suspend versioning on the associated S3 bucket or simply remove the resource from Terraform state if the associated S3 bucket is unversioned.

For more information, see How S3 versioning works.

Example Usage

With Versioning Enabled

resource "aws_s3_bucket" "example" {
  bucket = "example-bucket"
}

resource "aws_s3_bucket_acl" "example" {
  bucket = aws_s3_bucket.example.id
  acl    = "private"
}

resource "aws_s3_bucket_versioning" "versioning_example" {
  bucket = aws_s3_bucket.example.id
  versioning_configuration {
    status = "Enabled"
  }
}

With Versioning Disabled

resource "aws_s3_bucket" "example" {
  bucket = "example-bucket"
}

resource "aws_s3_bucket_acl" "example" {
  bucket = aws_s3_bucket.example.id
  acl    = "private"
}

resource "aws_s3_bucket_versioning" "versioning_example" {
  bucket = aws_s3_bucket.example.id
  versioning_configuration {
    status = "Disabled"
  }
}

Object Dependency On Versioning

When you create an object whose version_id you need and an aws_s3_bucket_versioning resource in the same configuration, you are more likely to have success by ensuring the s3_object depends either implicitly (see below) or explicitly (i.e., using depends_on = [aws_s3_bucket_versioning.example]) on the aws_s3_bucket_versioning resource.

This example shows the aws_s3_object.example depending implicitly on the versioning resource through the reference to aws_s3_bucket_versioning.example.bucket to define bucket:

resource "aws_s3_bucket" "example" {
  bucket = "yotto"
}

resource "aws_s3_bucket_versioning" "example" {
  bucket = aws_s3_bucket.example.id

  versioning_configuration {
    status = "Enabled"
  }
}

resource "aws_s3_object" "example" {
  bucket = aws_s3_bucket_versioning.example.id
  key    = "droeloe"
  source = "example.txt"
}

Argument Reference

This resource supports the following arguments:

versioning_configuration

The versioning_configuration configuration block supports the following arguments:

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Import

In Terraform v1.5.0 and later, use an import block to import S3 bucket versioning using the bucket or using the bucket and expected_bucket_owner separated by a comma (,). For example:

If the owner (account ID) of the source bucket is the same account used to configure the Terraform AWS Provider, import using the bucket:

import {
  to = aws_s3_bucket_versioning.example
  id = "bucket-name"
}

If the owner (account ID) of the source bucket differs from the account used to configure the Terraform AWS Provider, import using the bucket and expected_bucket_owner separated by a comma (,):

import {
  to = aws_s3_bucket_versioning.example
  id = "bucket-name,123456789012"
}

Using terraform import to import S3 bucket versioning using the bucket or using the bucket and expected_bucket_owner separated by a comma (,). For example:

If the owner (account ID) of the source bucket is the same account used to configure the Terraform AWS Provider, import using the bucket:

% terraform import aws_s3_bucket_versioning.example bucket-name

If the owner (account ID) of the source bucket differs from the account used to configure the Terraform AWS Provider, import using the bucket and expected_bucket_owner separated by a comma (,):

% terraform import aws_s3_bucket_versioning.example bucket-name,123456789012