Open pivotal-marcela-campo opened 2 years ago
I was able to resolve this by setting the bucket ACL to private and clearing additional ACLs which had been set. I definitely think this experience could be smoother but I wonder whether that might be tricky to do better than by extending the documentation.
I was able to resolve this by setting the bucket ACL to private and clearing additional ACLs which had been set.
Confirming that this works on a bucket-by-bucket basis, but not an ideal solution for orgs managing a lot of different buckets.
Agree with @devbyaccident here, manual intervention is not possible for our use case.
Perhaps we could add an argument to the aws_s3_bucket_ownership_controls
resource such as force_destroy_acls
that replaces any existing ACLs for the bucket with the private canned ACL before trying to set the object ownership to BucketOwnerEnforced
?
I ran into this issue when trying to write a child module for an S3 bucket. It included a variable enable_cloudfront_logging
which would create the required ACL for allowing a CloudFront distribution to log to the bucket. For the best user experience, I wanted it so that toggling the variable either way and applying the Terraform just worked, with no manual steps required.
I thought I'd found a crude solution by having two aws_s3_bucket_acl
resources, one with the private canned ACL and one with the CloudFront ACL. Using depends_on
and count
, I'd set it up so that the private canned ACL would be applied before setting the object ownership to BucketOwnerEnforced
, and setting the object ownership to BucketOwnerPreferred
would happen before applying the CloudFront ACL. However, I realised this doesn't work when creating a new bucket that doesn't have CloudFront logging enabled, as by default the new bucket will already have the BucketOwnerEnforced
setting, so the private canned ACL can't be applied.
It seems that the problem is that the S3 API is very particular about the ordering of these changes. However, implementing this ordering effectively in Terraform is a headache, if even possible. Having a way to overwrite the ACLs from the aws_s3_bucket_ownership_controls
resource would make everything simpler, and I think there's some precedent here considering that the aws_s3_bucket
resource has the force_destroy
argument that first deletes all the objects in the bucket.
Just another datapoint: The official Associate Tutorial from Hashicorp has the same issue.
Also faced the same issue today, Tried to disable s3 acl by changing the bucket owner to BucketOwnerEnforced
for bucket which already had s3-log-delivery READ and WRITE access and got below error:
Error: creating S3 Bucket (<bucket-name> Ownership Controls: InvalidBucketAclWithObjectOwnership: Bucket cannot have ACLs set with ObjectOwnership's BucketOwnerEnforced setting
│
│ with module.s3_logging[0].aws_s3_bucket_ownership_controls.change-ownership-s3-disable-acl[0],
│ on .terraform/modules/s3_logging/s3/main.tf line 175, in resource "aws_s3_bucket_ownership_controls" "change-ownership-s3-disable-acl":
│ 175: resource "aws_s3_bucket_ownership_controls" "change-ownership-s3-disable-acl" {
│
I was able to resolve this by setting the bucket ACL to private and clearing additional ACLs which had been set. I definitely think this experience could be smoother but I wonder whether that might be tricky to do better than by extending the documentation.
@acdha Did you did the above change manually to fix it?
@prashant0085 I had a similar requirement of transitioning S3 access logging buckets to use bucket policy and have ACLs disabled (BucketOwnerEnforced
). I can do it via Terraform, but it's a multi-step process, which is not scalable:
private
and leave bucket ownership unchanged. (Ensure you also apply the required bucket policy configuration, otherwise access logging stops)null
(or remove it from code) and set bucket ownership to BucketOwnerEnforced
.I figure a lot of the problem we're having here is the "feature" of the provider where it doesn't actually modify the ACL when you delete the resource. It just deletes it from state: From the provider docs
Even if the provider just "reset" the ACL to a base/default version it would be more useful, and may resolve this issue.
I am encountering this problem when creating buckets using http://github.com/samstav/terraform-aws-backend
Error: creating S3 Bucket (foo-tf-state-logs): operation error S3: CreateBucket, https response error StatusCode: 400, RequestID: XKSF4PSZPC0TFSRK, HostID: RwFULMMW2Lvj/CXLARoI9m1YZup/gYgM6+SToJn+T/3zIaWxpHGTYfVIomoJRY+XMMoYmPDa6VfZgFyezUvQBA==, api error InvalidBucketAclWithObjectOwnership: Bucket cannot have ACLs set with ObjectOwnership's BucketOwnerEnforced setting
resource "aws_s3_bucket" "tf_backend_logs_bucket" {
bucket = "${var.backend_bucket}-logs"
acl = "log-delivery-write"
versioning {
enabled = true
}
}
Any update on this?
Community Note
Terraform CLI and Terraform AWS Provider Version
Terraform v1.2.4 on darwin_amd64
Affected Resource(s)
Terraform Configuration Files
Please include all Terraform configurations required to reproduce the bug. Bug reports without a functional reproduction may be closed without investigation.
Debug Output
https://gist.github.com/pivotal-marcela-campo/84feafdef53f9cb8a92f2434b9b20816
Panic Output
Expected Behavior
ACLs should have been disabled with ownership set to BucketOwnerEnforced. Plan shows this is what will happen (1 resource deletion and 1 modification in place) but it is not the end state.
Actual Behavior
Got error
output of
tf plan
looks fineSteps to Reproduce
terraform apply
with acl=public-read and ownership=ObjectWriterterraform apply
with acl=. (no acl) and ownership=BucketOwnerEnforcedI also tried setting acl to either
private
or null in the resource instead of using count, but the end result is the same.It also breaks in the same fashion when trying to enable ACLs on a bucket that has them disabled.
Important Factoids
References
0000