diff --git a/builtin/providers/aws/resource_aws_cloudformation_stack.go b/builtin/providers/aws/resource_aws_cloudformation_stack.go index 1846a3105..3486c6a5b 100644 --- a/builtin/providers/aws/resource_aws_cloudformation_stack.go +++ b/builtin/providers/aws/resource_aws_cloudformation_stack.go @@ -190,8 +190,18 @@ func resourceAwsCloudFormationStackRead(d *schema.ResourceData, meta interface{} stacks := resp.Stacks if len(stacks) < 1 { + log.Printf("[DEBUG] Removing CloudFormation stack %s as it's already gone", d.Id()) + d.SetId("") return nil } + for _, s := range stacks { + if *s.StackId == d.Id() && *s.StackStatus == "DELETE_COMPLETE" { + log.Printf("[DEBUG] Removing CloudFormation stack %s"+ + " as it has been already deleted", d.Id()) + d.SetId("") + return nil + } + } tInput := cloudformation.GetTemplateInput{ StackName: aws.String(stackName), diff --git a/builtin/providers/aws/resource_aws_cloudformation_stack_test.go b/builtin/providers/aws/resource_aws_cloudformation_stack_test.go index 0c99f8d54..31b816d1a 100644 --- a/builtin/providers/aws/resource_aws_cloudformation_stack_test.go +++ b/builtin/providers/aws/resource_aws_cloudformation_stack_test.go @@ -101,9 +101,12 @@ func testAccCheckAWSCloudFormationDestroy(s *terraform.State) error { resp, err := conn.DescribeStacks(¶ms) - if err == nil { - if len(resp.Stacks) != 0 && - *resp.Stacks[0].StackId == rs.Primary.ID { + if err != nil { + return err + } + + for _, s := range resp.Stacks { + if *s.StackId == rs.Primary.ID && *s.StackStatus != "DELETE_COMPLETE" { return fmt.Errorf("CloudFormation stack still exists: %q", rs.Primary.ID) } }