DEV Community

jumpingcats
jumpingcats

Posted on

Using Terraform to create a bucket with versioning enabled (extra: an easy to use script)

Introduction

Amazon Simple Storage Service (S3) is a popular cloud storage solution that offers scalable, high-speed, and low-cost storage. In this blog, we will demonstrate how to use Terraform to set up an S3 bucket and enable versioning for it.

Prerequisites:

First, make sure that you have the following prerequisites:

Terraform installed on your system. If you don't have it, you can download it from https://www.terraform.io/downloads.html
An AWS account. If you don't have one, you can create one at https://aws.amazon.com/
AWS access keys. You can create these by following the instructions at https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys
Once you have these prerequisites, follow these steps to set up an S3 bucket using Terraform and enable versioning for it:

Steps:

  • Create a new directory for your Terraform project and navigate to it.
mkdir s3-bucket-terraform
cd s3-bucket-terraform
Enter fullscreen mode Exit fullscreen mode
  • Initialise Terraform in the current directory by running the following command:
terraform init
Enter fullscreen mode Exit fullscreen mode
  • Create a file named main.tf in the current directory and add the following code to it. This code defines the S3 bucket that we will create using Terraform.
resource "aws_s3_bucket" "my-bucket" {
  bucket = "my-bucket-name"
  acl    = "private"

  versioning {
    enabled = true
  }
}
Enter fullscreen mode Exit fullscreen mode

In this code, we use the aws_s3_bucket resource to create an S3 bucket named my-bucket-name with the private access control list (ACL). We also enable versioning for this bucket by setting the enabled property of the versioning block to true.

  • Set the AWS access keys as environment variables by running the following commands:
export AWS_ACCESS_KEY_ID="your-access-key-id"
export AWS_SECRET_ACCESS_KEY="your-secret-access-key"
Enter fullscreen mode Exit fullscreen mode
  • Run the following command to apply the changes defined in the Terraform code and create the S3 bucket:
terraform apply
Enter fullscreen mode Exit fullscreen mode

This command will prompt you to confirm the changes. Type yes and press Enter to continue. Terraform will then create the S3 bucket and enable versioning for it.

After the bucket is created, you can verify that it has been created and that versioning is enabled for it by going to the S3 section of the AWS Management Console and checking the list of S3 buckets. You should see the my-bucket-name bucket in the list, and if you click on it, you should see the versioning information in the Properties tab.

Congratulations! You have successfully set up an S3 bucket using Terraform and enabled versioning for it. This will allow you to store multiple versions of an object in your S3 bucket and easily recover earlier versions if needed.

Extra:

The following shell script can be used to set up an S3 bucket using Terraform and enable versioning for it. This script takes the bucket name as an argument, so you can use it to create multiple S3 buckets with different names. Before running this script, make sure that you have the prerequisites mentioned in the previous answer, such as Terraform and AWS access keys.

#!/bin/bash

# check if a bucket name was provided as an argument
if [ $# -eq 0 ]; then
  echo "Error: No bucket name provided. Usage: ./setup-s3-bucket.sh <bucket-name>"
  exit 1
fi

# create a new directory for the Terraform project
mkdir s3-bucket-terraform
cd s3-bucket-terraform

# initialize Terraform in the current directory
terraform init

# create a file named main.tf in the current directory and add the following code to it
cat << EOF > main.tf
resource "aws_s3_bucket" "my-bucket" {
  bucket = "$1"
  acl    = "private"

  versioning {
    enabled = true
  }
}
EOF

# set the AWS access keys as environment variables
export AWS_ACCESS_KEY_ID="your-access-key-id"
export AWS_SECRET_ACCESS_KEY="your-secret-access-key"

# apply the changes defined in the Terraform code and create the S3 bucket
terraform apply
Enter fullscreen mode Exit fullscreen mode

To run this script, save it to a file, such as setup-s3-bucket.sh, and make it executable using the following command:

chmod +x setup-s3-bucket.sh
Enter fullscreen mode Exit fullscreen mode

Then, run the script and provide the bucket name as an argument, like this:

./setup-s3-bucket.sh my-bucket-name
Enter fullscreen mode Exit fullscreen mode

The script will create the S3 bucket and enable versioning for it. You can verify this by going to the S3 section of the AWS Management Console and checking the list of S3 buckets. You should see the my-bucket-name bucket in the list, and if you click on it, you should see the versioning information in the Properties tab.

You can run the script multiple times with different bucket names to create multiple S3 buckets. For example:

./setup-s3-bucket.sh my-bucket-name-1
./setup-s3-bucket.sh my-bucket-name-2
Enter fullscreen mode Exit fullscreen mode

This will create two S3 buckets named my-bucket-name-1 and my-bucket-name-2, respectively, and enable versioning for both of them.

Thanks :)

If this blog helped you in any way, follow me on twitter for more such stuff (https://twitter.com/_jumpingcats)

Top comments (0)