diff --git a/aws/resource_aws_vpc_peering_connection.go b/aws/resource_aws_vpc_peering_connection.go index 2d2c60c9ec7..fe8a38635df 100644 --- a/aws/resource_aws_vpc_peering_connection.go +++ b/aws/resource_aws_vpc_peering_connection.go @@ -277,31 +277,18 @@ func resourceAwsVPCPeeringDelete(d *schema.ResourceData, meta interface{}) error req := &ec2.DeleteVpcPeeringConnectionInput{ VpcPeeringConnectionId: aws.String(d.Id()), } - log.Printf("[DEBUG] Deleting VPC Peering Connection: %s", req) + _, err := conn.DeleteVpcPeeringConnection(req) + + if isAWSErr(err, "InvalidVpcPeeringConnectionID.NotFound", "") { + return nil + } + if err != nil { - if isAWSErr(err, "InvalidVpcPeeringConnectionID.NotFound", "") { - return nil - } return fmt.Errorf("Error deleting VPC Peering Connection (%s): %s", d.Id(), err) } - // Wait for the vpc peering connection to delete - log.Printf("[DEBUG] Waiting for VPC Peering Connection (%s) to delete.", d.Id()) - stateConf := &resource.StateChangeConf{ - Pending: []string{ - ec2.VpcPeeringConnectionStateReasonCodeActive, - ec2.VpcPeeringConnectionStateReasonCodePendingAcceptance, - ec2.VpcPeeringConnectionStateReasonCodeDeleting, - }, - Target: []string{ - ec2.VpcPeeringConnectionStateReasonCodeRejected, - ec2.VpcPeeringConnectionStateReasonCodeDeleted, - }, - Refresh: vpcPeeringConnectionRefreshState(conn, d.Id()), - Timeout: d.Timeout(schema.TimeoutDelete), - } - if _, err := stateConf.WaitForState(); err != nil { + if err := waitForEc2VpcPeeringConnectionDeletion(conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil { return fmt.Errorf("Error waiting for VPC Peering Connection (%s) to be deleted: %s", d.Id(), err) } @@ -394,3 +381,23 @@ func vpcPeeringConnectionWaitUntilAvailable(conn *ec2.EC2, id string, timeout ti } return nil } + +func waitForEc2VpcPeeringConnectionDeletion(conn *ec2.EC2, id string, timeout time.Duration) error { + stateConf := &resource.StateChangeConf{ + Pending: []string{ + ec2.VpcPeeringConnectionStateReasonCodeActive, + ec2.VpcPeeringConnectionStateReasonCodePendingAcceptance, + ec2.VpcPeeringConnectionStateReasonCodeDeleting, + }, + Target: []string{ + ec2.VpcPeeringConnectionStateReasonCodeRejected, + ec2.VpcPeeringConnectionStateReasonCodeDeleted, + }, + Refresh: vpcPeeringConnectionRefreshState(conn, id), + Timeout: timeout, + } + + _, err := stateConf.WaitForState() + + return err +} diff --git a/aws/resource_aws_vpc_peering_connection_test.go b/aws/resource_aws_vpc_peering_connection_test.go index b4705879290..b48b23a106d 100644 --- a/aws/resource_aws_vpc_peering_connection_test.go +++ b/aws/resource_aws_vpc_peering_connection_test.go @@ -6,6 +6,7 @@ import ( "reflect" "regexp" "testing" + "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" @@ -14,6 +15,77 @@ import ( "github.com/hashicorp/terraform/terraform" ) +func init() { + resource.AddTestSweepers("aws_vpc_peering_connection", &resource.Sweeper{ + Name: "aws_vpc_peering_connection", + F: testSweepEc2VpcPeeringConnections, + }) +} + +func testSweepEc2VpcPeeringConnections(region string) error { + client, err := sharedClientForRegion(region) + if err != nil { + return fmt.Errorf("error getting client: %s", err) + } + + conn := client.(*AWSClient).ec2conn + input := &ec2.DescribeVpcPeeringConnectionsInput{} + + err = conn.DescribeVpcPeeringConnectionsPages(input, func(page *ec2.DescribeVpcPeeringConnectionsOutput, lastPage bool) bool { + if page == nil { + return !lastPage + } + + for _, vpcPeeringConnection := range page.VpcPeeringConnections { + deletedStatuses := map[string]bool{ + ec2.VpcPeeringConnectionStateReasonCodeDeleted: true, + ec2.VpcPeeringConnectionStateReasonCodeExpired: true, + ec2.VpcPeeringConnectionStateReasonCodeFailed: true, + ec2.VpcPeeringConnectionStateReasonCodeRejected: true, + } + + if _, ok := deletedStatuses[aws.StringValue(vpcPeeringConnection.Status.Code)]; ok { + continue + } + + id := aws.StringValue(vpcPeeringConnection.VpcPeeringConnectionId) + input := &ec2.DeleteVpcPeeringConnectionInput{ + VpcPeeringConnectionId: vpcPeeringConnection.VpcPeeringConnectionId, + } + + log.Printf("[INFO] Deleting EC2 VPC Peering Connection: %s", id) + + _, err := conn.DeleteVpcPeeringConnection(input) + + if isAWSErr(err, "InvalidVpcPeeringConnectionID.NotFound", "") { + continue + } + + if err != nil { + log.Printf("[ERROR] Error deleting EC2 VPC Peering Connection (%s): %s", id, err) + continue + } + + if err := waitForEc2VpcPeeringConnectionDeletion(conn, id, 5*time.Minute); err != nil { + log.Printf("[ERROR] Error waiting for EC2 VPC Peering Connection (%s) to be deleted: %s", id, err) + } + } + + return !lastPage + }) + + if testSweepSkipSweepError(err) { + log.Printf("[WARN] Skipping EC2 VPC Peering Connection sweep for %s: %s", region, err) + return nil + } + + if err != nil { + return fmt.Errorf("Error describing EC2 VPC Peering Connections: %s", err) + } + + return nil +} + func TestAccAWSVPCPeeringConnection_importBasic(t *testing.T) { resourceName := "aws_vpc_peering_connection.foo" diff --git a/aws/resource_aws_vpc_test.go b/aws/resource_aws_vpc_test.go index 00b05d8085e..de0f4091d02 100644 --- a/aws/resource_aws_vpc_test.go +++ b/aws/resource_aws_vpc_test.go @@ -25,6 +25,7 @@ func init() { "aws_route_table", "aws_security_group", "aws_subnet", + "aws_vpc_peering_connection", "aws_vpn_gateway", }, F: testSweepVPCs,