How can I remove VPC, I tried everything I can.

0
[cloudshell-user@ip-10-2-84-68 ~]$ #!/bin/bash
[cloudshell-user@ip-10-2-84-68 ~]$ vpc="vpc-0451179a01822bb2c"
[cloudshell-user@ip-10-2-84-68 ~]$ region="ap-southeast-1"
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-internet-gateways --region $region --filters 'Name=attachment.vpc-id,Values='$vpc | grep InternetGatewayId
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-subnets --region $region --filters 'Name=vpc-id,Values='$vpc | grep SubnetId
            "SubnetId": "subnet-0a4876918e50d2133",
            "SubnetId": "subnet-00690100198abfc2c",
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-route-tables --region $region --filters 'Name=vpc-id,Values='$vpc | grep RouteTableId
                    "RouteTableId": "rtb-0d4576706059012bd",
            "RouteTableId": "rtb-0d4576706059012bd",
                    "RouteTableId": "rtb-0b59b6e9d69621ab0",
                    "RouteTableId": "rtb-0b59b6e9d69621ab0",
            "RouteTableId": "rtb-0b59b6e9d69621ab0",
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-network-acls --region $region --filters 'Name=vpc-id,Values='$vpc | grep NetworkAclId
                    "NetworkAclId": "acl-020f2166d2681b0fb",
                    "NetworkAclId": "acl-020f2166d2681b0fb",
            "NetworkAclId": "acl-020f2166d2681b0fb",
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-vpc-peering-connections --region $region --filters 'Name=requester-vpc-info.vpc-id,Values='$vpc | grep VpcPeeringConnectionId
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-vpc-endpoints --region $region --filters 'Name=vpc-id,Values='$vpc | grep VpcEndpointId
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-nat-gateways --region $region --filter 'Name=vpc-id,Values='$vpc | grep NatGatewayId
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-security-groups --region $region --filters 'Name=vpc-id,Values='$vpc | grep GroupId
            "GroupId": "sg-0438ebaece1906cdf",
            "GroupId": "sg-095dd2ffe375ad929",
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-instances --region $region --filters 'Name=vpc-id,Values='$vpc | grep InstanceId
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-vpn-connections --region $region --filters 'Name=vpc-id,Values='$vpc | grep VpnConnectionId
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-vpn-gateways --region $region --filters 'Name=attachment.vpc-id,Values='$vpc | grep VpnGatewayId
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-network-interfaces --region $region --filters 'Name=vpc-id,Values='$vpc | grep NetworkInterfaceId
            "NetworkInterfaceId": "eni-05ff42074c4ed3b6a",
            "NetworkInterfaceId": "eni-070c32434d3cdf510",
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-carrier-gateways --region $region --filters Name=vpc-id,Values=$vpc | grep CarrierGatewayId

An error occurred (UnsupportedOperation) when calling the DescribeCarrierGateways operation: The functionality you requested is not available in this region.
[cloudshell-user@ip-10-2-84-68 ~]$ aws ec2 describe-local-gateway-route-table-vpc-associations --region $region --filters Name=vpc-id,Values=$vpc | grep LocalGatewayRouteTableVpcAssociationId


[cloudshell-user@ip-10-2-60-85 ~]$ #!/bin/bash
[cloudshell-user@ip-10-2-60-85 ~]$ 
[cloudshell-user@ip-10-2-60-85 ~]$ REGION=ap-southeast-1
[cloudshell-user@ip-10-2-60-85 ~]$ 
[cloudshell-user@ip-10-2-60-85 ~]$ # Detach subnets from route tables
[cloudshell-user@ip-10-2-60-85 ~]$ aws ec2 disassociate-route-table --association-id subnet-0a4876918e50d2133 --region $REGION

An error occurred (InvalidAssociationID.NotFound) when calling the DisassociateRouteTable operation: The association ID 'subnet-0a4876918e50d2133' does not exist
[cloudshell-user@ip-10-2-60-85 ~]$ aws ec2 disassociate-route-table --association-id subnet-00690100198abfc2c --region $REGION

