Skip to content

Commit

Permalink
Use a retry as the EC2 API is eventually consistent.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Stump committed Jul 9, 2019
1 parent 7ed006b commit 937520c
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions aws/resource_aws_ec2_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package aws
import (
"fmt"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down Expand Up @@ -78,22 +80,34 @@ func resourceAwsEc2TagRead(d *schema.ResourceData, meta interface{}) error {
}

key := d.Get("key").(string)

tags, err := conn.DescribeTags(&ec2.DescribeTagsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("resource-id"),
Values: []*string{aws.String(id)},
var tags *ec2.DescribeTagsOutput

// The EC2 API is eventually consistent. This means that writing a tag
// followed by an immediate describe call can sometimes fail. To address
// this we retry for a couple of minutes before failing.
retryError := resource.Retry(2*time.Minute, func() *resource.RetryError {
tags, err = conn.DescribeTags(&ec2.DescribeTagsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("resource-id"),
Values: []*string{aws.String(id)},
},
{
Name: aws.String("key"),
Values: []*string{aws.String(key)},
},
},
{
Name: aws.String("key"),
Values: []*string{aws.String(key)},
},
},
})

if err != nil {
return resource.RetryableError(err)
}

return nil
})

if err != nil {
return err
if retryError != nil {
return fmt.Errorf("[ERROR] Tag %s not found on resource %s", key, id)
}

if len(tags.Tags) != 1 {
Expand Down

0 comments on commit 937520c

Please sign in to comment.