Add retry mechanism to DeregisterImage from awsec2 (#8598)

This commit is contained in:
Sylvia Moss 2020-01-15 10:28:14 +01:00 committed by GitHub
parent 06a26e74b4
commit 0e177915f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 2 deletions

View File

@ -1,13 +1,16 @@
package common
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/common/retry"
)
// DestroyAMIs deregisters the AWS machine images in imageids from an active AWS account
@ -23,8 +26,19 @@ func DestroyAMIs(imageids []*string, ec2conn *ec2.EC2) error {
// Deregister image by name.
for _, i := range resp.Images {
_, err := ec2conn.DeregisterImage(&ec2.DeregisterImageInput{
ImageId: i.ImageId,
ctx := context.TODO()
err = retry.Config{
Tries: 11,
ShouldRetry: func(err error) bool {
return isAWSErr(err, "UnauthorizedOperation", "")
},
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
}.Run(ctx, func(ctx context.Context) error {
_, err := ec2conn.DeregisterImage(&ec2.DeregisterImageInput{
ImageId: i.ImageId,
})
return err
})
if err != nil {