Create an AWS S3 bucket using Terraform

Devops Den - Jun 25 - - Dev Community

Install Terraform: Ensure that you have Terraform installed. You can download it from the Terraform website.

Create a Directory for Your Terraform Configuration: Create a directory for your Terraform configuration files.

mkdir terraform-s3
cd terraform-s3

Enter fullscreen mode Exit fullscreen mode

Create a Terraform Configuration File: Create a file named main.tf and add the following content:

# main.tf

# Specify the provider
provider "aws" {
  region = "us-west-2"  # Change this to your preferred region
}

# Create an S3 bucket
resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-unique-bucket-name"  # Change this to your preferred bucket name
  acl    = "private"

  tags = {
    Name        = "MyBucket"
    Environment = "Dev"
  }
}

# Optionally, add a bucket policy
resource "aws_s3_bucket_policy" "my_bucket_policy" {
  bucket = aws_s3_bucket.my_bucket.id

  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Action    = "s3:GetObject"
        Effect    = "Allow"
        Principal = "*"
        Resource  = "${aws_s3_bucket.my_bucket.arn}/*"
      }
    ]
  })
}

Enter fullscreen mode Exit fullscreen mode

Initialize Terraform: Initialize your Terraform configuration. This will download the AWS provider plugin.

terraform init

Enter fullscreen mode Exit fullscreen mode

Apply the Configuration: Apply the Terraform configuration to create the S3 bucket.

terraform apply

Enter fullscreen mode Exit fullscreen mode

Thank You
Read More About S3 Bucket

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player