Skip to content

Commit

Permalink
Add nil pointer checks to avoid exception for VPC nuke
Browse files Browse the repository at this point in the history
  • Loading branch information
james03160927 committed Dec 12, 2024
1 parent 8ceb3d3 commit 562fa66
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions aws/resources/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func shouldIncludeInstanceId(instance *ec2.Instance, protected bool, configObj c
}

func (ei *EC2Instances) releaseEIPs(instanceIds []*string) error {
logging.Debugf("Releasing Elastic IP address(s) associated on instances")
for _, instanceID := range instanceIds {
logging.Debugf("Releasing Elastic IP address(s) associated with instances")

// get the elastic ip's associated with the EC2's
for _, instanceID := range instanceIds {
// Get the Elastic IPs associated with the EC2 instances
output, err := ei.Client.DescribeAddressesWithContext(ei.Context, &ec2.DescribeAddressesInput{
Filters: []*ec2.Filter{
{
Expand All @@ -96,15 +96,27 @@ func (ei *EC2Instances) releaseEIPs(instanceIds []*string) error {
return err
}

// Ensure output is not nil before iterating
if output == nil || len(output.Addresses) == 0 {
logging.Debugf("No Elastic IPs found for instance %s", *instanceID)
continue
}

for _, address := range output.Addresses {
if address.AllocationId == nil {
logging.Debugf("Skipping Elastic IP release: AllocationId is nil for instance %s", *instanceID)
continue
}

_, err := ei.Client.ReleaseAddressWithContext(ei.Context, &ec2.ReleaseAddressInput{
AllocationId: address.AllocationId,
})

if err != nil {
logging.Debugf("An error happened while releasing the elastic ip address %s, error %v", *address.AllocationId, err)
logging.Debugf("An error occurred while releasing the Elastic IP address %s, error: %v", *address.AllocationId, err)
continue
}

logging.Debugf("Released Elastic IP address %s from instance %s", *address.AllocationId, *instanceID)
}
}
Expand Down

0 comments on commit 562fa66

Please sign in to comment.