Ask Your Question
1

How to use Terraform to make an S3 Bucket accessible to the public?

asked 2023-05-13 18:27:29 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-13 18:52:01 +0000

bukephalos gravatar image

To make an S3 Bucket accessible to the public using Terraform, you can add the following configuration to your Terraform code:

  1. Add a bucket resource:
resource "aws_s3_bucket" "example_bucket" {
  bucket = "example-bucket"
  acl    = "public-read"
}

  1. Add a bucket policy:
resource "aws_s3_bucket_policy" "example_policy" {
  bucket = aws_s3_bucket.example_bucket.id

  policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": "*",
      "Action": [
        "s3:GetObject",
        "s3:ListBucket"
      ],
      "Resource": [
        "${aws_s3_bucket.example_bucket.arn}",
        "${aws_s3_bucket.example_bucket.arn}/*"
      ]
    }
  ]
}
EOF
}

The above configuration will allow any user to read objects from and list the contents of the bucket. Note that making a bucket publicly accessible in this manner may present security risks, so you should review the possible consequences before implementing this approach.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-13 18:27:29 +0000

Seen: 17 times

Last updated: May 13 '23