gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:44:43 25 4
gpt4 key购买 nike

我正在尝试将安装在/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
}
]
}

我尝试使用示例中给出的 describe volume 的 --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
}
]

任何想法将不胜感激。

最佳答案

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

输出:

vol-096ca253d37b3e42b

关于aws-cli - 如何使用 aws cli 为特定实例获取安装在/dev/sdf 的设备的 volumeid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44568898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com