Hands on DevSecOps Coaching that is provided on AWS and Azure Cloud platforms. Contact Coach AK at devops.coaching@gmail.com for more info. You can also reach out to Coach AK at +1(469) 733-5248
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.
You can provision resources in AWS cloud using Terraform by two ways as mentioned below:
AWS Access keys + secret keys (un-secure way)
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.
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.
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.
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
The team’s efficient and organized processes enabled them to come up to speed quickly and understand the requirements. IT Consulting
ReplyDeleteThank 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