An error occurred (InvalidAssociationID.NotFound) when calling the DisassociateRouteTable operation: The association ID 'subnet-00690100198abfc2c' does not exist
[cloudshell-user@ip-10-2-60-85 ~]$ 
[cloudshell-user@ip-10-2-60-85 ~]$ # Delete route tables
[cloudshell-user@ip-10-2-60-85 ~]$ aws ec2 delete-route-table --route-table-id rtb-0d4576706059012bd --region $REGION

An error occurred (DependencyViolation) when calling the DeleteRouteTable operation: The routeTable 'rtb-0d4576706059012bd' has dependencies and cannot be deleted.
[cloudshell-user@ip-10-2-60-85 ~]$ aws ec2 delete-route-table --route-table-id rtb-0b59b6e9d69621ab0 --region $REGION

An error occurred (DependencyViolation) when calling the DeleteRouteTable operation: The routeTable 'rtb-0b59b6e9d69621ab0' has dependencies and cannot be deleted.
[cloudshell-user@ip-10-2-60-85 ~]$ 
[cloudshell-user@ip-10-2-60-85 ~]$ # Delete network ACLs
[cloudshell-user@ip-10-2-60-85 ~]$ aws ec2 delete-network-acl --network-acl-id acl-020f2166d2681b0fb --region $REGION

An error occurred (InvalidParameterValue) when calling the DeleteNetworkAcl operation: cannot delete default network ACL acl-020f2166d2681b0fb
[cloudshell-user@ip-10-2-60-85 ~]$ 
[cloudshell-user@ip-10-2-60-85 ~]$ # Delete security groups
[cloudshell-user@ip-10-2-60-85 ~]$ aws ec2 delete-security-group --group-id sg-0438ebaece1906cdf --region $REGION

An error occurred (CannotDelete) when calling the DeleteSecurityGroup operation: the specified group: "sg-0438ebaece1906cdf" name: "default" cannot be deleted by a user
[cloudshell-user@ip-10-2-60-85 ~]$ aws ec2 delete-security-group --group-id sg-095dd2ffe375ad929 --region $REGION

An error occurred (DependencyViolation) when calling the DeleteSecurityGroup operation: resource sg-095dd2ffe375ad929 has a dependent object
[cloudshell-user@ip-10-2-60-85 ~]$ 
[cloudshell-user@ip-10-2-60-85 ~]$ # Delete network interfaces
[cloudshell-user@ip-10-2-60-85 ~]$ aws ec2 delete-network-interface --network-interface-id eni-05ff42074c4ed3b6a --region $REGION

An error occurred (InvalidParameterValue) when calling the DeleteNetworkInterface operation: Network interface 'eni-05ff42074c4ed3b6a' is currently in use.
[cloudshell-user@ip-10-2-60-85 ~]$ aws ec2 delete-network-interface --network-interface-id eni-070c32434d3cdf510 --region $REGION

An error occurred (InvalidParameterValue) when calling the DeleteNetworkInterface operation: Network interface 'eni-070c32434d3cdf510' is currently in use.
asked a year ago383 views
4 Answers
0

Have a good look at the documentation for the commands you're running. For example if you type "aws ec2 disassociate-route-table help" you'll see what parameters it's expecting. "--association-id" does not expect a subnet ID.

If you're not up to speed with the CLI, and this isn't something you need to do many times, why not use the console to delete what you need to?

EXPERT
answered a year ago
  • Dependency are in dependency are in deadlock. the console does not help. It is like billing ghost to me. dependency check says they are therefore but deleting commands do not found any.

  • I am using aws cloud shell and commands are ok. there is no syntax error.

  • No, there's no syntax errors but there are semantic errors. Maybe I wasn't clear - you are calling "aws ec2 disassociate-route-table --association-id subnet-0a4876918e50d2133" but this won't work, as you need to pass an Association ID, not a Subnet ID. BTW if you're deleting these resources because you're worried about being billed for them, note they don't cost anything.

