21 Days of AWS using Terraform — Day 11- Introduction to S3 using Terraform

Prashant Lakhera
2 min readNov 26, 2019

--

Welcome to Day 11 of 21 Days of AWS using Terraform, the topic for today is Introduction to S3 using terraform.

What is AWS S3?

AWS Simple Storage Service(S3) provides secure, durable and highly scalable object storage. S3 is easy to use and we can store and retrieve any amount of data from anywhere on the web.

Let’s create S3 bucket using terraform

provider "aws" {
region = "us-west-2"
}
resource "aws_s3_bucket" "my-test-bucket" {
bucket = "${var.s3_bucket_name}"
acl = "private"
versioning {
enabled = true
}
tags = {
Name = "21-days-of-aws-using-terraform"
}
}

For more info

https://www.terraform.io/docs/providers/aws/r/s3_bucket.html

bucket: name of the bucket, if we ommit that terraform will assign random bucket name
acl: Default to Private(other options public-read and public-read-write)
versioning: Versioning automatically keeps up with different versions of the same object.

NOTE: Every S3 bucket must be unique and that why random id is useful to prevent our bucket to collide with others.

To implement that let use random_id as providers

resource "random_id" "my-random-id" {
byte_length = 2
}

byte_length - The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.

https://www.terraform.io/docs/providers/random/r/id.html

Now to use that in your code

provider "aws" {
region = "us-west-2"
}
resource "random_id" "my-random-id" {
byte_length = 2
}
resource "aws_s3_bucket" "my-test-bucket" {
bucket = "${var.s3_bucket_name}-${random_id.my-random-id.dec}"
acl = "private"
versioning {
enabled = true
}
tags = {
Name = "21-days-of-aws-using-terraform"
}
}

To read the complete blog

GitHub Link

https://github.com/100daysofdevops/21_days_of_aws_using_terraform/tree/master/s3

Looking forward for you guys to join this journey

--

--

Prashant Lakhera
Prashant Lakhera

Written by Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

No responses yet