Home > Blockchain >  How to delete AWS VPC?
How to delete AWS VPC?

Time:04-05

When I do aws ec2 delete-vpc --vpc-id vpc-0a72e2c7f0fc1234bb --profile me I have this error:

An error occurred (DependencyViolation) when calling the DeleteVpc operation: The vpc 'vpc-0a72e2c7f0fc1234bb' has dependencies and cannot be deleted.

So I use this script:

#!/bin/bash

vpc="vpc-0a72e2c7f0fc1234bb"
 
aws ec2 describe-internet-gateways --profile me --filters 'Name=attachment.vpc-id,Values='$vpc | grep InternetGatewayId

aws ec2 describe-subnets --profile me --filters 'Name=vpc-id,Values='$vpc | grep SubnetId
 
aws ec2 describe-route-tables --profile me --filters 'Name=vpc-id,Values='$vpc | grep RouteTableId 

aws ec2 describe-network-acls --profile me --filters 'Name=vpc-id,Values='$vpc | grep NetworkAclId 

aws ec2 describe-vpc-peering-connections --profile me --filters 'Name=requester-vpc-info.vpc-id,Values='$vpc | grep VpcPeeringConnectionId 

aws ec2 describe-vpc-endpoints --profile me --filters 'Name=vpc-id,Values='$vpc | grep VpcEndpointId 

aws ec2 describe-nat-gateways --profile me --filter 'Name=vpc-id,Values='$vpc | grep NatGatewayId 

aws ec2 describe-security-groups --profile me --filters 'Name=vpc-id,Values='$vpc | grep GroupId 

aws ec2 describe-instances --profile me --filters 'Name=vpc-id,Values='$vpc | grep InstanceId 

aws ec2 describe-vpn-connections --profile me --filters 'Name=vpc-id,Values='$vpc | grep VpnConnectionId 

aws ec2 describe-vpn-gateways --profile me --filters 'Name=attachment.vpc-id,Values='$vpc | grep VpnGatewayId 

aws ec2 describe-network-interfaces --profile me --filters 'Name=vpc-id,Values='$vpc | grep NetworkInterfaceId

I have any result:

    "InternetGatewayId": "igw-0ccf14d1cd1234fc09",

    "SubnetId": "subnet-096c13760c2a123456",

    "RouteTableId": "rtb-0600f8621234a125c",

    "RouteTableId": "rtb-0600f8625403a123c",

    "NetworkAclId": "acl-097e5ff0cb12ebf03",

    "NetworkAclId": "acl-097e5ff0cb12ebf03",

    "GroupId": "sg-096d7fbdc9c078db7",

    "GroupId": "sg-0f0c04a1bdacbd123",

    "GroupId": "sg-0f0c04a1bdacbd123",

    "NetworkInterfaceId": "eni-0eefb11efb8a12d34",

So I try delete one by one but do not work. I have allays this error:

An error occurred (DependencyViolation) when calling the DeleteSecurityGroup operation: resource sg-096d7fbdc9c078db7 has a dependent object

or

An error occurred (CannotDelete) when calling the DeleteSecurityGroup operation: the specified group: "sg-0f0c04a1bdacbd123" name: "default" cannot be deleted by a user

or

An error occurred (DependencyViolation) when calling the DeleteRouteTable operation: The routeTable '0600f8621234a125c' has dependencies and cannot be deleted.

...

CodePudding user response:

An error occurred (CannotDelete) when calling the DeleteSecurityGroup operation: the specified group: "sg-0f0c04a1bdacbd123" name: "default" cannot be deleted by a user

sg-0f0c04a1bdacbd123 is a default security group. Every VPC has a default security group when it is created. You can not delete default security groups. They are deleted when the VPC itself is deleted.

An error occurred (DependencyViolation) when calling the DeleteRouteTable operation: The routeTable '0600f8621234a125c' has dependencies and cannot be deleted.

If this is the main route table of the VPC, then it can not be deleted by itself. If this is a custom route table created by you, first you may want to disassociate any subnet/internet gateway to which this route table may be attached. You can check if the route table is associated to something by going into the VPC->Route Tables, selecting the route table and check if there is any association under the Subnet Association and Edge Association.

An error occurred (DependencyViolation) when calling the DeleteSecurityGroup operation: resource sg-096d7fbdc9c078db7 has a dependent object

This is not a default security group, so this can be deleted as long as it is not associated to anything. Security groups are associated to Elastic Network Interfaces (ENIs). You may want to get rid of the ENIs first, or dissociate the security group from the ENIs (you can associate the default security groups to these ENIs, you can not have an ENIs without a security group).

CodePudding user response:

In my case, I have a EC2 LoadBalander. I remove this (attach to my VPC) and after I retry delete VPC and is it OK.

  • Related