【发布时间】:2019-04-14 06:18:00
【问题描述】:
我正在尝试从所有地区获取未使用的 SG,但它不起作用。
我试过下面的代码
#!/usr/bin/env python
import boto3
ec2 = boto3.resource('ec2')
regions = ec2.describe_regions().get('Regions',[])
for region in regions:
reg=region['RegionName']
sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())
all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs
print 'Total SGs:', len(all_sgs)
print 'SGS attached to instances:', len(all_inst_sgs)
print 'Orphaned SGs:', len(unused_sgs)
print 'Unattached SG names:', unused_sgs
【问题讨论】:
-
您提取了该区域,但没有在任何地方使用它。要在代码中切换区域,需要使用 boto3.session