0

I want to list volume's snapshots but in the output I would like to also see the Name of that volume (I mean tag). So far I was using:

aws ec2 describe-snapshots

And in the reply I got something like:

Snapshots: [
    {
        "Description": "some description",
        "Encrypted": false,
        "OwnerId": "someownerid",
        "Progress": "100%",
        "SnapshotId": "snap-example",
        "StartTime": "start time",
        "State": "completed",
        "VolumeId": "volume id",
        "VolumeSize": 32
    }
]

But what I would like to have in that output is also a volume name:

Snapshots: [
    {
        "Description": "some description",
        "Encrypted": false,
        "OwnerId": "someownerid",
        "Progress": "100%",
        "SnapshotId": "snap-example",
        "StartTime": "start time",
        "State": "completed",
        "VolumeId": "volume id",
        "VolumeSize": 32,
        "VolumeName": "Volume Name" #additional key:val
    }
]
4
  • 1
    There is no VolumeName attribute. Are you referring to Name tag of a Volume? Commented Aug 1, 2019 at 15:21
  • 1
    You'll have to merge the results of calling the following for each snapshot: aws ec2 describe-tags --filters Name=resource-id,Values=snap-1234. Or consider writing a small script in Python. Commented Aug 1, 2019 at 15:51
  • 1
    @jarmod the filter will give you results only for the snapshots that match the Value. And the actual CLI output includes tags, so see my answer below to see how we can extract those. Commented Aug 1, 2019 at 19:38
  • @Yogesh_D You're right, thanks. I saw zero tags in my describe-snapshots output and assumed they weren't being reported. Commented Aug 1, 2019 at 20:09

2 Answers 2

1

The aws ec2 describe-snapshots does return tags on snapshots if they are present. Something similar to this:

{
    "Description": "This snapshot is created by the AWS Backup service.",
    "Tags": [
        {
            "Value": "On",
            "Key": "Backup"
        },
        {
            "Value": "Jenkins_Machine",
            "Key": "Name"
        },
        {
            "Value": "*********",
            "Key": "aws:backup:source-resource"
        }
    ],
    "Encrypted": false,
    "VolumeId": "vol-*****",
    "State": "completed",
    "VolumeSize": 250,
    "StartTime": "2019-08-01T11:29:31.654Z",
    "Progress": "100%",
    "OwnerId": "******",
    "SnapshotId": "snap-******"
}

To be able to see the name (Assuming your snapshots have them) do this:

aws ec2 describe-snapshots --snapshot-id snap-**** --query 'Snapshots[*].{Description:Description,Name:Tags[?Key==`Name`].Value|[0],State:State}'

This should give you output like this:

[
    {
        "State": "completed", 
        "Description": "This snapshot is created by the AWS Backup service.", 
        "Name": "Jenkins_Machine"
    }
]

The fields are curtailed but you can add fields that you need at the end of the query like this ...State:State,VolumeId:VolumeId}, where I have newly added the VolumeId.

If you remove the --snapshot-id parameter, the above command should return you all snapshots, however for snapshots that don't have the Name tag its going to print null.

Edit: As @krishna_mee2004 pointed out, the OP is probably looking for snapshots for a particular volume. If that is the case you can still do it using this command. The filters option can be used to filter based on volume ID.

aws ec2 describe-snapshots --filters Name=volume-id,Values=vol-***** --query 'Snapshots[*].{Description:Description,Name:Tags[?Key==`Name`].Value|[0],State:State,VolumeId:VolumeId}'

Sign up to request clarification or add additional context in comments.

2 Comments

I believe they want the name of the volume from which the snapshot was created. In that case, it will require at least 2 commands (describe-snapshots and describe-volumes) to get all the info. And script should be able to work if the volume does not exist. An easier approach for this would be writing the script in a Programming language like Python or NodeJS.
@krishna_mee2004, on re reading the question maybe you are correct. Let me updation the answer, you can still do this using one command.
1

If you are referring to snapshot's name tag, you can write a simple python or ruby script using aws sdk. For example, a ruby code to list the snapshot id and value of its name tag will look like this:

require 'aws-sdk'

# provide region and credentials in parameter 
ec2 = Aws::EC2::Client.new

# paginate if you have a big list
resp = ec2.describe_snapshots

# iterate snapshots
resp.snapshots.each do |snapshot|
    # iterate tags and print if it has a name tag.
    snapshot.tags.each do |tag|
        # print whatever is required/available in the response structure
        puts "#{snapshot.snapshot_id} has the name tag with value #{tag.value}" if tag.key.casecmp? 'Name'
    end
end 

Refer the respective language api documentation to understand more about the usage of the sdk and the api calls. Make sure to setup the sdk before using and it varies based on the language you choose. For example, steps for setting up the ruby sdk is outlined here. You may also want to checkout the API reference for describe_snaphots used in the above code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.