Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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 如何通过使用boto传递卷id来查找ec2实例id_Python_Amazon Web Services_Amazon Ec2_Boto3 - Fatal编程技术网

Python 如何通过使用boto传递卷id来查找ec2实例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

我想将卷id作为参数传递,然后返回python中的实例id,您需要调用descripe\u instances

您可以自己用Python过滤结果,也可以传递block-device-mapping.volume-id的过滤器


卷一次只能附加到一个实例,因此此代码假定只返回一个实例。

正如@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'])