Logical Network

Introduction

Sometimes we need to check the VPCs and Subnets in your AWS account. Especially, when we are experimenting a lot and we need to free up the VPCs to stay within the limits imposed by Amazon. We can quickly list all the VPCs and Subnets in your AWS account using the AWS CLI.

Prerequisite

You must have already installed the AWS CLI and configured with the necessary permissions to list VPCs and subnets.

List VPCs

To list all VPCs in your current AWS region:

List VPCs AWS CLI command
aws ec2 describe-vpcs --query "Vpcs[].{ID:VpcId,CIDR:CidrBlock}" --output table

The describe-vpcs operation is used to retrieve details about your VPCs. The --query option is used to filter and format the output to show only the VPC IDs and their associated CIDR blocks. The --output table option formats the output as a table.

List VPCs AWS CLI output
--------------------------------------------
|               DescribeVpcs               |
+----------------+-------------------------+
|      CIDR      |           ID            |
+----------------+-------------------------+
|  10.0.0.0/16   |  vpc-0164b34c20f8a3819  |
|  172.31.0.0/16 |  vpc-0feaeadce8b18225e  |
|  10.0.0.0/16   |  vpc-0ddc2db4cc6dc0000  |
+----------------+-------------------------+

List Subnets

To list all subnets within a specific VPC or your current AWS region:

List Subnets AWS CLI command
aws ec2 describe-subnets --query "Subnets[].{ID:SubnetId,CIDR:CidrBlock,VPC:VpcId}" --output table
List Subnets AWS CLI output
-------------------------------------------------------------------------
|                            DescribeSubnets                            |
+----------------+----------------------------+-------------------------+
|      CIDR      |            ID              |           VPC           |
+----------------+----------------------------+-------------------------+
|  172.31.64.0/20|  subnet-085ca6a61df52146f  |  vpc-0feaeadce8b18225e  |
|  172.31.32.0/20|  subnet-0ccf174625076c913  |  vpc-0feaeadce8b18225e  |
|  172.31.0.0/20 |  subnet-0373daddf3d581157  |  vpc-0feaeadce8b18225e  |
|  172.31.80.0/20|  subnet-0c8959a4ab5f1b440  |  vpc-0feaeadce8b18225e  |
|  172.31.48.0/20|  subnet-0ca9e00ab880180db  |  vpc-0feaeadce8b18225e  |
|  172.31.16.0/20|  subnet-060dad57d831ae408  |  vpc-0feaeadce8b18225e  |
+----------------+----------------------------+-------------------------+

The AWS CLI commands default to the AWS region specified in your configuration. To list resources in a different region, add the --region parameter followed by the region code to your command (e.g., --region us-west-2).