Bootstrap FreeKB - Amazon Web Services (AWS) - List Virtual Private Cloud (VPC) using the AWS CLI
Amazon Web Services (AWS) - List Virtual Private Cloud (VPC) using the AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

The aws ec2 describe-vpcs command can be used to list your EC2 Virtual Private Clouds (VPC). Something like this should be returned. In this example, the VPC has CIDR 172.31.0.0/20 CIDR.

Tip

Your Virtual Private Cloud (VPC) will be associated with a CIDR block (e.g. 172.31.0.0/16).  In this example, the first available private IP would be 172.31.0.1 and the last available private IP would be 172.31.255.254. Typically, there are 6 subnets in the Virtual Private Cloud (VPC), perhaps something like this.

~]$ aws ec2 describe-vpcs
{
    "Vpcs": [
        {
            "CidrBlock": "172.31.0.0/16",
            "DhcpOptionsId": "dopt-017f01234abcd2fc9",
            "State": "available",
            "VpcId": "vpc-014d2fc1234abcd01",
            "OwnerId": "713541234252",
            "InstanceTenancy": "default",
            "CidrBlockAssociationSet": [
                {
                    "AssociationId": "vpc-cidr-assoc-0ea471234abcddd9f",
                    "CidrBlock": "172.31.0.0/16",
                    "CidrBlockState": {
                        "State": "associated"
                    }
                }
            ],
            "IsDefault": true,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "default"
                }
            ]
        }
    ]
}

 

And here is an example of how to limit the output using the --query option.

aws ec2 describe-vpcs --query 'Vpcs[?CidrBlock==`172.31.0.0/16`]'

 

The aws ec2 describe-subnets command can be used to list the EC2 subnets in your Virtual Private Clouds (VPC).

aws ec2 describe-subnets

 

Or, the --filter option can be used to list the Subnets in a specific VPC.

aws ec2 describe-subnets --filter "Name=vpc-id,Values=vpc-014d2fc1234abcd01"

 

In all liklihood, there are probably 3 (or more) subnets for these CIDRs.

  • 172.31.0.0/20 (first available IP 172.31.0.1, last available IP 172.31.15.254)
  • 172.31.16.0/20 (first available IP 172.16.0.1, last available IP 172.31.31.254)
  • 172.31.32.0/20 (first available IP 172.32.0.1, last available IP 172.31.47.254)
{
    "Subnets": [
        {
            "AvailabilityZone": "us-east-1d",
            "AvailabilityZoneId": "use1-az6",
            "AvailableIpAddressCount": 4091,
            "CidrBlock": "172.31.0.0/20",
            "DefaultForAz": true,
            "MapPublicIpOnLaunch": true,
            "MapCustomerOwnedIpOnLaunch": false,
            "State": "available",
            "SubnetId": "subnet-03f11411234f6abcd",
            "VpcId": "vpc-014d2123433abcd01",
            "OwnerId": "713512344252",
            "AssignIpv6AddressOnCreation": false,
            "Ipv6CidrBlockAssociationSet": [],
            "SubnetArn": "arn:aws:ec2:us-east-1:711234074252:subnet/subnet-031234abcd0f6cdbc",
            "EnableDns64": false,
            "Ipv6Native": false,
            "PrivateDnsNameOptionsOnLaunch": {
                "HostnameType": "ip-name",
                "EnableResourceNameDnsARecord": false,
                "EnableResourceNameDnsAAAARecord": false
            }
        }
    ]
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter eb73f1 in the box below so that we can be sure you are a human.