Day 67: AWS S3 Bucket Creation and Management using terraform

Day 67: AWS S3 Bucket Creation and Management using terraform

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

1. Create an S3 bucket using Terraform.

resource "aws_s3_bucket" "my_bucket" {
? bucket = "day67taskbucket0304"
}        
No alt text provided for this image

The aws_s3_bucket resource creates a new S3 bucket.

my_bucket is a unique identifier for this resource that can be used in other parts of your Terraform code. You can use a different name for this identifier if you prefer.

Run the?terraform init?command to initialize the working directory and download the required providers.

No alt text provided for this image

It will create an execution plan by analyzing the changes required to achieve the desired state of your infrastructure with?terraform plan

No alt text provided for this image

Finally, it will apply the changes to create or update resources as needed with?terraform apply.

No alt text provided for this image

S3 bucket successfully created.

No alt text provided for this image


2. 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.

No alt text provided for this image

Run terraform apply

No alt text provided for this image

Bucket is publicly accessible.

No alt text provided for this image


3. Enable versioning on the S3 bucket.

No alt text provided for this image

The versioning block is included, with enabled set to true. This enables versioning on the S3 bucket, which will keep multiple versions of each object stored in the bucket.

Bucket Versioning is Enabled.

No alt text provided for this image


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.

No alt text provided for this image

Run terraform apply

No alt text provided for this image
No alt text provided for this image

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

No alt text provided for this image


Thank you for reading!

Maksym Voitko

AI | Data Engineering & Back End & MLOps | Python, Big Data, AWS, GCP | Angel Investor

1 年

Thank you for the great article! What do you think about wrapping the code into your own module or using the aws s3 terraform module?

回复

要查看或添加评论,请登录

Sayali Shewale的更多文章

  • DevOps Project-3

    DevOps Project-3

    Project Description The project involves hosting a static website using an AWS S3 bucket. Amazon S3 is an object…

    7 条评论
  • DevOps Project-2

    DevOps Project-2

    Project Description The project is about automating the deployment process of a web application using Jenkins and its…

    2 条评论
  • Day 80: DevOps Project-1

    Day 80: DevOps Project-1

    Project Description The project aims to automate the building, testing, and deployment process of a web application…

    4 条评论
  • Day 73 - Setup Grafana on AWS EC2 Instance

    Day 73 - Setup Grafana on AWS EC2 Instance

    Task: Setup grafana in your local environment on AWS EC2. Go to the AWS console and Launch an EC2 instance Open port…

    2 条评论
  • Day 72 - Grafana

    Day 72 - Grafana

    What is Grafana? Grafana is an open-source data visualization and monitoring tool that allows you to query, visualize…

    3 条评论
  • Day 70 - Terraform Modules

    Day 70 - Terraform Modules

    Modules are containers for multiple resources that are used together. A module consists of a collection of .

    2 条评论
  • Day 69 - Meta-Arguments in Terraform

    Day 69 - Meta-Arguments in Terraform

    When you define a resource block in Terraform, by default, this specifies one resource that will be created. To manage…

    1 条评论
  • Day 68 - Scaling with Terraform

    Day 68 - Scaling with Terraform

    Understanding Scaling Scaling is the process of adding or removing resources to match the changing demands of your…

  • Day 66 - Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC)

    Day 66 - Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC)

    Task: 1.Create a VPC (Virtual Private Cloud) with CIDR block 10.

    3 条评论
  • Day 65 - Working with Terraform Resources

    Day 65 - Working with Terraform Resources

    Understanding Terraform Resources A resource in Terraform represents a component of your infrastructure, such as a…

    4 条评论

社区洞察

其他会员也浏览了