上次更新日期:2021 年 1 月 18 日
如何使用 AWS 命令行界面 (AWS CLI) 列出我的 Amazon Elastic Compute Cloud (Amazon EC2) 的 Amazon Elastic Block Store (Amazon EBS) 卷或快照信息?
解决方法注意:如果在运行 AWS CLI 命令时遇到错误,请确保您使用的是最新版本的 AWS CLI。
注意:在运行命令之前安装 jq 处理器。
Amazon Linux 和 Amazon Linux 2:
$ sudo wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
$ sudo chmod +x ./jq
$ sudo cp jq /usr/bin
有关其他分配,请参阅 GitHub 网站上的 jq 命令行处理器,以获取下载和安装说明。
查找超过 1 个月的所有快照以下命令使用 describe-snapshots 操作列出所有 EBS 快照,其中的时间戳早于一个月 (--date='-1 month')。
$ aws ec2 describe-snapshots --owner self --output json | jq '.Snapshots[] | select(.StartTime < "'$(date --date='-1 month' '+%Y-%m-%d')'") | '
列出所有区域中超过 1 个月的快照
以下示例命令使用与第一个示例相同的命令。它还将使用 describe-regions 操作循环检查所有区域的快照。
$ for REGION in $(aws ec2 describe-regions --output text --query 'Regions[].') ; do echo $REGION && aws ec2 describe-snapshots --owner self --region $REGION --output json | jq '.Snapshots[] | select(.StartTime < "'$(date --date='-1 month' '+%Y-%m-%d')'") | ' ; done
查找所有区域的 AWS 账户中所有公开提供的快照
此示例命令列出了所有快照,对于所有区域,CreateVolumePermission 组等于所有。
$ for REGION in $(aws ec2 describe-regions --output text --query 'Regions[].') ; do echo "$REGION:"; for snap in $(aws ec2 describe-snapshots --owner self --output json --region $REGION --query 'Snapshots.SnapshotId' | jq -r '.[]'); do aws ec2 describe-snapshot-attribute --snapshot-id $snap --region $REGION --output json --attribute createVolumePermission --query ']' | jq -r '.[]'; done; echo; done
获取所有区域中当前处于优化阶段(卷修改后)的所有卷的状态
以下示例命令通过使用 describe-volumes-modifications 操作列出所有卷,对于所有区域,modification-state 被设置为 optimizing 值。
$ for REGION in $(aws ec2 describe-regions --output text --query 'Regions[].') ; do echo $REGION && aws ec2 describe-volumes-modifications --query 'VolumesModifications[].{VolumeID:VolumeId,TargetSize:TargetSize,OriginalSize:OriginalSize,Progress:Progress,OriginalIops:OriginalIops,TargetIops:TargetIops}' --output json --filter 'Name=modification-state,Values=optimizing' --region $REGION; done
查找未附加到所有区域中任何实例的所有卷
此示例命令列出所有区域中状态被设为可用的卷。
$ for REGION in $(aws ec2 describe-regions --output text --query 'Regions[].') ; do echo $REGION && aws ec2 describe-volumes --filter "Name=status,Values=available" --query 'Volumes.{VolumeID:VolumeId,Size:Size,Type:VolumeType,AvailabilityZone:AvailabilityZone}' --region $REGION; done
查找所有区域中处于“错误”状态的所有卷
以下示例命令描述了所有区域中状态被设置为错误的所有卷。
$ for REGION in $(aws ec2 describe-regions --output text --query 'Regions[].') ; do echo $REGION && aws ec2 describe-volumes --filter "Name=status,Values=error" --query 'Volumes.{VolumeID:VolumeId,Size:Size,Type:VolumeType,AvailabilityZone:AvailabilityZone}' --region $REGION; done