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.
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