
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.
- 172.31.0.0/20 - Availability Zone us-east-1a
- 172.31.16.0/20 - Availability Zone us-east-1b
- 172.31.32.0/20 - Availability Zone us-east-1c
- 172.31.48.0/20 - Availability Zone us-east-1d
- 172.31.64.0/20 - Availability Zone us-east-1d
- 172.31.80.0/20 - Availability Zone us-east-1f
~]$ aws ec2 describe-vpcs
{
"Vpcs": [
{
"CidrBlock": "172.31.0.0/16",
"DhcpOptionsId": "dopt-017f01234abcd2fc9",
"State": "available",
"VpcId": "vpc-014d2fc1234abcd01",
"OwnerId": "123456789012",
"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