Skip to main content

How to Export EC2 AMIs and Snapshots List to a CSV File Using AWS CLI and Tags

How to Export EC2 AMIs and Snapshots List to a CSV File Using AWS CLI and Tags

Updated today

Overview

  • This guide provides step-by-step instructions on how to use the AWS Command Line Interface (CLI) and the jq utility to export a detailed list of Amazon Machine Images (AMIs) or EC2 Snapshots to a CSV file.

  • The commands provided will filter the resources based on a specific tag, Created by policy id, which is often used by backup applications like CloudRanger.

  • This is useful for auditing, reporting, or inventory management of backups created by a specific policy.

Prerequisites

Before you begin, ensure you have the following:

  1. AWS CLI: The AWS CLI must be installed and configured with credentials that have sufficient permissions to describe EC2 resources (e.g., ec2:DescribeImages and ec2:DescribeSnapshots).

  2. jq Utility: jq is a command-line JSON processor. It is required to format the JSON output from the AWS CLI into CSV. It is pre-installed in the AWS CloudShell environment.

  3. Backup Policy ID: You must know the specific value of the Created by policy id tag you wish to filter by (e.g., 5125).

Procedure

You can run these commands from your local terminal (if AWS CLI and jq are installed) or directly from the AWS CloudShell in the AWS console.

Part 1: Exporting AMIs to CSV

This command queries all AMIs in your account, filters them by the policy ID tag, and formats the output into a CSV file.

Command:

aws ec2 describe-images \
--owners self \
--filters "Name=tag:Created by policy id,Values=<mention_backup policy_id>" \
--query "Images[*]" \
--output json | jq -r '
["ImageId","Name","State","CreationDate","Platform","Architecture","Tags"],
(.[] | [
.ImageId,
.Name,
.State,
.CreationDate,
(.Platform // "Linux/UNIX"),
.Architecture,
(if .Tags then (.Tags | map("\(.Key)=\(.Value)") | join("; ")) else "" end)
]) | @csv' > amis_with_tags.csv

How to Use:

  1. Copy the command above.

  2. Replace YOUR_POLICY_ID in two places:

    1. In the filter: Values=YOUR_POLICY_ID

    2. In the output filename: amis_YOUR_POLICY_ID_with_tags.csv

  3. Example: If your policy ID is 5125, the command would be:
    aws ec2 describe-images \
    --owners self \
    --filters "Name=tag:Created by policy id,Values=5125" \
    --query "Images[*]" \
    --output json | jq -r '...' > amis_5125_with_tags.csv

  4. Run the command. A CSV file named amis_5125_with_tags.csv will be created.

Part 2: Exporting Snapshots to CSV

This command follows the same logic as Part 1 but queries for EC2 snapshots instead of AMIs.

Command:

aws ec2 describe-snapshots \

--owner-ids self \

--filters "Name=tag:Created by policy id,Values=YOUR_POLICY_ID" \

--query "Snapshots[*]" \

--output json | jq -r '

["SnapshotId","VolumeId","StartTime","State","VolumeSize","Tags"],

(.[] | [

.SnapshotId,

.VolumeId,

.StartTime,

.State,

.VolumeSize,

(if .Tags then (.Tags | map("\(.Key)=\(.Value)") | join("; ")) else "" end)

]) | @csv' > snapshots_YOUR_POLICY_ID_with_tags.csv

How to Use:

  1. Copy the command above.

  2. Replace YOUR_POLICY_ID in two places:

    1. In the filter: Values=YOUR_POLICY_ID

    2. In the output filename: snapshots_YOUR_POLICY_ID_with_tags.csv

  3. Example: If your policy ID is 5125, the command would be:

    aws ec2 describe-snapshots \

    --owner-ids self \

    --filters "Name=tag:Created by policy id,Values=5125" \

    --query "Snapshots[*]" \

    --output json | jq -r '...' > snapshots_5125_with_tags.csv

  4. Run the command. A CSV file named snapshots_5125_with_tags.csv will be created.

Part 3: Downloading the File from AWS CloudShell

If you ran these commands in AWS CloudShell, follow these steps to download the generated CSV file to your local computer:

  1. After the command finishes, click the "Actions" button in the top-right corner of the CloudShell terminal.

  2. Select "Download file" from the dropdown menu.

  3. In the "Download file" dialog box, enter the full path to your file. The path will simply be the filename you specified (e.g., amis_5125_with_tags.csv or snapshots_5125_with_tags.csv).

  4. Click the "Download" button. The file will be downloaded by your browser.

Did this answer your question?