Created
August 9, 2019 06:48
-
-
Save lantrix/3acdcbfd289e64e9dc0b9801f97e8c82 to your computer and use it in GitHub Desktop.
Delete all AWS Default VPCs with PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Requires -Module @{ModuleName="AWSPowerShell";ModuleVersion="3.3.450.0"} | |
| Write-Output -InputObject 'Delete all Default VPCs' | |
| $AllRegions = Get-EC2Region | |
| ForEach ($region in $AllRegions.RegionName) { | |
| Write-Output -InputObject "Searching $region" | |
| $defaultVpc = Get-EC2VPC -Filter @{name='isDefault'; Value='true'} -Region $region | |
| if ($defaultVpc) { | |
| Write-Output -InputObject "Found default VPC $($defaultVPC.VpcId) $($defaultVPC.CidrBlock) ... deleting" | |
| $VPCFilter = New-Object -TypeName Amazon.EC2.Model.Filter | |
| $VPCFilter.Value = $($defaultVPC.VpcId) | |
| $VPCFilter.Name = 'attachment.vpc-id' | |
| $igw = Get-EC2InternetGateway -Filter $VPCFilter -Region $region | |
| if ($igw) { Dismount-EC2InternetGateway -VpcId $($defaultVPC.VpcId) -InternetGatewayId $igw.InternetGatewayId -Region $region } | |
| if ($igw) { Remove-EC2InternetGateway -InternetGatewayId $igw.InternetGatewayId -Region $region -Force } | |
| $egw = Get-EC2EgressOnlyInternetGatewayList -Region $region | |
| if ($egw) { Remove-EC2EgressOnlyInternetGateway -EgressOnlyInternetGatewayId $egw.EgressOnlyInternetGatewayId -Region $region -Force } | |
| $VPCFilter.Name = 'vpc-id' | |
| $neti = Get-EC2NetworkInterface -Filter $VPCFilter -Region $region | |
| foreach ($eni in $neti) { | |
| #if ($eni.Attachment) { Dismount-EC2NetworkInterface -AttachmentId $eni.Attachment.AttachmentId -ForceDismount $true -Region $region } | |
| Remove-EC2NetworkInterface -NetworkInterfaceId $eni.NetworkInterfaceId -Region $region -Force | |
| } | |
| $VPCFilter.Name = 'requester-vpc-info.vpc-id' | |
| Get-EC2VpcPeeringConnection -Filter $VPCFilter -Region $region | Remove-EC2VpcPeeringConnection -Region $region | |
| $VPCFilter.Name = 'accepter-vpc-info.vpc-id' | |
| Get-EC2VpcPeeringConnection -Filter $VPCFilter -Region $region | Remove-EC2VpcPeeringConnection -Region $region | |
| $VPCFilter.Name = 'vpc-id' | |
| Get-EC2Subnet -Filter $VPCFilter -Region $region | Remove-EC2Subnet -Region $region -Force | |
| Remove-EC2VPC -VpcId $($defaultVPC.VpcId) -Region $region -Force | |
| Write-Output -InputObject "VPC $($defaultVPC.VpcId) $($defaultVPC.CidrBlock) ... DELETED" | |
| Get-EC2SecurityGroup -Filter $VPCFilter -Region $region | Remove-EC2SecurityGroup -Region $region -Force | |
| Get-EC2NetworkACL -Filter $VPCFilter -Region $region | Remove-EC2NetworkACL -Region $region -Force | |
| $VPCFilter.Name = 'vpc-id' | |
| $rtb = Get-EC2RouteTable -Filter $VPCFilter -Region $region | |
| if ($rtb) { Unregister-EC2RouteTable -AssociationId $rtb.Associations.RouteTableAssociationId -Region $region } | |
| if ($rtb) { Remove-EC2RouteTable -RouteTableId $rtb.RouteTableId -Region $region -Force } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment