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 オペレーションを使用して、タイムスタンプが 1 か月以上経過した (--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 Group が all に等しいすべてのスナップショットを一覧表示しています。
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 が optimizing の値に設定されている全ボリュームをリストします。
$ 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
全リージョンのどのインスタンスにも接続されていないボリュームをすべてリストする
このコマンド例では、全リージョンで status が available に設定されているボリュームをリストします。
$ 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
すべてのリージョン「エラー」状態にあるすべてのボリュームを検索する
次のコマンド例では、すべてのリージョンで status が error に設定されているすべてのボリュームを取得します。
$ 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 コマンドラインインターフェイス