我一直在尝试获取每个自动缩放组中所有实例的IP详细信息,我使用paginator列出并使用print asg['AutoScalingGroupName']
打印
import boto
import boto3
client = boto3.client('autoscaling', region_name='us-west-2')
autoscale = boto.connect_autoscale()
ec2 = boto.connect_ec2()
paginator = client.get_paginator('describe_auto_scaling_groups')
page_iterator = paginator.paginate(PaginationConfig={'PageSize': 100}
)
filtered_asgs = page_iterator.search(
'AutoScalingGroups[] | [?contains(Tags[?Key==`{}`].Value, `{}`)]'.format('Application', 'CCP')
)
for asg in filtered_asgs:
print asg['AutoScalingGroupName']
group = autoscale.get_all_groups(asg['AutoScalingGroupName'])[0]
instance_ids = [i.instance_id for i in group.instances]
reservations = ec2.get_all_instances(instance_ids)
instances = [i for r in reservations for i in r.instances]
print instances
请帮助我找出我在这里缺少的东西。
答案 0 :(得分:2)
有点晚了,但是我在这里发布了我的版本。
我这样做是为了仅获取通过调用describe_auto_scaling_groups获得的第一个自动缩放组的数据,进行相应的修改。
import boto3
ec2 = boto3.resource('ec2')
asg = boto3.client('autoscaling')
response= asg.describe_auto_scaling_groups(AutoScalingGroupNames=['myAutoScaleName',],)
groups=response.get("AutoScalingGroups")
instances=(groups[0].get('Instances'))
for i in instances:
pubip=ec2.Instance(i.get('InstanceId')).public_ip_address
print(pubip)
答案 1 :(得分:1)
此代码段应该可以使用,您只需添加特定的过滤器:
#!/usr/bin/env python
import boto3
ec2 = boto3.resource('ec2')
def get_instances():
client = boto3.client('autoscaling')
paginator = client.get_paginator('describe_auto_scaling_groups')
groups = paginator.paginate().build_full_result()
for asg in groups['AutoScalingGroups']:
print asg['AutoScalingGroupName']
instance_ids = [i for i in asg['Instances']]
running_instances = ec2.instances.filter(Filters=[{}])
for instance in running_instances:
print(instance.id, instance.public_dns_name, instance.private_ip_address)
if __name__ == '__main__':
get_instances()
答案 2 :(得分:1)
@Lorena,你的建议确实有帮助。
我已正确迭代过滤结果,如下所示。
import boto3
ec2 = boto3.resource('ec2', region_name='us-west-2')
def get_instances():
client = boto3.client('autoscaling', region_name='us-west-2')
paginator = client.get_paginator('describe_auto_scaling_groups')
groups = paginator.paginate(PaginationConfig={'PageSize': 100})
#print groups
filtered_asgs = groups.search('AutoScalingGroups[] | [?contains(Tags[?Key==`{}`].Value, `{}`)]'.format('Application', 'CCP'))
for asg in filtered_asgs:
print asg['AutoScalingGroupName']
instance_ids = [i for i in asg['Instances']]
running_instances = ec2.instances.filter(Filters=[{}])
for instance in running_instances:
print(instance.private_ip_address)
if __name__ == '__main__':
get_instances()