Python boto3从给定ASG中提取所有EC2实例
如何使用boto3列出给定ASG中的所有实例(id和IP)?请让我知道,如果你有一个工作的例子 我使用此代码打印实例ID和私有IP地址 来自ASG。我希望有帮助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
到目前为止,您编写了什么代码?
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)