[英]Boto to Boto3 function implementation
1) How can i implement this from boto into boto3 code: 1)我如何将其从 boto 实现为 boto3 代码:
conn = boto.connect_ec2() # boto way
sgs = conn.get_all_security_groups() # boto way
for sg in sgs:
if len(sg.instances()) == 0:
print(sg.name, sg.id, len(sg.instances()))
The above code basically prints all Security Groups with no instances attached.上面的代码基本上打印了所有没有附加实例的安全组。
2) And this individual command which uses duct.sh()
module : 2)这个使用duct.sh()
模块的单独命令:
command = 'aws ec2 describe-instances --filters "Name=instance.group-id,Values=' + sg.id + '\" --query \'Reservations[*].Instances[*].[InstanceId,Tags[?Key==`Name`] | [0].Value]\' --output json'
boto: get_all_security_groups()
博托: get_all_security_groups()
boto3: security_group_iterator = ec2.security_groups.all()
boto3: security_group_iterator = ec2.security_groups.all()
However, boto has the .instances()
method on boto.ec2.securitygroup.SecurityGroup
, whereas boto3 does not have an equivalent method on ec2.SecurityGroup
.然而,博托具有.instances()
上方法boto.ec2.securitygroup.SecurityGroup
,而boto3不会对等效的方法ec2.SecurityGroup
。
Therefore, it looks like you would have to call describe_instances()
, passing the security group as a Filter
:因此,看起来您必须调用describe_instances()
,将安全组作为Filter
传递:
response = client.describe_instances(
Filters=[{'Name':'instance.group-id','Values':['sg-abcd1234']}])
This will return a list of instances that use the given security group.这将返回使用给定安全组的实例列表。
You could then count len(response['Reservations'])
to find unused security groups.然后,您可以计算len(response['Reservations'])
以查找未使用的安全组。 (Note: This is an easy way to find zero-length responses, but to count the actual instances would require adding up all Reservations.Instances
.) (注意:这是一种查找零长度响应的简单方法,但要计算实际实例需要将所有Reservations.Instances
相加。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.