AWS CLI describe-snapshots 打印 SnapshotId

AWS CLI describe-snapshots to print SnapshotId

我正在尝试使用 bash 打印最新快照的 SnapshotId。这是我的命令:

aws ec2 describe-snapshots | grep TestVolume1 |head -n 1| > Output.txt

以上结果匹配 TestVolume1 写入 Output.txt 最新快照。我还想打印 SnapshotID 并努力将其输出到 Output.txt.

我已经尝试了 awk--filter,但没有用。非常感谢任何有关语法输出 SnapshotId 的帮助。我还能用什么?

您可以使用 --filter 参数仅检索匹配的快照并使用 --query 参数从响应中解析所需字段,

更新(根据快照开始时间添加反向排序的快照):

aws ec2 describe-snapshots --filters Name=description,Values="*TestVolume1*" --query "reverse(sort_by(Snapshots, &StartTime))[0].SnapshotId"

过滤器应用于快照的描述,希望它包含所需的文本 (TestVolume1)。