Skip to content

Commit

Permalink
Merge pull request #24537 from iandrewt/b-cloudfront-preconditionfailed
Browse files Browse the repository at this point in the history
b/fix PreconditionFailed errors on aws_cloudfront_distribution
  • Loading branch information
ewbankkit authored May 5, 2022
2 parents 1fb8bc0 + 41b231d commit 9bb800f
Show file tree
Hide file tree
Showing 3 changed files with 500 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/24537.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_cloudfront_distribution: Fix PreconditionFailed errors when other CloudFront resources are changed before the distribution
```
23 changes: 23 additions & 0 deletions internal/service/cloudfront/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,29 @@ func resourceDistributionUpdate(d *schema.ResourceData, meta interface{}) error
return nil
})

// Refresh our ETag if it is out of date and attempt update again
if tfawserr.ErrCodeEquals(err, cloudfront.ErrCodePreconditionFailed) {
getDistributionInput := &cloudfront.GetDistributionInput{
Id: aws.String(d.Id()),
}
var getDistributionOutput *cloudfront.GetDistributionOutput

log.Printf("[DEBUG] Refreshing CloudFront Distribution (%s) ETag", d.Id())
getDistributionOutput, err = conn.GetDistribution(getDistributionInput)

if err != nil {
return fmt.Errorf("error refreshing CloudFront Distribution (%s) ETag: %s", d.Id(), err)
}

if getDistributionOutput == nil {
return fmt.Errorf("error refreshing CloudFront Distribution (%s) ETag: empty response", d.Id())
}

params.IfMatch = getDistributionOutput.ETag

_, err = conn.UpdateDistribution(params)
}

// Propagate AWS Go SDK retried error, if any
if tfresource.TimedOut(err) {
_, err = conn.UpdateDistribution(params)
Expand Down
Loading

0 comments on commit 9bb800f

Please sign in to comment.