Skip to content

Commit

Permalink
Merge pull request #3355 from terraform-providers/b-aws_dynamodb_tabl…
Browse files Browse the repository at this point in the history
…e-delete-table

resource/aws_dynamodb_table: Retry on deletion ResourceInUseException and refactor
  • Loading branch information
bflad authored Feb 13, 2018
2 parents 49ea590 + a2b1390 commit eb650b0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
33 changes: 30 additions & 3 deletions aws/resource_aws_dynamodb_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,7 @@ func resourceAwsDynamoDbTableDelete(d *schema.ResourceData, meta interface{}) er

log.Printf("[DEBUG] DynamoDB delete table: %s", d.Id())

_, err := conn.DeleteTable(&dynamodb.DeleteTableInput{
TableName: aws.String(d.Id()),
})
err := deleteAwsDynamoDbTable(d.Id(), conn)
if err != nil {
if isAWSErr(err, dynamodb.ErrCodeResourceNotFoundException, "Requested resource not found: Table: ") {
return nil
Expand Down Expand Up @@ -477,6 +475,35 @@ func resourceAwsDynamoDbTableDelete(d *schema.ResourceData, meta interface{}) er
return err
}

func deleteAwsDynamoDbTable(tableName string, conn *dynamodb.DynamoDB) error {
input := &dynamodb.DeleteTableInput{
TableName: aws.String(tableName),
}

return resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteTable(input)
if err != nil {
// Subscriber limit exceeded: Only 10 tables can be created, updated, or deleted simultaneously
if isAWSErr(err, dynamodb.ErrCodeLimitExceededException, "simultaneously") {
return resource.RetryableError(err)
}
// This handles multiple scenarios in the DynamoDB API:
// 1. Updating a table immediately before deletion may return:
// ResourceInUseException: Attempt to change a resource which is still in use: Table is being updated:
// 2. Removing a table from a DynamoDB global table may return:
// ResourceInUseException: Attempt to change a resource which is still in use: Table is being deleted:
if isAWSErr(err, dynamodb.ErrCodeResourceInUseException, "") {
return resource.RetryableError(err)
}
if isAWSErr(err, dynamodb.ErrCodeResourceNotFoundException, "Requested resource not found: Table: ") {
return resource.NonRetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
}

func updateDynamoDbTimeToLive(d *schema.ResourceData, conn *dynamodb.DynamoDB) error {
toBeEnabled := false
attributeName := ""
Expand Down
16 changes: 1 addition & 15 deletions aws/resource_aws_dynamodb_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"regexp"
"strings"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand Down Expand Up @@ -46,20 +45,7 @@ func testSweepDynamoDbTables(region string) error {
}
log.Printf("[INFO] Deleting DynamoDB Table: %s", *tableName)

input := &dynamodb.DeleteTableInput{
TableName: tableName,
}
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteTable(input)
if err != nil {
// Subscriber limit exceeded: Only 10 tables can be created, updated, or deleted simultaneously
if isAWSErr(err, dynamodb.ErrCodeLimitExceededException, "simultaneously") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
err := deleteAwsDynamoDbTable(*tableName, conn)
if err != nil {
log.Printf("[ERROR] Failed to delete DynamoDB Table %s: %s", *tableName, err)
continue
Expand Down

0 comments on commit eb650b0

Please sign in to comment.