How can I list Amazon EBS volume or snapshot information for my Amazon EC2 instance by using the AWS CLI?
Last updated: 2021-01-18
How can I list Amazon Elastic Block Store (Amazon EBS) volume or snapshot information for my Amazon Elastic Compute Cloud (Amazon EC2) instance using the AWS Command Line Interface (AWS CLI)?
Resolution
Note: If you receive errors when running AWS CLI commands, make sure that you’re using the most recent version of the AWS CLI.
Note: Install the jq processor before running the commands.
Amazon Linux and 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
For other distributions, see jq Command Line Processor on the GitHub website for download and installation instructions.
Find all snapshots over one month old
The following command lists all EBS snapshots using the describe-snapshots operation, where the timestamp is older than one month ( --date='-1 month').
$ aws ec2 describe-snapshots --owner self --output json | jq '.Snapshots[] | select(.StartTime < "'$(date --date='-1 month' '+%Y-%m-%d')'") | [.Description, .StartTime, .SnapshotId]'
List snapshots over 1 month old in all Regions
The following example command uses the same command as in the first example. It also loops through snapshots in all Regions using the describe-regions operation.
$ 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 | jq '.Snapshots[] | select(.StartTime < "'$(date --date='-1 month' '+%Y-%m-%d')'") | [.Description, .StartTime, .SnapshotId]' ; done
Find all publicly available snapshots in an AWS account in all Regions
This example command lists all snapshots where the CreateVolumePermission Group is equal to all, for all Regions.
$ 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 json --region $REGION --query 'Snapshots[*].SnapshotId' | jq -r '.[]'); do aws ec2 describe-snapshot-attribute --snapshot-id $snap --region $REGION --output json --attribute createVolumePermission --query '[SnapshotId,CreateVolumePermissions[?Group == `all`]]' | jq -r '.[]'; done; echo; done
Get the status of all volumes currently in the optimizing stage (after volume modification) in all Regions
The following example command lists all volumes by using the describe-volumes-modifications operation, where the modification-state is set to the value of optimizing, for all Regions.
$ 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
Find all volumes not attached to any instance in all Regions
This example command lists volumes where the status is set to available, for all Regions.
$ 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
Find all volumes in the "error" state in all Regions
The following example command describes all volumes, in all Regions, where the status is set to 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
Related information
Did this article help?
Do you need billing or technical support?