Skip to content

Commit

Permalink
Merge pull request #9157 from res0nance/ebs-snapshot-timeout
Browse files Browse the repository at this point in the history
Add timeouts for create and delete and refactor to use isAWSErr
  • Loading branch information
bflad authored Jul 9, 2019
2 parents 5a6df11 + 688b5f1 commit b90dddd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
41 changes: 23 additions & 18 deletions aws/resource_aws_ebs_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"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/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -18,6 +17,11 @@ func resourceAwsEbsSnapshot() *schema.Resource {
Read: resourceAwsEbsSnapshotRead,
Delete: resourceAwsEbsSnapshotDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},

Schema: map[string]*schema.Schema{
"volume_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -95,7 +99,7 @@ func resourceAwsEbsSnapshotCreate(d *schema.ResourceData, meta interface{}) erro

d.SetId(*res.SnapshotId)

err = resourceAwsEbsSnapshotWaitForAvailable(d.Id(), conn)
err = resourceAwsEbsSnapshotWaitForAvailable(d, conn)
if err != nil {
return err
}
Expand Down Expand Up @@ -149,34 +153,35 @@ func resourceAwsEbsSnapshotRead(d *schema.ResourceData, meta interface{}) error
func resourceAwsEbsSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

return resource.Retry(5*time.Minute, func() *resource.RetryError {
return resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
request := &ec2.DeleteSnapshotInput{
SnapshotId: aws.String(d.Id()),
}
_, err := conn.DeleteSnapshot(request)
if err == nil {
return nil
}

ebsErr, ok := err.(awserr.Error)
if ebsErr.Code() == "SnapshotInUse" {
if isAWSErr(err, "SnapshotInUse", "") {
return resource.RetryableError(fmt.Errorf("EBS SnapshotInUse - trying again while it detaches"))
}

if !ok {
return resource.NonRetryableError(err)
}

return resource.NonRetryableError(err)
})
}

func resourceAwsEbsSnapshotWaitForAvailable(id string, conn *ec2.EC2) error {
log.Printf("Waiting for Snapshot %s to become available...", id)
func resourceAwsEbsSnapshotWaitForAvailable(d *schema.ResourceData, conn *ec2.EC2) error {
log.Printf("Waiting for Snapshot %s to become available...", d.Id())

req := &ec2.DescribeSnapshotsInput{
SnapshotIds: []*string{aws.String(id)},
}
err := conn.WaitUntilSnapshotCompleted(req)
return err
return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
req := &ec2.DescribeSnapshotsInput{
SnapshotIds: []*string{aws.String(d.Id())},
}
err := conn.WaitUntilSnapshotCompleted(req)
if err == nil {
return nil
}
if isAWSErr(err, "ResourceNotReady", "") {
return resource.RetryableError(fmt.Errorf("EBS CreatingSnapshot - waiting for snapshot to become available"))
}
return resource.NonRetryableError(err)
})
}
5 changes: 5 additions & 0 deletions aws/resource_aws_ebs_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ resource "aws_ebs_snapshot" "test" {
tags = {
Name = "%s"
}
timeouts {
create = "10m"
delete = "10m"
}
}
`, rName)
}
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/ebs_snapshot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ The following arguments are supported:
* `description` - (Optional) A description of what the snapshot is.
* `tags` - (Optional) A mapping of tags to assign to the snapshot

### Timeouts

`aws_ebs_snapshot` provides the following
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:

- `create` - (Default `10 minutes`) Used for creating the ebs snapshot
- `delete` - (Default `10 minutes`) Used for deleting the ebs snapshot

## Attributes Reference

Expand Down

0 comments on commit b90dddd

Please sign in to comment.