๐ถ AWS S3 Bucket
Amazon S3 (Simple Storage Service) is an object storage service that offers industry-leading scalability, data availability, security, and performance. It can be used for a variety of use cases, such as storing and retrieving data, hosting static websites, and more.
In this task, we will learn how to create and manage S3 buckets in AWS.
๐ถ Task:
Create an S3 bucket using Terraform.
resource "aws_s3_bucket" "my_bucket" { bucket = "my-terra-s3-bucket" }
Enable ACL for your S3 bucket and choose 'Bucket Owner Preferred.
Configure the bucket to allow public read access.
```plaintext resource "aws_s3_bucket_policy" "bucket_policy" { bucket = aws_s3_bucket.my_bucket.id policy = data.aws_iam_policy_document.allow_read_only_access.json }
data "aws_iam_policy_document" "allow_read_only_access" { statement { principals { type = "AWS" identifiers = ["130407889756"] # use your aws account id }
actions = [ "s3:GetObject", "s3:ListBucket", ]
resources = [ aws_s3_bucket.my_bucket.arn, "${aws_s3_bucket.my_bucket.arn}/*", ] } }
* Now use 'terraform apply' to execute the above file.
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696512478038/c90580b1-77f4-4a0a-af1f-0a57a46dcf04.png align="center")
* Check the bucket public access is now enabled.
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696511037015/b83a42e6-45f4-4003-9ae1-0771daa39876.png align="center")
* Create an S3 bucket policy that allows read-only access to a specific IAM user or role.
```yaml
resource "aws_s3_bucket_public_access_block" "example" {
bucket = aws_s3_bucket.my_bucket.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_bucket_acl" "bucket_acl" {
bucket = aws_s3_bucket.my_bucket.id
acl = "public-read"
}
Now use 'terraform apply' to execute the above file.
Check Bucket Policy:
Enable versioning on the S3 bucket.
#S3.tf file resource "aws_s3_bucket" "my_bucket" { bucket = "my-terra-s3-bucket-day67" versioning { enabled = true } }
Now apply terraform apply to execute the above changes:
Now check the S3 bucket to verify versioning.
๐ถ Conclusion:
In conclusion, this blog has walked you through the essential steps of creating and managing an AWS S3 bucket using Terraform. We started by defining the S3 bucket configuration in the S3.tf
file, enabling versioning to enhance data protection and retrieval capabilities.
By executing terraform apply
, you brought your infrastructure to life, creating the S3 bucket as specified. The final step, a quick verification of versioning settings, ensures that your bucket is configured as intended.
I hope this blog has been a valuable resource in your journey to mastering cloud technologies.
Happy Learning :)
Stay in the loop with my latest insights and articles on cloud โ๏ธ and DevOps โพ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).
Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.
#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes #Terraform #AWS