DEV Community

Devops Den
Devops Den

Posted on

Create an AWS S3 bucket using Terraform

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

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay