Reputation: 1
I want to pass the volume id as a parameter which then returns the instance id in python
Upvotes: 0
Views: 1113
Reputation: 270224
As @Rajesh pointed out, an easier way is to use DescribeVolumes
, which returns Attachment
information:
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'])
This code assumes that the instance is the first attachment on the volume (since EBS volumes can only be attached to one instance).
Upvotes: 0
Reputation: 270224
You will need to call describe_instances()
.
You can either filter the results yourself in Python, or pass Filters
for block-device-mapping.volume-id
.
import boto3
ec2_client = boto3.client('ec2', region_name='ap-southeast-2')
response = ec2_client.describe_instances(Filters=[{'Name':'block-device-mapping.volume-id','Values':['vol-deadbeef']}])
instance_id = response['Reservations'][0]['Instances'][0]['InstanceId']
print(instance_id)
A volume can only be attached to one instance at a time, so this code assumes only one instance is returned.
Upvotes: 1