How to enable S3 bucket versioning using Terraform?

To enable versioning on an S3 bucket using Terraform, you can use the following HCL code:

provider "aws" { region = "us-east-1" } resource "aws_s3_bucket" "my_bucket" { bucket = "my-unique-bucket-name" versioning { enabled = true } }

In this code snippet, we first specify the AWS provider with the desired region. Then, we create an S3 bucket resource called my_bucket with the desired bucket name. Finally, we use the versioning block to enable versioning on the bucket by setting enabled = true.

You can save this code in a Terraform configuration file (e.g., main.tf) and then run terraform init and terraform apply commands to create the S3 bucket with versioning enabled.