【发布时间】:2017-04-29 23:46:29
【问题描述】:
我正在使用 AWS Python SDK Boto3,我想知道哪些安全组未使用。用 boto2 我做到了,但我不知道如何用 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收集信息,将两个安全组名称值存储到各自的集合中,然后进行推断。 -
是的,当然,但我想知道是否有提供此信息的函数。在boto2中有
get_all_security_groups()。 -
不幸的是,没有。 Boto3 是一个重写 API,它有很好的文档记录和良好的维护,与 boto2 相比不那么令人惊讶。
-
Boto3 有很多强大的方法来得到你想要的。检查我的答案并根据您的需要进行调整。
-
这需要找到所有使用安全组(ec2s、负载均衡器、lambda 等等?)的答案。实际上,boto3 需要一种调用方式来查找对给定安全组的所有引用。
标签: python amazon-web-services amazon-ec2 aws-sdk boto3