Open In App

How To Create AWS S3 Bucker Using Terraform ?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

S3 stands for Simple Storage Service. S3 buckets are cloud storage services by Amazon Web Service. It is used to store objects, It consists of data in any format like documents, images, videos, and application code. These are highly scalable.

What Is Terraform?

Terraform is an open-source infrastructure as code (IaC) tool. It is maintained and developed by Hashicorp. It is used to configure the infrastructure using code. The infrastructure is declared in a configuration language called HCL(Hashicorp Certified Language). Terraform enables the automation of infrastructure provisioning with cloud providers such as AWS, Microsoft Azure, and Google Cloud.

How to create an S3 bucket using Terraform on AWS?

Prerequisite

  1. AWS account.
  2. Terraform installation.
  3. AWS CLI.

Step 1: Open the cmd if you are in Windows and configure aws using the aws configure command.

aws configure

aws configure

Step 2: After that open a code editor(vs code) and make a file called provider.tf

.tf is the file extension for the terraform file. This file will store the information about the cloud provider, version, and region for the s3 bucket. Use the following code to set the provider, version, and region.

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

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

provider.tf file and terraform init command

After this open terminal where this file is located and type terraform init to initialize terraform. The Terraform init command prepares the working directory for use with Terraform. It initialises the backend, any child module installation and any plugin installation.

Step 3: Now we have defied our provider.Let’s create S3 bucket , make a file called main.tf in the same directory where provider.tf file is located. In this file we have define the resource , bucket name. The bucket name should be unique , here the bucket name is “my-s3-test-bucket02” . Here is the following code :

# Bucket creation
resource "aws_s3_bucket" "my_s3_bucket"{
    bucket = "my-s3-test-bucket02"

    tags = {
    Name = "My bucket"
    Enviroment ="Dev"
}
}

main.tf  file and terraform plan command

After this run terraform plan command.The plan command determines the deltas between the current configuration and prior state data.

Step 4: Now you have to run terraform apply command to create a S3 bucket in AWS.

terraform apply

terraform apply command

This command will start create S3 bucket in aws console. After few minutes the bucket has been created.

Step 5 : To confirm that your S3 bucket has been created, head over to the AWS management console then go to S3 then Buckets. You will see that our bucket “my-s3-test-bucket02” has been created.

aws bucket creation

Step 6: If you’d like to clean up this S3 bucket you can run the command terraform destroy in the terminal.

terraform destroy

terrafrom destroy

AWS S3 Bucket Using Terraform – FAQs

How to destroy an S3 bucket provisioned with Terraform?

Use the Terraform destroy command to remove the S3 bucket.

How to enable server-side encryption for the S3 bucket using Terraform?

To enable server-side encryption you have to provide appropriate encryption configuration in the Terraform script.

What are the prerequisites for creating an S3 bucket with Terraform?

  1. An AWS account with permissions
  2. Terraform installed on the local machine
  3. AWS CLI

How to specify the region for the S3 bucket using Terraform?

Specify the desired region for the S3 bucket using Terraform’s region parameter within the resource block.

How to import an existing S3 bucket into Terraform’s state?

Use Terraform import command to import existing resources into Terraform’s state for management.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads