12

i want to get volume id attached to instance like how we get instance id from the meta data

InstanceID=`curl http://169.254.169.254/latest/meta-data/instance-id

3 Answers 3

12

The volumes IDs are not available from the metadata. The virtual devices are available under block-device-mapping/

You will need to use the AWS CLI (or script, program, etc.) to get the volume IDs assigned to an instance.

With the CLI:

aws ec2 describe-volumes

The output will include the instance ID that a volume is attached to.

Once you know the instance ID, you can filter on just those volumes attached to that instance:

aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=i-555550604eaf99999

The documentation will have more details and examples.

describe-volumes

2
  • I just want to get a particular volume id of volume with tag = "test " attach to instance id = "i-----" , thanks for your response.
    – Nani
    Dec 14, 2017 at 20:40
  • 2
    The CLI has commands to filter on tags and many items. AWS has good documentation for most common services. The CLI has extensive documentation plus there are a large number of examples on the Internet. Dec 14, 2017 at 20:47
8

In my case, I needed to retrieve volume id on the instance I only had shell access to. I needed to communicate the volume id to the customer who setup the vm.

In that case, if you don't have the aws profile, and only need to get volume id, you can use the aws ebsnvme-id tool which is preinstalled on linux distros (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html)

So f.ex.:

$ sudo /sbin/ebsnvme-id /dev/nvme1n1
Volume ID: vol-c26087a73f2293647
sdb
3

The answer is already given but I was looking for current instance attach volume from which I am running query.

So you can just pass the instance Id from the curl response.

aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)

sample output

{
    "Volumes": [
        {
            "AvailabilityZone": "us-west-2b", 
            "Attachments": [
                {
                    "AttachTime": "2018-03-01T07:47:43.000Z", 
                    "InstanceId": "i-abcdedf", 
                    "VolumeId": "vol-12345", 
                    "State": "attached", 
                    "DeleteOnTermination": true, 
                    "Device": "/dev/xvda"
                }
            ], 
            "Encrypted": false, 
            "VolumeType": "gp2", 
            "VolumeId": "vol-123456", 
            "State": "in-use", 
            "Iops": 450, 
            "SnapshotId": "snap-1234", 
            "CreateTime": "2018-03-01T07:47:43.261Z", 
            "Size": 150
        }
    ]
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .