Day 53 : AWS S3 Bucket Creation and Management

Day 53 : AWS S3 Bucket Creation and Management

Amazon Web Services (AWS) provides a wide range of cloud services, among which Amazon S3 (Simple Storage Service) stands out as a foundational pillar for storing and managing data. In this guide, we'll delve into the process of creating and managing S3 buckets, exploring essential tasks such as setting up access permissions, creating policies, and enabling versioning.

What is an S3 Bucket?

An S3 bucket is essentially a container for storing objects, which can range from text files to images, videos, and any other type of digital data. These buckets are organized within AWS regions and are designed to offer scalability, high availability, and robust security features.

Task

Create an S3 bucket using Terraform.

Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to define and manage AWS resources in a declarative manner. Let's walk through the steps of creating an S3 bucket using Terraform.

provider "aws" {
  region = "ap-south-1"
}

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-day67-bucket"
}

Configure the bucket to allow public read access.

resource "aws_s3_bucket_acl" "bucket_acl" {
  bucket = aws_s3_bucket.my_bucket.id
  acl    = "public-read"
}

To allow public read access to the S3 bucket, the code creates an ACL (access control list) resource using the "aws_s3_bucket_acl" resource type. The resource is associated with the S3 bucket resource "aws_s3_bucket.my_bucket" using the "bucket" parameter. The "ACL" parameter is set to "public-read", which allows public read access to the bucket.

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

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 = ["683633011377"]
    }

    actions = [
      "s3:GetObject",
      "s3:ListBucket",
    ]

    resources = [
      aws_s3_bucket.my_bucket.arn,
      "${aws_s3_bucket.my_bucket.arn}/*",
    ]
  }

To provide read-only access to a specific IAM user or role, the code creates an S3 bucket policy resource using the "aws_s3_bucket_policy" resource type. The resource is associated with the S3 bucket resource "aws_s3_bucket.my_bucket" using the "bucket" parameter. The "policy" parameter is set to the Terraform data source "data.aws_iam_policy_document.allow_read_only_access.json", which defines the policy document.

The policy document is created using the "data" block, which creates a Terraform data source.

The data source "aws_iam_policy_document.allow_read_only_access" defines a policy document that allows read-only access to the S3 bucket for a specific IAM user or role. The policy document is specified using JSON syntax.

The policy document has a single "statement" block, which defines the permissions to grant. The statement grants the "s3:GetObject" and "s3:ListBucket" permissions for the specified bucket and bucket objects. The "principals" block specifies the AWS user or role to which the permissions are granted. In this case, the "identifiers" field specifies the AWS account ID of the user or role to which read-only access is granted.

S3 bucket policy is created that allows read-only access to a specific IAM user.

Conclusion

Amazon S3 offers a robust and scalable solution for storing and managing data in the cloud. By leveraging tools like Terraform, configuring access permissions, creating bucket policies, and enabling versioning, you can effectively harness the power of S3 buckets for your various use cases. Whether it's hosting static websites, sharing files publicly, or maintaining data integrity, AWS S3 provides the tools you need to succeed in the cloud storage realm.

I'm confident that this article will prove to be valuable, helping you discover new insights and learn something enriching.

Thank you :)