func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
	conn := provider.Meta().(*AWSClient).ec2conn

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_instance" {
			continue
		}

		// Try to find the resource
		var err error
		resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
			InstanceIds: []*string{aws.String(rs.Primary.ID)},
		})
		if err == nil {
			if len(resp.Reservations) > 0 {
				return fmt.Errorf("still exist.")
			}

			return nil
		}

		// Verify the error is what we want
		ec2err, ok := err.(awserr.Error)
		if !ok {
			return err
		}
		if ec2err.Code() != "InvalidInstanceID.NotFound" {
			return err
		}
	}

	return nil
}
func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
	conn := provider.Meta().(*AWSClient).ec2conn

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_instance" {
			continue
		}

		// Try to find the resource
		resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
			InstanceIds: []*string{aws.String(rs.Primary.ID)},
		})
		if err == nil {
			for _, r := range resp.Reservations {
				for _, i := range r.Instances {
					if i.State != nil && *i.State.Name != "terminated" {
						return fmt.Errorf("Found unterminated instance: %s", i)
					}
				}
			}
		}

		// Verify the error is what we want
		if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidInstanceID.NotFound" {
			continue
		}

		return err
	}

	return nil
}
func testAccCheckRoute53ZoneAssociationExistsWithProvider(s *terraform.State, n string, zone *route53.HostedZone, provider *schema.Provider) error {
	rs, ok := s.RootModule().Resources[n]
	if !ok {
		return fmt.Errorf("Not found: %s", n)
	}

	if rs.Primary.ID == "" {
		return fmt.Errorf("No zone association ID is set")
	}

	zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(rs.Primary.ID)

	conn := provider.Meta().(*AWSClient).r53conn
	resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(zone_id)})
	if err != nil {
		return fmt.Errorf("Hosted zone err: %v", err)
	}

	exists := false
	for _, vpc := range resp.VPCs {
		if vpc_id == *vpc.VPCId {
			exists = true
		}
	}
	if !exists {
		return fmt.Errorf("Hosted zone association not found")
	}

	*zone = *resp.HostedZone
	return nil
}
func testAccCheckRoute53ZoneDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
	conn := provider.Meta().(*AWSClient).r53conn
	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_route53_zone" {
			continue
		}

		_, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)})
		if err == nil {
			return fmt.Errorf("Hosted zone still exists")
		}
	}
	return nil
}
func testAccCheckRoute53DelegationSetDestroy(s *terraform.State, provider *schema.Provider) error {
	conn := provider.Meta().(*AWSClient).r53conn
	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_route53_delegation_set" {
			continue
		}

		_, err := conn.GetReusableDelegationSet(&route53.GetReusableDelegationSetInput{Id: aws.String(rs.Primary.ID)})
		if err == nil {
			return fmt.Errorf("Delegation set still exists")
		}
	}
	return nil
}
func testAccCheckAWSS3BucketDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
	conn := provider.Meta().(*AWSClient).s3conn

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_s3_bucket" {
			continue
		}
		_, err := conn.DeleteBucket(&s3.DeleteBucketInput{
			Bucket: aws.String(rs.Primary.ID),
		})
		if err != nil {
			if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoSuchBucket" {
				return nil
			}
			return err
		}
	}
	return nil
}
func testAccCheckRoute53ZoneExistsWithProvider(s *terraform.State, n string, zone *route53.GetHostedZoneOutput, provider *schema.Provider) error {
	rs, ok := s.RootModule().Resources[n]
	if !ok {
		return fmt.Errorf("Not found: %s", n)
	}

	if rs.Primary.ID == "" {
		return fmt.Errorf("No hosted zone ID is set")
	}

	conn := provider.Meta().(*AWSClient).r53conn
	resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)})
	if err != nil {
		return fmt.Errorf("Hosted zone err: %v", err)
	}

	aws_comment := *resp.HostedZone.Config.Comment
	rs_comment := rs.Primary.Attributes["comment"]
	if rs_comment != "" && rs_comment != aws_comment {
		return fmt.Errorf("Hosted zone with comment '%s' found but does not match '%s'", aws_comment, rs_comment)
	}

	if !*resp.HostedZone.Config.PrivateZone {
		sorted_ns := make([]string, len(resp.DelegationSet.NameServers))
		for i, ns := range resp.DelegationSet.NameServers {
			sorted_ns[i] = *ns
		}
		sort.Strings(sorted_ns)
		for idx, ns := range sorted_ns {
			attribute := fmt.Sprintf("name_servers.%d", idx)
			dsns := rs.Primary.Attributes[attribute]
			if dsns != ns {
				return fmt.Errorf("Got: %v for %v, Expected: %v", dsns, attribute, ns)
			}
		}
	}

	*zone = *resp
	return nil
}
func testAccCreateRandomRoute53RecordsInZoneId(provider *schema.Provider, zone *route53.GetHostedZoneOutput, recordsCount int) error {
	conn := provider.Meta().(*AWSClient).r53conn

	var changes []*route53.Change
	if recordsCount > 100 {
		return fmt.Errorf("Route53 API only allows 100 record sets in a single batch")
	}
	for i := 0; i < recordsCount; i++ {
		changes = append(changes, &route53.Change{
			Action: aws.String("UPSERT"),
			ResourceRecordSet: &route53.ResourceRecordSet{
				Name: aws.String(fmt.Sprintf("%d-tf-acc-random.%s", acctest.RandInt(), *zone.HostedZone.Name)),
				Type: aws.String("CNAME"),
				ResourceRecords: []*route53.ResourceRecord{
					&route53.ResourceRecord{Value: aws.String(fmt.Sprintf("random.%s", *zone.HostedZone.Name))},
				},
				TTL: aws.Int64(int64(30)),
			},
		})
	}

	req := &route53.ChangeResourceRecordSetsInput{
		HostedZoneId: zone.HostedZone.Id,
		ChangeBatch: &route53.ChangeBatch{
			Comment: aws.String("Generated by Terraform"),
			Changes: changes,
		},
	}
	log.Printf("[DEBUG] Change set: %s\n", *req)
	resp, err := changeRoute53RecordSet(conn, req)
	if err != nil {
		return err
	}
	changeInfo := resp.(*route53.ChangeResourceRecordSetsOutput).ChangeInfo
	err = waitForRoute53RecordSetToSync(conn, cleanChangeID(*changeInfo.Id))
	return err
}
func testAccCheckRoute53ZoneAssociationDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
	conn := provider.Meta().(*AWSClient).r53conn
	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_route53_zone_association" {
			continue
		}

		zone_id, vpc_id := resourceAwsRoute53ZoneAssociationParseId(rs.Primary.ID)

		resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(zone_id)})
		if err != nil {
			exists := false
			for _, vpc := range resp.VPCs {
				if vpc_id == *vpc.VPCId {
					exists = true
				}
			}
			if exists {
				return fmt.Errorf("VPC: %v is still associated to HostedZone: %v", vpc_id, zone_id)
			}
		}
	}
	return nil
}