Bootstrap FreeKB - Amazon Web Services (AWS) - List Subnets using the AWS CLI
Amazon Web Services (AWS) - List Subnets 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.

Within each AWS region, there are Availabiltiy Zones which are isolated from each other. For example, the us-east-1 region has the following Availability Zones.

 

It is somewhat common for subnets to be associated with a particular Availability Zone, perhaps something this.

 

The aws ec2 describe-vpcs command can be used to list your EC2 Virtual Private Clouds (VPC). Something like this should be returned.

~]# 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"
                }
            ]
        }
    ]
}

 

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

aws ec2 describe-subnets

 

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

aws ec2 describe-subnets --query 'Subnets[?AvailabilityZone==`us-east-1a`]'

 

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"

 

Or, the --filter option can be used to list the Subnets with a specific CIDR block.

aws ec2 describe-subnets --filter "Name=cidr-block,Values=172.31.0.0/20"

 

Something like this should be returned. In this example, the 172.31.0.0/20 subnet is returned.

{
    "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": "123456789012",
            "AssignIpv6AddressOnCreation": false,
            "Ipv6CidrBlockAssociationSet": [],
            "SubnetArn": "arn:aws:ec2:us-east-1:123456789012: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 afbbca in the box below so that we can be sure you are a human.