Using Terraform to Automate Your AWS S3 Bucket Setup.

Using Terraform to Automate Your AWS S3 Bucket Setup.

Terraform.

Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp, that lets you build, change, and version cloud and on-prem resources safely and efficiently in human-readable configuration files that you can version, reuse, and share.

Amazon S3.

Amazon Web Services (AWS) S3 (Simple Storage Service) is a scalable, high-performance cloud storage service that allows businesses and developers to store and retrieve data from anywhere on the web. It is probably the most commonly used, go-to storage service for AWS users given the features like extremely high availability, security, and simple connection to other AWS Services. Includes features like versioning, lifecycle management, and replication (cross-region or same-region replication). S3 is designed for 99.999999999% (11 9s) of durability, meaning your data is safe and highly available.

In this article, I will walk you through the process of launching an Amazon S3 bucket using a Terraform script, with Visual Studio Code (VSCode) as the code editor.

Prerequisite:

  • AWS Account
  • Terraform Installation
  • AWS CLI

TASK1 : Create IAM user.

Step 1: Navigate the IAM and click on User .

Screenshot 2025 01 02 105254

STEP 2: Click on create user.

Screenshot 2025 01 02 105307

STEP 3: Enter the user name and click on next button.

Screenshot 2025 01 02 105826

STEP 4: Select attach policies directly.

  • Tick on amazons3full access.
  • Then, next click on next button.
Screenshot 2025 01 02 110401

STEP 5: Click on create user.

Screenshot 2025 01 02 11044211

TASK 2: Create access key.

STEP 1: Click your user name and select on create access key.

  • Select the command line interface and tick confirmation.
  • Click on create access key.
  • Download the csv.file.
  • You will get Access key and secret key.
Screenshot 2025 01 02 11050711
Screenshot 2025 01 02 110621
Screenshot 2025 01 02 11063911
Screenshot 2025 01 02 11083211

TASK 3: Configure Profile Using AWS Configure Command.

STEP 1: Configuration.

aws configure
configure list
  • You will paste your Access key.
  • Paste your Secret access key.
  • Click on enter.
Screenshot 2025 01 02 111308
Screenshot 2025 01 02 111645

TASK 4: Create terraform file(.tf) & write the script.

STEP 1: Open your folder on your VScode.

  • Create the provider.tf file.
  • Enter the following script.
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.82.2"
    }
  }
}

provider "aws" {
  # Configuration options
  region ="us-east-1"
}

STEP 2: Save the file.

Screenshot 2025 01 02 100331

STEP 3: Open the terminal and enter the terraform init.

Screenshot 2025 01 02 100528

STEP 4: Create the another main.tf file.

  • Enter the following script and save the file.
# Bucket creation
resource "aws_s3_bucket" "my_s3_bucket"{
    bucket = "mys3testbucket0"

    tags = {
    Name = "My bucket"
    Enviroment ="Dev"
}
}
Screenshot 2025 01 02 11472911

TASK 5: Execute the terraform script.

STEP 1: Go to terminal enter the terraform plan.

Screenshot 2025 01 02 113548

STEP 2: Then, next Enter the terraform apply.

Screenshot 2025 01 02 114820

TASK 6: Verify S3 bucket is create or not.

STEP 1: Go to S3 bucket and verify your bucket.

Screenshot 2025 01 02 11492211

STEP 2: If you are delete your bucket you the terraform destroy command.

Screenshot 2025 01 02 115009

Conclusion.

In this tutorial, we’ve successfully demonstrated how to launch an Amazon S3 bucket using a Terraform script, with Visual Studio Code as our development environment. By following these steps, you can easily automate the creation and management of S3 buckets, saving time and reducing human error in your cloud infrastructure setup. Terraform’s declarative approach ensures that your configurations are repeatable and consistent, making it an ideal tool for scalable cloud management.

With this foundational knowledge, you can expand on this setup, integrating other AWS resources, and further optimizing your cloud environment using Terraform. Happy coding and deploying!

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *