Terraform Create S3 Bucket

Here’s an example of how to use Terraform to create a new Amazon Simple Storage Service (S3) bucket:

First, you need to specify the provider for the S3 service in your Terraform configuration file:

provider "aws" {
  region = "us-west-2"

Next, you can use the aws_s3_bucket resource to create a new bucket:

resource "aws_s3_bucket" "example" {
  bucket = "my-new-bucket"
  acl    = "private"

This creates a new S3 bucket called “my-new-bucket” with a private access control list (ACL).

Run terraform init to download the AWS provider plugin.

Run terraform apply to create the S3 bucket

You can also specify additional options such as the bucket’s location constraint, the storage class of the bucket, and the versioning configuration by using the appropriate arguments in the aws_s3_bucket resource block.

It’s important to note that when you create a S3 bucket using terraform, the bucket and all its content will be deleted when you run terraform destroy command. Also, you need to have access to the AWS credentials in order to run this.

You can also use the terraform import command to import an existing S3 bucket into your Terraform state, this way you can manage it using Terraform.

Content Protection by DMCA.com