AWS S3 Bucket Creation and Management Using Terraform

AWS S3 Bucket Creation and Management Using Terraform

Prerequisites:

AWS S3 Bucket:

Amazon Simple Storage Service (Amazon S3) is a scalable object storage service offered by Amazon Web Services (AWS). It allows users to store and retrieve any amount of data from anywhere on the web. S3 is designed for durability, availability, and scalability, making it suitable for a wide range of use cases such as data backup, archival, data lakes, static website hosting, content distribution, and more.

Why AWS S3 buckets are useful:

  1. Scalability: S3 can scale virtually infinitely, allowing you to store any amount of data without worrying about capacity planning or provisioning additional infrastructure.

  2. Durability and Availability: S3 is designed to provide 99.999999999% (11 nines) durability of objects over a given year and 99.99% availability. It achieves this by storing data redundantly across multiple facilities and servers within an AWS region.

  3. Security: S3 provides several mechanisms to secure your data, including bucket policies, access control lists (ACLs), and AWS Identity and Access Management (IAM) roles and policies.

  4. Cost-effectiveness: S3 offers a pay-as-you-go pricing model, allowing you to pay only for the storage and data transfer you use, with no upfront costs or long-term commitments.

  5. Integration with other AWS Services: S3 integrates seamlessly with other AWS services like AWS Lambda, Amazon Athena, Amazon Redshift, and Amazon CloudFront, enabling you to build powerful, scalable applications.

Using Terraform to create S3 buckets offers several advantages:

  1. Infrastructure as Code (IaC): Terraform allows you to define your infrastructure as code, enabling you to version control, manage, and automate the provisioning of resources like S3 buckets. This approach improves consistency, repeatability, and collaboration among team members.

  2. State Management: Terraform maintains a state file that keeps track of the resources it manages. This state file helps Terraform understand the current state of your infrastructure and make necessary changes to achieve the desired state. Managing S3 buckets with Terraform ensures that your infrastructure state is consistent and reproducible.

  3. Modularity and Reusability: Terraform modules enable you to encapsulate and reuse infrastructure components, including S3 buckets, across multiple projects. This modular approach promotes code reuse, simplifies maintenance, and improves consistency.

  4. Plan and Apply Workflow: Terraform follows a plan and apply workflow, where it first generates an execution plan showing what changes will be made to your infrastructure and then applies those changes in a controlled manner. This workflow helps prevent unintended changes and ensures that infrastructure modifications are applied safely.

In summary, AWS S3 buckets provide scalable, durable, and cost-effective storage for various use cases, and using Terraform to manage S3 buckets offers benefits such as infrastructure as code, state management, modularity, and a plan and apply workflow.

Create an S3 bucket using Terraform:

Now Need to open Visual studio code. and install the hashicorp terraform extension in your visual studio code after that create a folder that is terraform-project and go the terraform-project folder after that create a another folder terraform-files.

Now go the mouse in terraform-files and right click, open the open in integrated terminal.

Now check we have terraform or not.

Now need o go the AWS Console and go to the IAM service create a user and provide the Administrator Access and create the access key (click here).

Now I hope you have aws cli in window and Ubuntu in your laptop, If not please install AWS CLI in window and Ubuntu (Click here).

Now go the Visual Studio Code and open the terminal and type aws --version , You can see we have AWS CLI

Now need to type aws configure and provide to access key with the user how to provide the access key just go the my blog and check (cleck here)

Now need the create a provider.tf file

provider.tf

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.44.0"
    }
  }
}

provider "aws" {
  region = "ap-south-1"
}

Now need to create a main.tf file need to create S3 Bucket.

resource "aws_s3_bucket" "my_bucket" {
  bucket = "terrabucket222"

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

Now need to apply terraform init, terraform plan and terraform apply commands.

Now you can check, go the the AWS and open the s3 service.

Now you can see we have created automatically s3 bucket using terraform.