Rafiq
Rafiq

Reputation: 1642

How to list the name of all AWS subnets using boto3

I can easily list all security group names using the following codes:

import boto3
ec2_client = boto3.client('ec2')

print('All Security Groups:')
print('----------------')
sg_all = ec2_client.describe_security_groups()
for sg in sg_all['SecurityGroups'] :
    print(sg['GroupName'])

I am trying to list all the subnet names the same way like:

print('Subnets:')
print('-------')
sn_all = ec2_client.describe_subnets()
for sn in sn_all['Subnets'] :
    print(sn['SubnetName'])

Here, variable sn gets all each subnet including all properties and tags but cannot find the right property name for subnet like GroupName.

I can use boto3.resource('ec2') or the following codes but for simplicity I am looking for the similar method I used above to list all security groups:

print('Subnets:')
print('-------')
sn_all = ec2_client.describe_subnets()
for sn in sn_all['Subnets'] :
    for tag in sn['Tags']:
        if tag['Key'] == 'Name':
            print(tag['Value'])

Any help is greatly appreciated.

Upvotes: 3

Views: 11717

Answers (3)

Rafiq
Rafiq

Reputation: 1642

It's been a long time since I posted this question and accepted the answer from @JohnRotenstein. Learning more about AWS, boto3 and Python, I thought it would be helpful for others to give a clearer answer. I'm answering this keeping full respect to @JohnRotenstein's answer, as that explained why we cannot always just use the Subnet Name to list a subnet.

As the tag Name is optional in AWS subnets, not all subnets have this Name tag. So, listing the subnets based on the Name tag won't give all the subnets. To get all the subnets with their Name tags, if exists, the following solution will work. It will list all the subnets even if the Name tag is missing.

import boto3
ec2_client = boto3.client('ec2')
print('Subnet_ID\tSubnet_Name')
print('-----------------------------')
sn_all = ec2_client.describe_subnets()

for sn in sn_all['Subnets'] :
    print(sn['SubnetId'], end='')
    for tag in sn['Tags']:
        if tag['Key'] == 'Name':
            print('\t' + tag['Value'])

Output:

Subnet_ID       Subnet_Name
-----------------------------
subnet-02d9041b12d08xxxx        public_1
subnet-085b8f0deb821xxxx        public_2
subnet-09a2ffb46dc55xxxx        public_3

Upvotes: 0

Adarsh Kumar
Adarsh Kumar

Reputation: 67

import boto3
ec2_client = boto3.client('ec2')
print('Subnets:')
print('-------')
sn_all = ec2_client.describe_subnets()
for sn in sn_all['Subnets'] :
    print(sn['SubnetId'])

Upvotes: 7

John Rotenstein
John Rotenstein

Reputation: 269191

Amazon VPC Subnets do not have a "Name" field (whereas Security Groups do have a GroupName field).

In the management console, you can see that Security Groups have two columns: Group Name and Name. The Name field is actually the value of the tag called Name.

Subnets, on the other hand, only have the Name tag.

Tip: An easy way to see what information is available is by looking at the AWS Command-Line Interface (CLI) documentation for describe-subnets.

Upvotes: 3

Related Questions