Skip to content

Commit

Permalink
Nuke only interface type ec2 network (gruntwork-io#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
james03160927 authored Jul 8, 2024
1 parent aef8a3e commit e3b5013
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions aws/resources/ec2_network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ func (ni *NetworkInterface) getAll(c context.Context, configObj config.Config) (
continue
}

// NOTE: Not all network interface types can be detached programmatically, and some may take longer to nuke.
// Interfaces attached to Lambda or other AWS services may have specific detachment mechanisms managed by
// those services. Attempting to detach these via the API can cause errors. Skipping non-interface types
// ensures they are cleaned up automatically upon service deletion.
if awsgo.StringValue(networkInterface.InterfaceType) != NetworkInterfaceTypeInterface {
logging.Debugf("[Skip] Can't detach network interface of type '%v' via API. "+
"Detachment for this type is managed by the dependent service and will occur automatically upon "+
"resource deletion.", awsgo.StringValue(networkInterface.InterfaceType))
continue
}

if shouldIncludeNetworkInterface(networkInterface, firstSeenTime, configObj) {
identifiers = append(identifiers, networkInterface.NetworkInterfaceId)
}
Expand Down
4 changes: 4 additions & 0 deletions aws/resources/ec2_network_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func TestNetworkInterface_GetAll(t *testing.T) {
NetworkInterfaces: []*ec2.NetworkInterface{
{
NetworkInterfaceId: awsgo.String(testId1),
InterfaceType: awsgo.String(NetworkInterfaceTypeInterface),
TagSet: []*ec2.Tag{
{
Key: awsgo.String("Name"),
Expand All @@ -85,6 +86,7 @@ func TestNetworkInterface_GetAll(t *testing.T) {
},
{
NetworkInterfaceId: awsgo.String(testId2),
InterfaceType: awsgo.String(NetworkInterfaceTypeInterface),
TagSet: []*ec2.Tag{
{
Key: awsgo.String("Name"),
Expand Down Expand Up @@ -180,6 +182,7 @@ func TestNetworkInterface_NukeAll(t *testing.T) {
NetworkInterfaces: []*ec2.NetworkInterface{
{
NetworkInterfaceId: awsgo.String(testId1),
InterfaceType: awsgo.String(NetworkInterfaceTypeInterface),
TagSet: []*ec2.Tag{
{
Key: awsgo.String("Name"),
Expand All @@ -193,6 +196,7 @@ func TestNetworkInterface_NukeAll(t *testing.T) {
},
{
NetworkInterfaceId: awsgo.String(testId2),
InterfaceType: awsgo.String(NetworkInterfaceTypeInterface),
TagSet: []*ec2.Tag{
{
Key: awsgo.String("Name"),
Expand Down
4 changes: 4 additions & 0 deletions aws/resources/ec2_network_interface_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"github.com/gruntwork-io/go-commons/errors"
)

const (
NetworkInterfaceTypeInterface = "interface"
)

type NetworkInterface struct {
BaseAwsResource
Client ec2iface.EC2API
Expand Down

0 comments on commit e3b5013

Please sign in to comment.