Create S3 bucket with versioning enabled with CFN

TOC

Use CloudFormation to create S3 buckets with versioning enabled

One of the features S3 offers is versioning.

Versioning in Amazon S3 is a means of keeping multiple variants of an object in the same bucket. You can use the S3 Versioning feature to preserve, retrieve, and restore every version of every object stored in your buckets.

Using versioning in S3 buckets

In this case, we will use CloudFormation to create an S3 bucket with versioning enabled.

Environment

Diagram of create S3 bucket with versioning enabled by CloudFormation

Create an S3 bucket.

Enable versioning for the bucket.

CloudFormation template files

Build the above configuration with CloudFormation.
The CloudFormation templates are located at the following URL

https://github.com/awstut-an-r/awstut-fa/tree/main/109

Explanation of key points of the template files

S3 bucket

Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      AccessControl: Private
      BucketName: !Ref Prefix
      VersioningConfiguration:
        Status: Enabled
Code language: YAML (yaml)

Versioning is set by the VersioningConfiguration property.

Versioning is enabled by setting the internal Status property to “Enabled”.

Architecting

Use CloudFormation to build this environment and check the actual behavior.

Create CloudFormation stacks and check resources in stacks

Create a CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following page

あわせて読みたい
CloudFormation’s nested stack 【How to build an environment with a nested CloudFormation stack】 Examine nested stacks in CloudFormation. CloudFormation allows you to nest stacks. Nested ...

After checking the resources in each stack, information on the main resources created this time is as follows

  • S3 bucket: fa-109

Check each resource from the AWS Management Console.

Check the S3 bucket.

Detail of S3 1.

It is indeed versioning is enabled.

Checking Action

Object Preserve

Now that everything is ready, place objects in this bucket.
Using the AWS CLI, place a test file

$ echo 'hogehoge' > sample.txt

$ aws s3 cp sample.txt s3://fa-109/
upload: ./sample.txt to s3://fa-109/sample.txt
Code language: Bash (bash)

Check the status of the S3 bucket.

Detail of S3 2.

The test file is indeed placed in the bucket.

Check the versioning information of this object.

Detail of S3 3.

Versioning information is displayed.
Since the object has just been placed in the bucket, the current status is the latest version.

Object Update

Next, we check the behavior of the object when it is updated.
Update the object again from AWS CLI.

$ echo 'fugafuga' >> sample.txt

$ aws s3 cp sample.txt s3://fa-109/
upload: ./sample.txt to s3://fa-109/sample.txt
Code language: Bash (bash)

Check the status of the S3 bucket again.

Detail of S3 4.

The version has been updated because the object has been updated.
As you can see, not only the latest version, but also older versions of the object can be downloaded.

You can download from the management console like this, but you can also download older versions of objects from the AWS CLI.

The procedure is as follows

  • Confirm the version ID from the object’s version list
  • Obtain the object by specifying the version ID

Let’s actually do this.

First, check the version list of the object.

$ aws s3api list-object-versions \
--bucket fa-109 \
--prefix sample.txt
{
    "Versions": [
        {
            "ETag": "\"304693ccbc28bbeac4689b24bda76e0e\"",
            "Size": 18,
            "StorageClass": "STANDARD",
            "Key": "sample.txt",
            "VersionId": "pihuAbcKaZeTLRXpUXecgeT0NihYkpnM",
            "IsLatest": true,
            "LastModified": "2022-12-27T22:43:18+00:00",
            "Owner": {
                "DisplayName": "[owner-name]",
                "ID": "[owner-id]"
            }
        },
        {
            "ETag": "\"d9a3fdfc7ca17c47ed007bed5d2eb873\"",
            "Size": 9,
            "StorageClass": "STANDARD",
            "Key": "sample.txt",
            "VersionId": "i6yvYFzBM45jJHa1yyosvPxtw2Kyrozf",
            "IsLatest": false,
            "LastModified": "2022-12-27T22:32:22+00:00",
            "Owner": {
                "DisplayName": "[owner-name]",
                "ID": "[owner-id]"
            }
        }
    ]
}
Code language: Bash (bash)

The first is the latest version.
This can be determined from the IsLatest value.

The second is the older version.
The value of VersionId is “pihuAbcKaZeTLRXpUXecgeT0NihYkpnM,” which is the version ID of the older version.

Now that we know the version ID, we can retrieve the old version object.

$ aws s3api get-object \
--bucket fa-109 \
--key sample.txt \
--version-id i6yvYFzBM45jJHa1yyosvPxtw2Kyrozf \
sample_old.txt
{
    "AcceptRanges": "bytes",
    "LastModified": "2022-12-27T22:32:22+00:00",
    "ContentLength": 9,
    "ETag": "\"d9a3fdfc7ca17c47ed007bed5d2eb873\"",
    "VersionId": "i6yvYFzBM45jJHa1yyosvPxtw2Kyrozf",
    "ContentType": "text/plain",
    "Metadata": {}
}

$ cat sample_old.txt
hogehoge
Code language: Bash (bash)

The object has been downloaded.
The content is indeed the one before the update.

Summary

We have seen how to use CloudFormation to create an S3 bucket with versioning enabled and how to retrieve an old version of an object.

TOC