AWS CLI 筛选为特定 AMI 名称的 return 个最新 AMI 的唯一列表

AWS CLI filter to return unique list of the latests AMIs for a particular AMI name

我想使用 AWS CLI 生成仅包含最新 AMI 名称的唯一列表

这是我当前的命令,其中 returns 每个服务的多个 AMI 名称:

aws ec2 describe-images --owners "<ACCOUNT_ID>" --filters "Name=name,Values=${ami_name}*"  --query 'reverse(sort_by(Images, &CreationDate))[*].Name' --output table

示例输出:

-------------------------------------------
|             DescribeImages              |
+-----------------------------------------+
|  service_abc_500 (latest)               |
|  service_xyz_350 (latest)               |
|  service_abc_499                        |
|  service_abc_498                        |
|  service_xyz_349                        |

期望的输出:

-------------------------------------------
|             DescribeImages              |
+-----------------------------------------+
|  service_abc_500                        |
|  service_xyz_350                        |

给定服务的 AMI 名称将始终具有相同的前缀,唯一的区别是附加到 AMI 名称末尾的唯一 ID,即 _500_350

您可以在查询中使用 [-1] 而不是 [*] 来仅检索最新的项目。参见:How to filter the output with the --query option

这是文档中给出的示例:

The following example retrieves a list of images that meet several criteria. It then uses the --query parameter to sort the output by CreationDate, selecting only the most recent. Finally, it displays the ImageId of that one image.

$ aws ec2 describe-images \
    --owners amazon \
    --filters "Name=name,Values=amzn*gp2" "Name=virtualization-type,Values=hvm" "Name=root-device-type,Values=ebs" \
    --query "sort_by(Images, &CreationDate)[-1].ImageId" \
    --output text

ami-00ced3122871a4921