How to deploy an S3 Bucket in AWS- using Terraform

Reading Time: 3 minutes

In this blog, we will create a Terraform Script for deploying an s3 bucket in AWS. S3 bucket is a simple storage service in the AWS cloud. It allows to store and access any amount of data. It stores all the data as objects, that is, it is an object-based storage service.

Using a terraform script, the entire infrastructure can be managed by declaring all the components of the infrastructure in the form of code.

Steps to follow for creating an S3 bucket using terraform:

To begin with, AWS CLI must be installed, use the command below to make sure.

aws --version

Step 1: In your terminal, using the following commands create a directory and navigate into the directory for creating a terraform configuration file for the provision of an s3 bucket.

mkdir terraform

cd terraform && nano s3_bucket.tf    

Step 2: Now, in this file, write the following code.
(Note- It uses .tf file extension for the plain text files in which the terraform code is stored.)

In the created file,

provider "aws"{

  region     = "us-east-1"

  access_key = "own_access_key"

  secret_key = "own_secret_key"

}

The above part of the code represents the provider that is set to AWS and its further details like region, access key, and secret key using which AWS has been configured.

resource "aws_s3_bucket" "first"{

  bucket = "pavneet-1-s3-bucket"

}

resource "aws_s3_bucket_acl" "example1" {

  bucket = aws_s3_bucket.first.id

  acl    = "private"

}

Then the second part of the code in the file above represents:

  • The type of resource “aws_s3_bucket” is defined along with the name of the resource that is “first”. Also, the name of the bucket is “pavneet-1-s3-bucket”.
  • The “aws_s3_bucket_acl”, is a type of resource that is used to provide ACL (Access Control List- allows to manage the access to the defined bucket) to the S3 bucket defined. And the value for this is set to private.

Step 3: Now use the following command “terraform init”, for terraform to install the required plugins defined in the file.

Step 4: Run the command “terraform plan”. This is used for verification of errors in the file and also shows the changes that will be made in order to achieve the desired state.

Step 5: For creating the S3 bucket, use “terraform apply”. This command is used to make the changes in the real infrastructure in order to execute the plan created above.

Step 6: After completion of the above steps, log in to the AWS console. The creation of the defined S3 bucket can be verified.

Conclusion:

So with the help of the above steps, an S3 bucket in AWS using terraform can be created easily. For more information on the S3 bucket in terraform, refer to this link.

Leave a Reply