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.