我试图删除 Amazon VPC,但收到了一条依赖项错误。如何删除我的 Amazon VPC?

3 分钟阅读
0

在试图删除 Amazon Virtual Private Cloud(Amazon VPC)时,我收到了一条依赖项错误。

简短描述

在删除 Amazon VPC 时,也会删除其所有组件。这些组件包括子网、安全组、网络访问控制列表(网络 ACL)、路由表、互联网网关和 DHCP 选项。但是,在试图删除 Amazon VPC 之前,必须先删除或取消关联所有依赖资源。请按照此处列出的顺序执行下列步骤,以避免出现依赖项错误。

解决方案

在删除 Amazon VPC 之前,必须先删除依赖资源。

您可以使用 AWS 命令行界面(AWS CLI)delete-vpc 命令或 Amazon VPC 控制台来删除 Amazon VPC。

**注意:**如果在运行 AWS CLI 命令时收到错误,请确保您使用的是最新的 AWS CLI 版本

解决通过 AWS CLI 尝试删除 Amazon VPC 时收到的错误

以下错误表示您必须先删除依赖项,然后才能删除 Amazon VPC:

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

要解决此错误,请完成下面的步骤:

1.    运行下面的脚本以查找其余依赖项:

#!/bin/bash
vpc="vpc-xxxxxxxxxxxxx"
region="yy-yyyy-y"
aws ec2 describe-internet-gateways --region $region --filters 'Name=attachment.vpc-id,Values='$vpc | grep InternetGatewayId
aws ec2 describe-subnets --region $region --filters 'Name=vpc-id,Values='$vpc | grep SubnetId
aws ec2 describe-route-tables --region $region --filters 'Name=vpc-id,Values='$vpc | grep RouteTableId
aws ec2 describe-network-acls --region $region --filters 'Name=vpc-id,Values='$vpc | grep NetworkAclId
aws ec2 describe-vpc-peering-connections --region $region --filters 'Name=requester-vpc-info.vpc-id,Values='$vpc | grep VpcPeeringConnectionId
aws ec2 describe-vpc-endpoints --region $region --filters 'Name=vpc-id,Values='$vpc | grep VpcEndpointId
aws ec2 describe-nat-gateways --region $region --filter 'Name=vpc-id,Values='$vpc | grep NatGatewayId
aws ec2 describe-security-groups --region $region --filters 'Name=vpc-id,Values='$vpc | grep GroupId
aws ec2 describe-instances --region $region --filters 'Name=vpc-id,Values='$vpc | grep InstanceId
aws ec2 describe-vpn-connections --region $region --filters 'Name=vpc-id,Values='$vpc | grep VpnConnectionId
aws ec2 describe-vpn-gateways --region $region --filters 'Name=attachment.vpc-id,Values='$vpc | grep VpnGatewayId
aws ec2 describe-network-interfaces --region $region --filters 'Name=vpc-id,Values='$vpc | grep NetworkInterfaceId
aws ec2 describe-carrier-gateways --region $region --filters Name=vpc-id,Values=$vpc | grep CarrierGatewayId
aws ec2 describe-local-gateway-route-table-vpc-associations --region $region --filters Name=vpc-id,Values=$vpc | grep LocalGatewayRouteTableVpcAssociationId

**注意:**在前面的脚本中,在 vpc 字段中输入您的 VPC ID,在 Region 字段中输入您的 AWS 区域(例如 us-east-1)。此外,请确保使用具有适当权限的凭证来运行 Describe API 调用。

2.    删除您在步骤 1 中确定的其余依赖项,然后重新尝试删除您的 Amazon VPC。

解决在试图通过 Amazon VPC 控制台删除 Amazon VPC 时收到的错误

"VPC contains one or more instances, and cannot be deleted until those instances have been terminated."

此错误表示 Amazon VPC 上正在运行 Amazon Elastic Compute Cloud(Amazon EC2)实例。若要解决此错误,请终止您的实例

"VPC contains one or more in-use network interfaces, and cannot be deleted until those network interfaces have been deleted."

此错误表明 Amazon VPC 的某些网络接口正在使用或可用。

"An error occurred (InvalidParameterValue) when calling the DeleteNetworkInterface operation: Network interface 'eni-aabbccdd' is currently in use."

此错误表示存在您无法删除、由请求者托管的网络接口。若要删除请求者托管的网络接口,您必须删除创建网络接口的 AWS 服务。

请按照下列步骤删除依赖服务:

1.    打开 Amazon EC2 控制台

2.    选择 Amazon VPC 所在的 AWS 区域。

3.    在导航窗格的网络接口下,搜索要删除的 Amazon VPC 的 VPC ID。

4.    选择网络接口,然后选择详细信息选项卡。

5.    查看描述,以查看网络接口附加到哪些资源。

6.    删除关联的资源。例如,您正在删除一个网络接口,而请求者 ID 为 amazon-elb。使用弹性网络接口的描述字段中的值来识别负载均衡器。然后,导航到 Amazon EC2 控制台的负载均衡器部分,找到该负载均衡器并将其删除。**
注意:**如果该网络接口是主网络接口,则删除实例时会删除此接口。

"VPC has one or more attached NAT gateways or virtual private gateways, and cannot be deleted until they are detached or deleted."

此错误表明 Amazon VPC 有一个依赖项。要删除依赖项,请按照 Delete a NAT gatewayDetach and delete a virtual private gateway 中的步骤进行操作。

"The VPC is peered as a requester with at least one other VPC through peering connections."

此错误表示 Amazon VPC 中存在活动的 VPC 对等连接。若要将其删除,请按照 Delete a VPC peering connection 中的步骤进行操作。

如果仍然存在阻止删除 Amazon VPC 的依赖项,请按照以下过程进行操作。

若要删除运营商网关,请执行下列操作:

1.    打开 Amazon VPC 控制台

2.    在导航窗格中,选择运营商网关

3.    选择运营商网关,然后选择操作。然后,选择删除运营商网关

4.    在删除运营商网关对话框中,输入删除,然后选择删除

若要解除 VPC 与任何本地网关路由表的关联,请执行下列操作:

1.    打开 AWS Outposts 控制台

2.    在导航窗格中,选择本地网关路由表

3.    选择路由表。

4.    选择操作,然后选择查看详细信息

5.    在 VPC 关联中,选择要取消关联的 VPC,然后选择取消关联

6.    选择取消关联

相关信息

为什么我无法分离或删除 Lambda 创建的弹性网络接口?

如何删除与其他 AWS 账户共享的 VPC?

为什么我无法删除请求者托管的 VPC 端点?

AWS 官方
AWS 官方已更新 1 年前