| by Arround The Web | No comments

How to Create S3 Bucket Using Terraform

Terraform is Infrastructure As a Code (IAC) platform, which is preferred by DevOps to create, manage and configure Cloud Resources and Services, as they are comfortable in coding and using command line interface. Terraform is a partner of AWS, so it means you can create, configure and manage your AWS resources using Terraform.

This post will teach how to create S3 Bucket using Terraform. S3 Bucket is an AWS Service for simple storage service that stores the data as an object in a bucket, to continue with this post make sure that AWS CLI and Terraform are installed in your Windows.

Create an S3 Bucket Using Terraform

Before starting the procedure, confirm that Terraform and AWS CLI are installed in your system.

Check the version of terraform by typing:

> terraform -v

In the output above, it is visible that Terraform is installed in the system.

Also, use this command to see the AWS CLI version:

> aws --version

It is visible that AWS CLI is also installed in the system.

The next step is to configure your AWS CLI, so type this command in Command Prompt and type the required parameters by copying them from your AWS account: 

> aws configure

Once your AWS is configured.

Create a folder named “aws_s3_bucket” using this command:

> mkdir aws_s3_bucket

Open this folder in any code editor:

Create a file named “provider.tf

Write this code for AWS provider:

terraform {
 required_providers {
   aws = {
     source  = "hashicorp/aws"
     version = "~> 4.0"
   }
 }
}

provider "aws" {
 region = "us-east-1"
}

Save this file:

Create a new file named “main.tf”:

Use this code to create the S3 resource:

resource "aws_s3_bucket" "b" {
 bucket = "linuxhint-terraform-bucket"

 tags = {
   Name        = "My Bucket"
   Environment = "Dev"
 }
}

Save the file:

Open the terminal, it can be either the terminal of Code editor or Command Prompt in this directory and type this command to initialize this workspace:

> terraform init 

It will consume some time to initialize and will give a success message once it is initialized:

Type this command to see if there is any error in the configuration or not:

> terraform plan

Use this command to execute the file and create S3 bucket:

> terraform apply

Type “yes” when continuity message appears while execution:

It will take some time and will display success message on the creation of S3 Bucket:

Let’s confirm it by going into the Amazon Management Console and searching and opening the S3 Buckets list:

In the Buckets dashboard, See if the S3 you created using Terraform is available or not:

You can see that the S3 bucket is created using Terraform successfully.

Conclusion

If you are a person who prefers a command line interface more than GUI, use Terraform to create an S3 bucket, by installing AWS CLI and Terraform in your Windows machine and Configuring AWS. Create a folder with “.tf” files for storing AWS provider code and S3 creation code. Initialize this folder and create an S3 Bucket by executing the code using the “terraform apply” command.

Share Button

Source: linuxhint.com

Leave a Reply