Skip to main content

Command Palette

Search for a command to run...

Creating an S3 Bucket Using Terraform

Terraform with AWS : Day - 3

Published
3 min read
Creating an S3 Bucket Using Terraform

A simple and practical guide to manage cloud storage with code

Amazon S3 is one of the most used cloud storage services. It helps you store files like images, videos, backups, and logs in a secure and scalable way. Instead of creating buckets manually from the AWS console every time, you can use Terraform to do it automatically and consistently. Let’s walk through how it works in a simple and clear way.


What is an S3 Bucket in Simple Terms

An S3 bucket is like a folder in the cloud where you can store files.
In real life, think of it as a storage box in a warehouse. You can put items in it, organize them, and control who can access them.

Terraform lets you create this storage box using a few lines of code.


Why Use Terraform to Create an S3 Bucket

Creating resources manually can lead to human errors. You may forget to enable encryption, choose the wrong region, or miss important settings.

With Terraform, everything is written as code, so you get:

Consistent setup across environments  
Easy reuse of configuration  
Better visibility of infrastructure

If you ever delete a bucket by mistake, you can recreate it using the same code in minutes.


Step by Step Process to Create an S3 Bucket

Follow these simple steps to create an S3 bucket using Terraform.

  1. Install and Configure AWS CLI

    Make sure your AWS credentials are configured in your system.

  2. Create a Terraform Project Folder

    Inside your folder, create a file named main.tf.

  3. Add Provider Configuration

    This tells Terraform to connect to AWS.

    Example structure:

     provider "aws" {
       region = "ap-south-1"
     }
    
  4. Define the S3 Bucket Resource

    This is where the actual bucket gets defined.

     resource "aws_s3_bucket" "my_bucket" {
       bucket = "my-unique-bucket-name-12345"
     }
    

    The bucket name must be globally unique.

  5. Initialize Terraform

    Run:

     terraform init
    
  6. Preview the Changes

    Run:

     terraform plan
    

    This command shows what will be created.

  7. Create the Bucket

    Run:

     terraform apply
    

    Confirm when prompted, and Terraform will create the bucket for you.

Best Practices for S3 Buckets with Terraform

When creating buckets, it is important to add security and management features.

You should enable:

Versioning to keep old file versions  
Server-side encryption for data safety  
Proper access control using IAM

These can also be added in Terraform so that your bucket is always created with safe defaults.


Final Thoughts

Creating an S3 bucket with Terraform makes your infrastructure reliable and repeatable. Instead of manually clicking through dashboards, you define everything once and reuse it anytime.

YouTube Link : Day - 3