I want to use the AWS Command Line Interface (AWS CLI) to list Amazon Elastic Block Store (Amazon EBS) volume or snapshot information for my Amazon Elastic Compute Cloud (Amazon EC2) instance.
Resolution
Note: If you receive errors when you run AWS CLI commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.
Before you run the following commands, first install the jq processor. For more information, see ./jq on the jq website.
To install the jq processor for Amazon Linux and Amazon Linux 2, run the following command.
$ sudo yum install jq
For other Linux distribution installation instructions and syntax commands, see the documentation for your Linux distribution.
Find all Amazon EBS snapshots over one month old
The following command uses the describe-snapshots operation to list all EBS snapshots with a timestamp that's older than one month ( --date='-1 month') from the current date.
aws ec2 describe-snapshots \
--owner-ids self \
--query "Snapshots[?(StartTime<='$(date --date='-1 month' '+%Y-%m-%d')')].{ID:SnapshotId,Time:StartTime,Details:Description}"
List EBS snapshots over one month old in all Regions
The following example command uses the same command as the previous example. It also uses the describe-regions operation to loop through EBS snapshots in all 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
Find all publicly available EBS snapshots in an AWS account, in all Regions
This example command lists all EBS 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 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
The following example command uses the describe-volumes-modifications operation to list all EBS volumes where the modification-state value is set to 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 EBS volumes not attached to any instance in all Regions
This example command lists EBS 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 EBS volumes in the "error" state in all Regions
The following example command describes all EBS 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
AWS Command Line Interface