【发布时间】:2017-04-29 10:59:07
【问题描述】:
我可以使用以下代码轻松列出所有安全组名称:
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'])
我正在尝试以相同的方式列出所有子网名称:
print('Subnets:')
print('-------')
sn_all = ec2_client.describe_subnets()
for sn in sn_all['Subnets'] :
print(sn['SubnetName'])
在这里,变量sn 获取所有子网,包括所有属性和标签,但找不到像GroupName 这样的子网的正确属性名称。
我可以使用 boto3.resource('ec2') 或以下代码,但为简单起见,我正在寻找我上面用来列出所有安全组的类似方法:
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'])
非常感谢任何帮助。
【问题讨论】:
标签: amazon-web-services boto3 amazon-vpc