Python 如何通过使用boto传递卷id来查找ec2实例id
我想将卷id作为参数传递,然后返回python中的实例id,您需要调用descripe\u instances 您可以自己用Python过滤结果,也可以传递block-device-mapping.volume-id的过滤器Python 如何通过使用boto传递卷id来查找ec2实例id,python,amazon-web-services,amazon-ec2,boto3,Python,Amazon Web Services,Amazon Ec2,Boto3,我想将卷id作为参数传递,然后返回python中的实例id,您需要调用descripe\u instances 您可以自己用Python过滤结果,也可以传递block-device-mapping.volume-id的过滤器 卷一次只能附加到一个实例,因此此代码假定只返回一个实例。正如@Rajesh指出的,更简单的方法是使用DescribeVolumes,它返回附件信息: import boto3 ec2_client = boto3.client('ec2', region_name='ap
卷一次只能附加到一个实例,因此此代码假定只返回一个实例。正如@Rajesh指出的,更简单的方法是使用DescribeVolumes,它返回附件信息:
import boto3
ec2_client = boto3.client('ec2', region_name='ap-southeast-2')
response = ec2_client.describe_volumes(VolumeIds=['vol-deadbeef'])
print(response['Volumes'][0]['Attachments'][0]['InstanceId'])
此代码假定该实例是卷上的第一个附件,因为EBS卷只能附加到一个实例。您还可以对DescribeVolumes执行API调用,该调用将返回包含InstanceID的附件信息。
import boto3
ec2_client = boto3.client('ec2', region_name='ap-southeast-2')
response = ec2_client.describe_volumes(VolumeIds=['vol-deadbeef'])
print(response['Volumes'][0]['Attachments'][0]['InstanceId'])