Monday, July 12, 2021

Terraform create S3 bucket example | How to create S3 bucket in AWS using Terraform

Terraform is an infrastructure orchestration tool for creating web services in AWS automatically. You can use Terraform for provisioning any resources in AWS. We will learn how to create S3 bucket in AWS using Terraform.

Watch this on YouTube channel:
Pre-requisites:
You can provision resources in AWS cloud using Terraform by two ways as mentioned below:
  1. AWS Access keys + secret keys (un-secure way)
  2. Create an IAM Role with AmazonS3FullAccess Policy. (more secure way)

Option 2 is recommended approach as we already installed Terraform on EC2 instance that is inside AWS cloud. So we do not need to use Access Keys + secret keys. But if you have installed Terraform on your local machine you would need to go with Option1.

Terraform Script to create S3 bucket in AWS

You can clone the entire code from my GitHub Repo 

Create Terraform variables file

sudo vi variables.tf

variable "aws_region" {
description = "The AWS region to use to create resources."
default = "us-east-2"
}
variable "bucket_prefix" {
type = string
description = "(required since we are not using 'bucket') Creates a unique bucket name beginning with the specified prefix"
default = "my-s3bucket-"
}
variable "tags" {
type = map
description = "(Optional) A mapping of tags to assign to the bucket."
default = {
environment = "DEV"
terraform = "true"
}
}
variable "versioning" {
type = bool
description = "(Optional) A state of versioning."
default = true
}
variable "acl" {
type = string
description = " Defaults to private "
default = "private"
}


Create output.tf file

sudo vi outputs.tf

output "s3_bucket_name" {
  value = aws_s3_bucket.my-s3-bucket.id
}
output "s3_bucket_region" {
    value = aws_s3_bucket.my-s3-bucket.region
}

Create main.tf file

sudo vi main.tf

provider "aws" {
  region = var.aws_region
}
resource "aws_s3_bucket" "my-s3-bucket" {
  bucket_prefix = var.bucket_prefix
  acl = var.acl
  
   versioning {
    enabled = var.versioning
  }
  
  tags = var.tags
}

Execute Terraform commands
Now execute the below command:
terraform init
you should see like below screenshot.


Execute the below command
terraform plan
the above command will show how many resources will be added.
Plan: 1 to add, 0 to change, 0 to destroy.

Execute the below command
terraform apply
Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.


Now login to AWS--> S3, to see the new bucket created.



If you are having any errors related to credentials make sure you have access to AWS by attaching IAM role with AmazonS3FullAccess or access keys + secret keys are setup.

2 comments:

  1. The team’s efficient and organized processes enabled them to come up to speed quickly and understand the requirements. IT Consulting

    ReplyDelete
  2. Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. Scumbuckets

    ReplyDelete

DevSecOps Bootcamp Dec 2024 Schedule | DevSecOps & AWS Azure Cloud Coaching by Coach AK | DevSecOps and Cloud Computing Online Classes

Lot of new topics covered like GitHub Actions, GitHub Advanced Security, Helm, Prometheus and Grafana..) The DevOps requirements in the IT m...