diff --git a/aws/resource_aws_dynamodb_table.go b/aws/resource_aws_dynamodb_table.go index 50cf27a08c1..1aa608abd6a 100644 --- a/aws/resource_aws_dynamodb_table.go +++ b/aws/resource_aws_dynamodb_table.go @@ -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 @@ -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 := "" diff --git a/aws/resource_aws_dynamodb_table_test.go b/aws/resource_aws_dynamodb_table_test.go index 2c0723d6cdf..f50a7193cb9 100644 --- a/aws/resource_aws_dynamodb_table_test.go +++ b/aws/resource_aws_dynamodb_table_test.go @@ -6,7 +6,6 @@ import ( "regexp" "strings" "testing" - "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" @@ -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