如何使用aws cli为特定实例获取在/ dev / sdf上安装的设备的volumeid?

时间:2017-06-15 13:30:53

标签: aws-cli

我正在尝试将/ dev / sdf中安装的设备的卷ID获取到特定实例。

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

输出:

{
    "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
        }
    ]
}

我尝试过在示例中给出的描述卷的--query选项然后我得到了输出。

命令:

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"
    }
]

然后我试图获取附件,我得到了以下错误。

命令:

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

输出:

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

任何想法都会受到赞赏。

2 个答案:

答案 0 :(得分:4)

我使用--filters选项使用相同的命令完成了相同的任务。

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

输出:

vol-096ca253d37b3e42b

答案 1 :(得分:0)

另一种使用描述实例的方法

{{1}}