0

Here are some examples of deleting VPC components and the VPC. I'm using tags as the filter, change to whatever you need.

Delete route table/subnet association.

aws ec2 disassociate-route-table \
	--association-id \
		$(aws ec2 describe-route-tables \
			--output text \
			--query "RouteTables[*].Associations[?SubnetId==\`\
				$(aws ec2 describe-subnets \
					--output text \
						--query "Subnets[0].SubnetId" \
						--filters "Name=tag:Name,Values=public-subnet")\`].RouteTableAssociationId" \
			--filters "Name=tag:Name,Values=public-route-table" \)

Delete route

aws ec2 delete-route \
	--destination-cidr-block "0.0.0.0/0" \
	--route-table-id \
		$(aws ec2 describe-route-tables \
			--output text \
			--query "RouteTables[0].RouteTableId" \
			--filters "Name=tag:Name,Values=public-route-table")

Delete Route table

aws ec2 delete-route-table \
	--route-table-id \
		$(aws ec2 describe-route-tables \
			--output text \
			--query "RouteTables[0].RouteTableId" \
			--filters "Name=tag:Name,Values=public-route-table")

Detach and delete internet gateway

aws ec2 detach-internet-gateway \
	--internet-gateway-id \
		$(aws ec2 describe-internet-gateways \
			--filters "Name=tag:Name,Values=internet-gateway" \
			--output text \
			--query "InternetGateways[0].InternetGatewayId") \
	--vpc-id \
		$(aws ec2 describe-vpcs \
			--filters "Name=tag:Name,Values=vpc" \
			--output text \
			--query "Vpcs[0].VpcId")
			
aws ec2 delete-internet-gateway \
	--internet-gateway-id \
		$(aws ec2 describe-internet-gateways \
			--filters "Name=tag:Name,Values=internet-gateway" \
			--output text \
			--query "InternetGateways[0].InternetGatewayId")

Delete subnet

aws ec2 delete-subnet \
	--subnet-id \
		$(aws ec2 describe-subnets \
			--filters "Name=tag:Name,Values=public-subnet" \
			--output text \
			--query "Subnets[0].SubnetId")

Delete VPC

aws ec2 delete-vpc \
	--vpc-id \
		$(aws ec2 describe-vpcs \
			--filters "Name=tag:Name,Values=vpc" \
			--output text \
			--query "Vpcs[0].VpcId")

If you have Network Interfaces that prevent deleting the VPC, you will need to find whatever that is an delete them first.

profile pictureAWS
EXPERT
kentrad
answered a year ago
  • Those Network Interfaces are not getting deleted also security rules are also not getting deleted. I will try you code replaced with name in Name and let you know.

  • Run this command to get an idea of what the ENIs are used for.

    aws ec2 describe-network-interfaces --query NetworkInterfaces[*].Description

0

It looks like maybe you're running a bash script (I see '#!/bin/bash' as the first command) and inside their you're setting a region of ap-southeast-1, so you're returning information about your assets in that region. But after the script terminates, what region do you have set? Probably a different one as you can't seem to delete the assets your script reported.

EXPERT
answered a year ago
  • Thank you for bringing my attention to it, I have updated code to set region but getting errors in different way.

0

I'm having a similar problem and I've narrowed it down to a mysterious user account ID attached to a network adapter and no such user exists. I get errors telling me that I lack the permissions to remove it despite running through both an admin account and as root. I'm the only user on this account so it's not like someone else attached a network adapter to a running machine built by me. I didn't create an account, attach a network interface, and delete the account - which even if I did, it should then automatically revert to me as root. So whatever the issue is, I'm stumped. I'm of no help to you except to say that you aren't alone. lol

sleerf
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions