Day 67: AWS S3 Bucket Creation and Management

Day 67: AWS S3 Bucket Creation and Management

ยท

2 min read

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, you will learn how to create and manage S3 buckets in AWS.

Task

Create an S3 bucket using Terraform.

  • Create a file called aws-s3.tf . To create S3 bucket define the following resource block in your terraform configuration file.
resource "aws_s3_bucket" "my_bucket" {
  bucket = "day77-s3-bucket"
}

Configure the bucket to allow public read access.

  • Create a file called aws-public-access.tf . To configure public access add the following resource block.
# Allow public read acces
resource "aws_s3_bucket_public_access_block" "public_access_block" {
  bucket = aws_s3_bucket.my_bucket.id

  block_public_acls       = false
  block_public_policy     = false
  ignore_public_acls      = false
  restrict_public_buckets = false
}

Create an S3 bucket policy that allows read-only access to a specific IAM user or role.

  • Create a file called aws-s3-iam-user.tf . To allow read-only access to an IAM user or role modify the existing bucket policy resource block as follows:
# Bucket policy to allow read-only access to the devops-user
resource "aws_s3_bucket_policy" "my_bucket_policy" {
  bucket = aws_s3_bucket.my_bucket.id

  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Sid    = "AllowUserAccess"
        Effect = "Allow"
        Principal = {
          AWS = "arn:aws:iam::974262444728:user/iamadmin"
        }
        Action   = "s3:GetObject"
        Resource = "${aws_s3_bucket.my_bucket.arn}/*"
      }
    ]
  })
}

Enable versioning on the S3 bucket.

  • Create a file called aws-s3-versioning.tf . To enable this version add this following resource block.
# Enable versioning for the S3 bucket
resource "aws_s3_bucket_versioning" "bucket_versioning" {
  bucket = aws_s3_bucket.my_bucket.id
  versioning_configuration {
    status = "Enabled"
  }
}

Execute Terraform

  • Run terraform init , terraform apply to create above configuration.

Validate the infrastructure

  • Now navigate to s3 bucket and see your s3 bucket will create with you allowd your configuration.

By following these steps, you will be able to create and manage S3 buckets in AWS using Terraform. Take advantage of the flexibility and scalability offered by S3 to meet your storage needs effectively.


Happy Learning

Thanks For Reading! :)

-SriParthu๐Ÿ’๐Ÿ’ฅ

ย