Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws_s3_bucket_public_access_block fails to create while terraform apply

i am going my first steps in Terraform for AWS and i want to create an S3 bucket and set "block all public access" to ON.

Versions:

Terraform v0.12.24 + provider.aws v2.60.0

file provider.tf

provider "aws" {
    region  = "eu-west-1"
    profile = "<myprofile>"
}

file s3.tf

resource "aws_s3_bucket" "<myname>" {
    bucket = "<myname>"
    region  = "eu-west-1"
}

resource "aws_s3_bucket_public_access_block" "<myname>" {
    bucket = "aws_s3_bucket.<myname>.id"
    block_public_acls = true
    block_public_policy = true
    ignore_public_acls = true
    restrict_public_buckets = true
}

the values in <> are my own values and if its the same value on my file, i used the same example value here (so in the s3.tf file its just one name for all the variables)

if i make a

terraform apply

it will create everything, but at the "aws_s3_bucket_public_access_block" it will go into a timeout after like 1 minute and tell me:

Error: error creating public access block policy for S3 bucket (aws_s3_bucket.<myname>.id): NoSuchBucket: The specified bucket does not exist
        status code: 404

i also tried to add

depends_on = [ aws_s3_bucket.<myname> ]

in the "aws_s3_bucket_public_access_block" but it doesnt work either.

I searched and tried and searched but nothing really works. Someone reported an error about it and told i must set the AWS_REGION globally in an .env file, tried it and didnt work either.

like image 972
Joshi Avatar asked May 07 '20 14:05

Joshi


People also ask

How do I block public access to S3 bucket with Terraform?

To control the access of the S3 bucket you need to use the aws_s3_bucket_public_access_block resource in your Terraform code as shown below.

What is aws_s3_bucket_public_access_block?

Resource: aws_s3_bucket_public_access_block. Manages S3 bucket-level Public Access Block configuration. For more information about these settings, see the AWS S3 Block Public Access documentation.

How do I create a multiple S3 bucket in Terraform?

The bucket names are mentioned in the default key. And then count , Will calculate the number of buckets we need to create from the s3_bucket_name variable. Run terraform plan to verify the script and then run terraform apply to create multiple S3 buckets as per your requirement.

How to use Terraform with AWS S3 bucket?

2. $ terraform apply – Run the Terraform apply command and you should be able to upload the files to the S3 bucket. There are many more things that you can do with Terraform and the S3 Bucket. Here is a guide on how to rename an AWS S3 bucket in Terraform which can help you rename your S3 bucket. 4.

How do I block public access to my Amazon S3 bucket?

By default, new buckets, access points, and objects do not allow public access. For more information, see Blocking public access to your Amazon S3 storage . You can use the S3 console, AWS CLI, AWS SDKs, and REST API to configure block public access settings for your bucket.

Can multiple AWS accounts have the same S3 public access block?

For more information about these settings, see the AWS S3 Block Public Access documentation. Each AWS account may only have one S3 Public Access Block configuration. Multiple configurations of the resource against the same AWS account will cause a perpetual difference.

What are the S3 bucket permissions?

In the Access column, Amazon S3 labels the permissions for a bucket as follows: Public – Everyone has access to one or more of the following: List objects, Write objects, Read and write permissions. Objects can be public – The bucket is not public, but anyone with the appropriate permissions can grant public access to objects.


Video Answer


1 Answers

Your reference to S3 bucket is incorrect.

Change:

bucket = "aws_s3_bucket.<myname>.id"

To:

bucket = aws_s3_bucket.<myname>.id

No double quotes since you are referencing another resource

like image 137
marcincuber Avatar answered Dec 31 '22 03:12

marcincuber