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.

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

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.

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
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Thanks For Reading!!!!

Sunil Kumar

Senior Cloud Engineer - Nagarro | Google Cloud Certified {"CDI, ACE , PCA, PDE}|Cloud DevOps Specialist | 5x GCP , Azure Certified | Devops | Linux | Docker | Terraform | Jenkins CI/CD | Kubernetes

1 年

Wow day 67??

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

Anup D Ghattikar的更多文章

  • Django Rest Framework

    Django Rest Framework

    Introduction to Django Rest Framework Django REST framework (DRF) is a powerful and elegant toolkit built on top of the…

    11 条评论
  • Docker Tool Mastering

    Docker Tool Mastering

    What is Docker? Docker is a platform and toolset that simplifies the process of developing, deploying, and running…

    4 条评论
  • Devop's Bits & Byte's - By Anup Ghattikar

    Devop's Bits & Byte's - By Anup Ghattikar

    Medium Link: https://medium.com/@aghattikar82/devops-bits-bytes-by-anup-ghattikar-9a8c6b788ec9 HashNode Link:…

    10 条评论
  • Kubernetes Troubleshooting

    Kubernetes Troubleshooting

    Cluster Management Display endpoint information about the master and services in the cluster kubectl cluster-info…

    3 条评论
  • Kubernetes Cluster Maintenance

    Kubernetes Cluster Maintenance

    Kubernetes Cluster Upgrade Upgrade master Upgrading the control plane consist of the following steps: Upgrade kubeadm…

    2 条评论
  • Day 05:Kubernetes Storage Kubernetes Security

    Day 05:Kubernetes Storage Kubernetes Security

    Persistent Volumes Managing storage is a distinct problem from managing compute instances. The PersistentVolume…

  • Kubernetes services and service discovery

    Kubernetes services and service discovery

    Kubernetes is a powerful platform for deploying, scaling, and managing containerized applications. However, once you…

  • Day 03:Kubernetes Workloads

    Day 03:Kubernetes Workloads

    Kubernetes Deployment with YAML YAML (which stands for YAML Ain’t Markup Language) is a language used to provide…

    2 条评论
  • Kubernetes Networking

    Kubernetes Networking

    Topics Are: Services, Ingress, Network Policies, DNS, CNI Plugins Services Services provide a way to expose a set of…

    7 条评论
  • 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 条评论

社区洞察

其他会员也浏览了