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

Add nil pointer checks to avoid exception for VPC nuke #807

Merged
merged 1 commit into from
Dec 15, 2024
Merged
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
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