boto3 从给定的 ASG 中提取所有 EC2 实例

boto3 to pull all EC2 instances from a given ASG

如何使用 boto3 列出给定 ASG 的所有实例(id 和 IP)?如果您有一个有效的示例,请告诉我。

I use this code to print the Instance IDs and the Private IP address from the ASG. I hope it helps.

 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)