How do I list all of my Amazon EBS snapshots with or without a specified key tag using the AWS CLI?

3 minute read
0

I want to use the AWS Command Line Interface (AWS CLI) to list all my Amazon Elastic Block Store (Amazon EBS) snapshots. What commands are a best practice to use when I want to list all snapshots, with or without a specified tag key?

Resolution

Note: Before beginning this resolution, install and configure the AWS CLI.

If you receive errors when running AWS CLI commands, make sure that you’re using the most recent version of the AWS CLI.

List all EBS snapshots in a particular Region

The following example command lists all EBS snapshots using the describe-snapshots operation in the Region us-east-1:

aws ec2 describe-snapshots --owner-ids self  --query 'Snapshots[]' --region=us-east-1

The following is example output for the describe-snapshots command:

Created for policy: policy-08843cf0d7f6189ae schedule: Default Schedule    False    111122223333    100%    snap-091e33a177cb2e49b    2020-09-10T19:27:07.882Z    completed    vol-03b223394ea08e690    8
TAGS    instance-id    i-0919c4d810b9c3695
TAGS    dlm:managed    true
TAGS    timestamp    2020-09-10T19:27:07.548Z
TAGS    aws:dlm:lifecycle-policy-id    policy-08843cf0d7f6189ae
TAGS    aws:dlm:lifecycle-schedule-name    Default Schedule

test one hellop    False    111122223333    100%    snap-02faf8ffc48e512f4    2020-09-10T19:17:34.974Z    completed    vol-03b223394ea08e690    8 
TAGS    ec2-console    false

Created for policy: policy-08843cf0d7f6189ae schedule: Default Schedule    False    111122223333    100%    snap-007e74c24d8f3aaf1    2020-09-10T17:28:31.993Z    completed    vol-03b223394ea08e690    8
TAGS    instance-id    i-0919c4d810b9c3695
TAGS    dlm:managed    true
TAGS    aws:dlm:lifecycle-schedule-name    Default Schedule
TAGS    timestamp    2020-09-10T17:28:31.650Z
TAGS    aws:dlm:lifecycle-policy-id    policy-08843cf0d7f6189ae

test one     False    111122223333    100%    snap-00f20d2d2c17bbea0    2020-09-08T07:47:47.660Z    completed    vol-062b2c633c981f99e    8 
TAGS    ec2-console    true

Filter the list of EBS snapshots for a specified tag key

The following command lists EBS snapshots using the describe-snapshots operation with a specified tag key:

aws ec2 describe-snapshots  --owner-ids self  --query 'Snapshots[?(Tags[?Key == `name`].Value)]'

The following command lists all snapshots with the tag key ec2-console:

$ aws ec2 describe-snapshots --owner-ids self --query 'Snapshots[?(Tags[?Key == `ec2-console`].Value)]'

The following is an example output for the preceding command:

test one hellop    False    111122223333    100%    snap-02faf8ffc48e512f4    2020-09-10T19:17:34.974Z    completed    vol-03b223394ea08e690    8 
TAGS    ec2-console    false

test one     False    111122223333    100%    snap-00f20d2d2c17bbea0    2020-09-08T07:47:47.660Z    completed    vol-062b2c633c981f99e    8 
TAGS    ec2-console    true

Filter the list of EBS snapshots for snapshots that don't have a specified tag key

The following command lists EBS snapshots that don't have a specified tag key:

aws ec2 describe-snapshots --owner-ids self --query 'Snapshots[?!not_null(Tags[?Key == `name`].Value)]'

The following example command filters the list of EBS snapshots for all snapshots that don't have the tag key ec2-console:

$ aws ec2 describe-snapshots --owner-ids self --query 'Snapshots[?!not_null(Tags[?Key == `ec2-console`].Value)]'

The following is an example output for the preceding command:

Created for policy: policy-08843cf0d7f6189ae schedule: Default Schedule    False    111122223333    100%    snap-091e33a177cb2e49b    2020-09-10T19:27:07.882Z    completed    vol-03b223394ea08e690    8
TAGS    instance-id    i-0919c4d810b9c3695
TAGS    dlm:managed    true
TAGS    timestamp    2020-09-10T19:27:07.548Z
TAGS    aws:dlm:lifecycle-policy-id    policy-08843cf0d7f6189ae
TAGS    aws:dlm:lifecycle-schedule-name    Default Schedule

Created for policy: policy-08843cf0d7f6189ae schedule: Default Schedule    False    111122223333    100%    snap-007e74c24d8f3aaf1    2020-09-10T17:28:31.993Z    completed    vol-03b223394ea08e690    8
TAGS    instance-id    i-0919c4d810b9c3695
TAGS    dlm:managed    true
TAGS    aws:dlm:lifecycle-schedule-name    Default Schedule
TAGS    timestamp    2020-09-10T17:28:31.650Z
TAGS    aws:dlm:lifecycle-policy-id    policy-08843cf0d7f6189ae

Related information

Tagging AWS resources

AWS OFFICIAL
AWS OFFICIALUpdated a year ago