terraform-provider-aws: Applying aws_s3_bucket_policy and aws_s3_bucket_public_access_block at the same time may cause an error
Terraform Version
$ terraform -v
Terraform v0.11.11
+ provider.aws v1.59.0
Affected Resource(s)
- aws_s3_bucket
- aws_s3_bucket_policy
- aws_s3_bucket_public_access_block
Terraform Configuration
provider "aws" {
version = "= 1.59.0"
region = "us-east-1"
}
resource "aws_s3_bucket" "b" {
bucket = "minamijoyo-public-access-block-test"
}
resource "aws_s3_bucket_policy" "b" {
bucket = "${aws_s3_bucket.b.id}"
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "MYBUCKETPOLICY",
"Statement": [
{
"Sid": "IPAllow",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::minamijoyo-public-access-block-test/*",
"Condition": {
"IpAddress": {"aws:SourceIp": "127.0.0.1/32"}
}
}
]
}
POLICY
}
resource "aws_s3_bucket_public_access_block" "example" {
bucket = "${aws_s3_bucket.b.id}"
block_public_acls = true
block_public_policy = true
}
Debug Output
2392 2019-02-21T11:49:50.073+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: -----------------------------------------------------↲
2393 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: 2019/02/21 11:49:50 [DEBUG] [aws-sdk-go] DEBUG: Response s3/PutBucketPolicy Details:↲
2394 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: ---[ RESPONSE ]--------------------------------------↲
2395 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: HTTP/1.1 409 Conflict↲
2396 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: Connection: close↲
2397 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: Transfer-Encoding: chunked↲
2398 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: Content-Type: application/xml↲
2399 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: Date: Thu, 21 Feb 2019 02:49:49 GMT↲
2400 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: Server: AmazonS3↲
2401 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: X-Amz-Id-2: I2Fd71SFEnfx9m7SOjcCaF6G+ZdyDYMMk/3qzSk7ZhXZ9ERhAyVGzlKhtFYd3TRxwg5yHVVm+i0=↲
2402 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: X-Amz-Request-Id: B663B2CF1942B6E2↲
2403 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4:-↲
2404 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4:-↲
2405 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: -----------------------------------------------------↲
2406 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: 2019/02/21 11:49:50 [DEBUG] [aws-sdk-go] <?xml version="1.0" encoding="UTF-8"?>↲
2407 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: <Error><Code>OperationAborted</Code><Message>A conflicting conditional operation is currently in progress against this resource. Please try again.</ Message><RequestId>B663B2CF1942B6E2</RequestId><HostId>I2Fd71SFEnfx9m7SOjcCaF6G+ZdyDYMMk/3qzSk7ZhXZ9ERhAyVGzlKhtFYd3TRxwg5yHVVm+i0=</HostId></Error>↲
2408 2019-02-21T11:49:50.920+0900 [DEBUG] plugin.terraform-provider-aws_v1.59.0_x4: 2019/02/21 11:49:50 [DEBUG] [aws-sdk-go] DEBUG: Validate Response s3/PutBucketPolicy failed, not retrying, error OperationAborted: A conflicting conditional operation is currently in progress against this resource. Please try again.↲
Panic Output
No panic
Expected Behavior
No error
Actual Behavior
Got an error
$ terraform apply
Error: Error applying plan:
1 error(s) occurred:
* aws_s3_bucket_policy.b: 1 error(s) occurred:
* aws_s3_bucket_policy.b: Error putting S3 policy: OperationAborted: A conflicting conditional operation is currently in progress against this resource. Please try again.
status code: 409, request id: B663B2CF1942B6E2, host id: I2Fd71SFEnfx9m7SOjcCaF6G+ZdyDYMMk/3qzSk7ZhXZ9ERhAyVGzlKhtFYd3TRxwg5yHVVm+i0=
Steps to Reproduce
terraform apply.
Important Factoids
Success and failure depend on timing. I tried it a couple of times, but in my environment, there are fewer cases without the error.
References
Although the types of resources are different, calling S3 API in parallel to the same bucket may cause this error.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 84
- Comments: 26 (7 by maintainers)
Commits related to this issue
- workaround for error putting S3 policy https://github.com/terraform-providers/terraform-provider-aws/issues/7628#issuecomment-469825984 — committed to asannou/terraform-aws-cloudformation-stackset by asannou 4 years ago
- Serialise origin bucket modifications You can't modify an S3 bucket's policy & public access block at the same time, AWS API will complain: OperationAborted: A conflicting conditional operation is c... — committed to alexjurkiewicz/terraform-aws-cloudfront-s3-cdn by alexjurkiewicz 3 years ago
- fix: solves conflicting conditional operation Without this depency, it may cause the following error: │ Error: Error putting S3 policy: OperationAborted: A conflicting conditional operation is curren... — committed to 3scale-ops/tf-aws-s3-backup by raelga 3 years ago
- fix: solves conflicting conditional operation Without this depency, it may cause the following error: │ Error: Error putting S3 policy: OperationAborted: A conflicting conditional operation is curren... — committed to 3scale-ops/tf-aws-s3-backup by raelga 3 years ago
- fix: solves conflicting conditional operation Without this depency, it may cause the following error: Error putting S3 policy: OperationAborted: A conflicting conditional operation is currently in pr... — committed to 3scale-ops/tf-aws-s3-backup by raelga 3 years ago
- fix: solves conflicting conditional operation Without this depency, it may cause the following error: Error putting S3 policy: OperationAborted: A conflicting conditional operation is currently in pr... — committed to 3scale-ops/tf-aws-s3-backup by raelga 3 years ago
- fix: solves conflicting conditional operation Without this depency, it may cause the following error: Error putting S3 policy: OperationAborted: A conflicting conditional operation is currently in pr... — committed to 3scale-ops/tf-aws-s3-backup by raelga 3 years ago
- fix: solves conflicting conditional operation Without this depency, it may cause the following error: Error putting S3 policy: OperationAborted: A conflicting conditional operation is currently in pr... — committed to 3scale-ops/tf-aws-s3-backup by raelga 3 years ago
Using
depends_onis a good way to force the public access block and policy to be applied one-by-one instead of concurrently:This worked for me.
I think that
depends_onwould work as a workaround, but it does not actually depend on it, we should implement an appropriate error handling. Since similar problems can occur with many s3 related resources, I’m not sure how do we handle it is the best.We’ve been intermittently experiencing a variety of
OperationAborted: A conflicting conditional operation...errors when creating new S3 buckets. For example:In our case, we typically aren’t creating
aws_s3_bucket_policyresources and have not seen from triaging Terraform debug logs that parallel calls to the S3 service are being made for the same bucket.In each of the cases we’ve triaged, the S3 bucket can eventually be created and set with the desired configuration if enough retries are attempted. Generally, a single retry within a minute of the first failed try has been sufficient. In some cases, though, we’ve had wait for up to 45 minutes before a retry succeeds.
From descriptions I’ve seen of this error, it sounds like a common condition that would trigger this is recreating an S3 bucket which has just just been deleted or hitting a soft limit on the number of S3 buckets in an account. I don’t believe either has been true in our case, though, since the number of buckets have been well under our account limit around the time of some failures and the buckets we’re creating are most often unique.
In the implementation the
aws_s3_bucketin the Terraform AWS provider code, I’m not seeing code which would seem to be causing parallel S3 API calls for the same bucket to be made. We have seen at least occasionally that when Terraform makes an apparently successful Put call to an S3 API from theresourceAwsS3BucketUpdatefunction that the corresponding Get call made from theresourceAwsS3BucketReadfunction later might fail. For example, we saw aPutBucketLifecycleConfigurationcall return an HTTP 200 OK response but the subsequentGetBucketLifecycleConfigurationcall for the same bucket returned an HTTP 404 Not Found response. Perhaps this is due to eventual consistency issues with the AWS S3 service. The calls made from withinresourceAwsS3BucketUpdategenerally don’t appear to follow a Put with a Get to confirm that the S3 service has committed the desired configuration. Where multiple Put calls could be strung together (e.g., when encryption, lifecycle, and/or other features are all enabled together for the same S3 service), I wonder if adding Gets for each of the Puts would help serialize the configuration changes in AWS (and, therefore, help avoid “conflicting conditional operations”). It’s hard to tell if this would help, though, since it’s not really clear from the AWS error messaging what the “conflicting conditional operations” are in the event of a failure.For the S3 server side encryption configuration one, we had put up a PR, which would add retries for 409 errors, although that PR has been up for over 10 months now without any attention. Perhaps it would make sense to add similar retries for each of the S3 API calls that the AWS provider could make. Unfortunately, though, the retry timeout might need to be very high (45 minutes or longer?) in order to reliably overcome the errors. It would be nice to figure out the sequences of events from interactions with the AWS S3 API that produce these errors so that we could avoid them (either in Terraform code or, at least where possible, within the Terraform AWS provider).
This is also experienced with the
aws_s3_bucket_ownership_controlsresource. I think the eventual consistency is the more likely issue and TF should just backoff and retry on this error message.It seems this is also an issue when managing aws_s3_bucket_notification resources.
This should be resolved with #12949. However, since this issue is tricky to test, please open a new issue (and reference this issue) if you continue to see problems!
Some sort ordering seems needed, a related issue is that if you have AWS Guardduty enabled and you terraform destroy an s3 bucket you will get a security alert that the “Block public access policies” have been removed because they are removed before the bucket is removed rather than just destroying the bucket first.
seeing this on terraform
1.0.4and AWS provider0.57.0as wellThe same with
aws_s3_bucket_public_access_blockandaws_s3_bucket_ownership_controlsconfigured for the bucket. I overcome this by makingaws_s3_bucket_ownership_controlsexplicitdepends_onaws_s3_bucket_public_access_blockHi all 👋 Just letting you know that this is issue is featured on this quarters roadmap. If a PR exists to close the issue a maintainer will review and either make changes directly, or work with the original author to get the contribution merged. If you have written a PR to resolve the issue please ensure the “Allow edits from maintainers” box is checked. Thanks for your patience and we are looking forward to getting this merged soon!
Either that or sort out planned API calls to S3 so that they happen in sequence.
IMO documenting the use of
depends_onto serialize creation would be sufficient.