如何使用 AWS CLI 列出 Amazon EC2 实例的 Amazon EBS 卷或快照信息?

2 分钟阅读
0

如何使用 AWS Command Line Interface (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 yum install jq

如需其他 Linux 发行版的安装说明和语法命令,请参阅您的 Linux 发行版的文档。

查找超过 1 个月的所有快照

以下命令使用 describe-snapshots 操作列出所有时间戳早于一个月 (--date='-1 month') 的 EBS 快照。

aws ec2 describe-snapshots \
    --owner-ids self \
    --query "Snapshots[?(StartTime<='$(date --date='-1 month' '+%Y-%m-%d')')].{ID:SnapshotId,Time:StartTime,Details:Description}"

列出所有区域中超过 1 个月的快照

以下示例命令使用与第一个示例相同的命令。它还将使用 describe-regions 操作循环检查所有区域的快照。

for REGION in $(aws ec2 describe-regions --output text --query 'Regions[].[RegionName]') ; do echo $REGION && aws ec2 describe-snapshots --owner self --region $REGION --output json --query "Snapshots[?(StartTime<='$(date --date='-1 month' '+%Y-%m-%d')')].{ID:SnapshotId,Time:StartTime,Details:Description}" ; done

查找所有区域的 AWS 账户中所有公开提供的快照

此示例命令列出所有区域中,CreateVolumePermission 等于全部的所有快照。

for REGION in $(aws ec2 describe-regions --output text --query 'Regions[].[RegionName]') ; do echo "$REGION:"; for snap in $(aws ec2 describe-snapshots --owner self --output text --region $REGION --query 'Snapshots[*].SnapshotId'); do aws ec2 describe-snapshot-attribute --snapshot-id $snap --region $REGION --output text --attribute createVolumePermission --query '[SnapshotId,CreateVolumePermissions[?Group == `all`]]'; done; echo; done

以下示例命令通过使用 describe-volumes-modifications 操作列出所有区域中,modification-state 的值被设为正在优化的所有卷。

$ for REGION in $(aws ec2 describe-regions --output text --query 'Regions[].[RegionName]') ; 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[].[RegionName]') ; 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[].[RegionName]') ; 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

相关信息

AWS Command Line Interface

AWS 官方
AWS 官方已更新 2 年前