Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_alb: Cleanup ENIs after deleting ALB #1427

Merged
merged 1 commit into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions aws/resource_aws_alb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -401,6 +402,62 @@ func resourceAwsAlbDelete(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error deleting ALB: %s", err)
}

err := cleanupALBNetworkInterfaces(meta.(*AWSClient).ec2conn, d.Id())
if err != nil {
log.Printf("[WARN] Failed to cleanup ENIs for ALB %q: %#v", d.Id(), err)
}

return nil
}

// ALB automatically creates ENI(s) on creation
// but the cleanup is asynchronous and may take time
// which then blocks IGW, SG or VPC on deletion
// So we make the cleanup "synchronous" here
func cleanupALBNetworkInterfaces(conn *ec2.EC2, albArn string) error {
re := regexp.MustCompile("([^/]+/[^/]+/[^/]+)$")
matches := re.FindStringSubmatch(albArn)
if len(matches) != 2 {
return fmt.Errorf("Unexpected ARN format: %q", albArn)
}

// e.g. app/example-alb/b26e625cdde161e6
name := matches[1]

out, err := conn.DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("attachment.instance-owner-id"),
Values: []*string{aws.String("amazon-elb")},
},
{
Name: aws.String("description"),
Values: []*string{aws.String("ELB " + name)},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is ALB failing here? more a question rather than a remark :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually intention - the underlying API uses a different naming convention, "ELB v2" they call it, so ELB is correct here in this context.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know, thanks! :)

},
},
})
if err != nil {
return err
}

log.Printf("[DEBUG] Found %d ENIs to cleanup for ALB %q",
len(out.NetworkInterfaces), name)

if len(out.NetworkInterfaces) == 0 {
// Nothing to cleanup
return nil
}

err = detachNetworkInterfaces(conn, out.NetworkInterfaces)
if err != nil {
return err
}

err = deleteNetworkInterfaces(conn, out.NetworkInterfaces)
if err != nil {
return err
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_elb.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ func isValidProtocol(s string) bool {

// ELB automatically creates ENI(s) on creation
// but the cleanup is asynchronous and may take time
// which then blocks IGW or VPC on deletion
// which then blocks IGW, SG or VPC on deletion
// So we make the cleanup "synchronous" here
func cleanupELBNetworkInterfaces(conn *ec2.EC2, name string) error {
out, err := conn.DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{
Expand Down