Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python boto3从给定ASG中提取所有EC2实例_Python_Python 3.x_Amazon Ec2_Boto3_Autoscaling - Fatal编程技术网

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

Python boto3从给定ASG中提取所有EC2实例,python,python-3.x,amazon-ec2,boto3,autoscaling,Python,Python 3.x,Amazon Ec2,Boto3,Autoscaling,如何使用boto3列出给定ASG中的所有实例(id和IP)?请让我知道,如果你有一个工作的例子 我使用此代码打印实例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

如何使用boto3列出给定ASG中的所有实例(id和IP)?请让我知道,如果你有一个工作的例子

我使用此代码打印实例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)