我正在使用AWS Python SDK Boto3,并试图了解哪些安全组未被使用。随着boto2我做到了,但我不知道如何做到与boto3一样。boto3搜索未使用的安全组
from boto.ec2.connection import EC2Connection
from boto.ec2.regioninfo import RegionInfo
import boto.sns
import sys
import logging
from security_groups_config import config
# Get settings from config.py
aws_access_key = config['aws_access_key']
aws_secret_key = config['aws_secret_key']
ec2_region_name = config['ec2_region_name']
ec2_region_endpoint = config['ec2_region_endpoint']
region = RegionInfo(name=ec2_region_name, endpoint=ec2_region_endpoint)
if aws_access_key:
conn = EC2Connection(aws_access_key, aws_secret_key, region=region)
else:
conn = EC2Connection(region=region)
sgs = conn.get_all_security_groups()
## Searching unused SG if the instances number is 0
def search_unused_sg(event, context):
for sg in sgs:
print sg.name, len(sg.instances())
在boto3,您可以收集来自“describe_instances”和“describe_security_groups”的信息,存储安全组名称价值转化为各自的集合,然后进行扣除。 – mootmoot
是的,当然,但我想知道是否有一个函数提供这些信息。在boto2中有'''get_all_security_groups()'''。 – Robert
不幸的是,没有。 Boto3是一个重写API,与boto2相比,它有很好的文档记录和良好的维护,并且没有什么惊喜。 – mootmoot