21 Days of AWS using Terraform — Day 11- Introduction to S3 using Terraform
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
- Website: http://100daysofdevops.com/
- Twitter: @100daysofdevops OR @lakhera2015
- Facebook: https://www.facebook.com/groups/795382630808645/
- Medium: https://medium.com/@devopslearning
- GitHub: https://github.com/100daysofdevops/100daysofdevops
- Slack: https://join.slack.com/t/100daysofdevops/shared_invite/enQtNzg1MjUzMzQzMzgxLWM4Yjk0ZWJiMjY4ZWE3ODBjZjgyYTllZmUxNzFkNTgxZjQ4NDlmZjkzODAwNDczOTYwOTM2MzlhZDNkM2FkMDA
- YouTube Channel: https://www.youtube.com/user/laprashant/videos?view_as=subscriber