Saturday, October 29, 2022

[SOLVED] How to find unused VPC in AWS account

Issue

Is there any way to find unused VPCs in an AWS account?

I mean the VPCs that don't have any EC2 instances, RDS and other services associated with it.

One way is to just search with VPC ID in running instances, RDS and for other services to find out whether it is in use or not. Is there any other way or AWS CLI to find unused VPCs?


Solution

There are many resources that be included in a VPC, such as:

  • Amazon EC2 instances
  • Amazon RDS instances
  • Amazon Redshift instances
  • Amazon Elasticache instances
  • Elastic Load Balancers
  • Elastic Network Interfaces
  • and so on!

Rather than trying to iterate through each of these services, you could iterate through the Elastic Network Interfaces (ENIs), since everything connects to a VPC via an ENI.

Here's a command you could run using the AWS Command-Line Interface (CLI) that shows ENIs attached to a given VPC:

aws ec2 describe-network-interfaces --filters 'Name=vpc-id,Values=vpc-abcd1234' --query 'NetworkInterfaces[*].NetworkInterfaceId'

If no ENIs are returned, then you'd probably call it an unused VPC.



Answered By - John Rotenstein
Answer Checked By - Mildred Charles (WPSolving Admin)