Skip to content

Commit

Permalink
Merge pull request #158 from youyangl/master
Browse files Browse the repository at this point in the history
Disassociate elastic IP address when detaching Internet Gateway
  • Loading branch information
k8s-ci-robot committed Jan 9, 2023
2 parents ecb4ed8 + 4f1fb38 commit 3e30fa4
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions aws-janitor/resources/internet_gateways.go
Expand Up @@ -77,6 +77,37 @@ func (InternetGateways) MarkAndSweep(opts Options, set *Set) error {
break
}

var publicIPsToRelease []*string

pageFunc := func(page *ec2.DescribeNetworkInterfacesOutput, _ bool) bool {
for _, eni := range page.NetworkInterfaces {
publicIPsToRelease = append(publicIPsToRelease, eni.Association.PublicIp)
}
return true
}

if err := svc.DescribeNetworkInterfacesPages(&ec2.DescribeNetworkInterfacesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{aws.String(*att.VpcId)},
},
},
}, pageFunc); err != nil {
return err
}

// According to https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Internet_Gateway.html#detach-igw
// Before detaching the internet gateway, we must dissassociate elastic IPs first.
for _, publicIP := range publicIPsToRelease {
disassociateReq := &ec2.DisassociateAddressInput{
PublicIp: aws.String(*publicIP),
}
if _, err := svc.DisassociateAddress(disassociateReq); err != nil {
logger.Warningf("%s: disassociate failed: %v", *publicIP, err)
}
}

detachReq := &ec2.DetachInternetGatewayInput{
InternetGatewayId: ig.InternetGatewayId,
VpcId: att.VpcId,
Expand Down

0 comments on commit 3e30fa4

Please sign in to comment.