关于亚马逊网络服务:AWS CLI describe-snapshots 打印 SnapshotId

AWS CLI describe-snapshots to print SnapshotId

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

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

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

我已经尝试过 awk--filter 但这没有帮助。将不胜感激任何有关输出 SnapshotId 语法的帮助。我还能用什么?


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

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

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

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