3

I am trying to get the volume id of the device mounted at /dev/sdf to a particular instance.

Command: aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=$instance_id_main --region us-west-2 --output json

Output:

{
    "Volumes": [
        {
            "AvailabilityZone": "us-west-2a", 
            "Attachments": [
                {
                    "AttachTime": "2017-06-15T12:59:18.000Z", 
                    "InstanceId": "i-073cfdf5832e5a7ab", 
                    "VolumeId": "vol-096ca253d37b3e42b", 
                    "State": "attached", 
                    "DeleteOnTermination": false, 
                    "Device": "/dev/sdf"
                }
            ], 
            "Tags": [
                {
                    "Value": "NewVolume", 
                    "Key": "Name"
                }
            ], 
            "Encrypted": false, 
            "VolumeType": "gp2", 
            "VolumeId": "vol-096ca253d37b3e42b", 
            "State": "in-use", 
            "Iops": 100, 
            "SnapshotId": "", 
            "CreateTime": "2017-06-15T12:39:06.687Z", 
            "Size": 5
        }, 
        {
            "AvailabilityZone": "us-west-2a", 
            "Attachments": [
                {
                    "AttachTime": "2017-06-15T12:57:46.000Z", 
                    "InstanceId": "i-073cfdf5832e5a7ab", 
                    "VolumeId": "vol-0189e6a20392bb709", 
                    "State": "attached", 
                    "DeleteOnTermination": true, 
                    "Device": "/dev/sda1"
                }
            ], 
            "Tags": [
                {
                    "Value": "NewTesting", 
                    "Key": "Name"
                }
            ], 
            "Encrypted": false, 
            "VolumeType": "gp2", 
            "VolumeId": "vol-0189e6a20392bb709", 
            "State": "in-use", 
            "Iops": 100, 
            "SnapshotId": "snap-0a642b1f5be55819a", 
            "CreateTime": "2017-06-15T12:57:46.027Z", 
            "Size": 8
        }
    ]
}

I have tried with --query option of describe volume given in the example then I got below output.

Command:

aws ec2  describe-volumes --filters Name=attachment.instance-id,Values=$instance_id_main --query 'Volumes[*].{ID:VolumeId,Tag:Tags}' --region us-west-2 --output json

[
    {
        "Tag": [
            {
                "Value": "NewVolume", 
                "Key": "Name"
            }
        ], 
        "ID": "vol-096ca253d37b3e42b"
    }, 
    {
        "Tag": [
            {
                "Value": "NewTesting", 
                "Key": "Name"
            }
        ], 
        "ID": "vol-0189e6a20392bb709"
    }
]

Then I tried to get the Attachments and I got below error.

Command:

aws ec2  describe-volumes --filters Name=attachment.instance-id,Values=$instance_id_main --query 'Volumes[*].{Attachments:Device[*]}' --region us-west-2 --output text

Output:

[
    {
        "Attachments": null
    }, 
    {
        "Attachments": null
    }
]

Any idea would be appreciated.

3 Answers 3

7
die() { status=$1; shift; echo "FATAL: $*"; exit $status; }
EC2_INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id || die \"wget instance-id has failed: $?\"`"
EC2_AWSAVZONE=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
EC2_REGION=${EC2_AWSAVZONE::-1}

DATA_VOLUME_ID="`aws ec2 describe-volumes  --filters Name=attachment.device,Values=/dev/sdf Name=attachment.instance-id,Values=$EC2_INSTANCE_ID --query 'Volumes[*].{ID:VolumeId}' --region $EC2_REGION --output text`"

echo $DATA_VOLUME_ID

Output:

vol-096ca253d37b3e42b
0

Another method using describe-instances

#query using instance name
aws ec2 describe-instances --query 'Reservations[].Instances[].BlockDeviceMappings[?DeviceName==`/dev/sdf`].[Ebs.VolumeId]' --filters Name=tag:Name,Values=${INSTANCE_NAME} --output=text

#query using instance-id
aws ec2 describe-instances --instance-ids ${INSTANCE_ID} --query 'Reservations[].Instances[].BlockDeviceMappings[?DeviceName==`/dev/sdf`].[Ebs.VolumeId]' --output=text
0

If you're on Nitro you can use the ebsnvme-id utility to get the volume ID from a device like this:

awk '{print $3'} < <(/sbin/ebsnvme-id  --volume /dev/nvme1n1)

Bonus:

You can get the device mounted at a specific mountpoint with findmnt like this:

findmnt -noheadings --output SOURCE --target /mnt/foo

Put all this together, and you can get the EBS Volume ID of the volume mounted at /mnt/foo like this:

awk '{print $3}' < <(
  /sbin/ebsnvme-id  --volume "$(
    findmnt -noheadings --output SOURCE --target /mnt/foo
  )"
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.