【发布时间】:2018-05-13 12:29:23
【问题描述】:
如何使用 boto3 列出给定 ASG 的所有实例(id 和 IP)?如果您有一个工作示例,请告诉我。
【问题讨论】:
-
到目前为止你写了什么代码?
标签: python python-3.x amazon-ec2 boto3 autoscaling
如何使用 boto3 列出给定 ASG 的所有实例(id 和 IP)?如果您有一个工作示例,请告诉我。
【问题讨论】:
标签: python python-3.x amazon-ec2 boto3 autoscaling
我使用此代码打印实例 ID 和私有 IP 地址 来自 ASG。希望对你有帮助。
asg_client = boto3.client('autoscaling',aws_access_key_id=acc_key,aws_secret_access_key=sec_key,region_name='us-west-2')
ec2_client = boto3.client('ec2',aws_access_key_id=acc_key,aws_secret_access_key=sec_key,region_name='us-west-2')
asg = "YOUR_ASG_NAME"
print asg
asg_response = asg_client.describe_auto_scaling_groups(AutoScalingGroupNames=[asg])
instance_ids = [] # List to hold the instance-ids
for i in asg_response['AutoScalingGroups']:
for k in i['Instances']:
instance_ids.append(k['InstanceId'])
ec2_response = ec2_client.describe_instances(
InstanceIds = instance_ids
)
print instance_ids #This line will print the instance_ids
private_ip = [] # List to hold the Private IP Address
for instances in ec2_response['Reservations']:
for ip in instances['Instances']:
private_ip.append(ip['PrivateIpAddress'])
print "\n".join(private_ip)
【讨论】: