EC2 Snapshot Management: How to get AWS EC2 Snapshot Information with Python

Oluwasegun Adedigba - Jun 20 - - Dev Community

Introduction

Amazon Elastic Compute Cloud (EC2) snapshots are integral to data backup and disaster recovery strategies within AWS. They provide point-in-time copies of your EC2 instance volumes, allowing you to restore data quickly and reliably in the event of failures, data loss, or system corruption. As organizations scale their cloud infrastructure, managing these snapshots becomes increasingly complex and time-consuming. Automation is the key to simplifying this process, ensuring data integrity, and optimizing operational efficiency.

In this blog post, we'll walk through a Python script that automates the extraction of snapshot information, including associated instance details. This script exports the gathered data to a CSV file for easy analysis and documentation. By leveraging this automated approach, you can streamline your workflow, maintain a robust backup strategy, and gain valuable insights into your AWS environment.

Prerequisites

Before diving into the script, ensure you have the following prerequisites:

  1. AWS Account: You need an active AWS account with EC2 instances and associated snapshots.
  2. AWS CLI and Boto3: The AWS Command Line Interface (CLI) and Boto3 (the AWS SDK for Python) should be installed and configured on your machine.
  3. Python Environment: Make sure you have Python installed on your local machine.
  4. IAM Permissions: The IAM user or role you use must have the necessary permissions to describe EC2 instances and snapshots. Typically, AmazonEC2ReadOnlyAccess is sufficient.

Setting Up AWS CLI and Boto3

First, install the AWS CLI and Boto3. Open your terminal and run:

pip install awscli boto3
Enter fullscreen mode Exit fullscreen mode

Next, configure the AWS CLI with your credentials:

aws configure
Enter fullscreen mode Exit fullscreen mode

You'll be prompted to enter your AWS Access Key ID, Secret Access Key, default region, and output format. This configuration is essential for Boto3 to interact with your AWS environment.

Automating Snapshot Information Extraction

To automate the extraction of EC2 snapshot information, we need to perform the following steps:

  1. Retrieve the names of EC2 instances.
  2. Extract EC2 instance IDs from snapshot descriptions.
  3. Gather snapshot information and export it to a CSV file.

1. Retrieving Instance Names

Each EC2 instance can have multiple tags, one of which is typically the Name tag. This tag is crucial for identifying instances more easily.

import boto3

def get_instance_name(ec2, instance_id):
    response = ec2.describe_instances(InstanceIds=[instance_id])
    for reservation in response['Reservations']:
        for instance in reservation['Instances']:
            for tag in instance.get('Tags', []):
                if tag['Key'] == 'Name':
                    return tag['Value']
    return 'N/A'
Enter fullscreen mode Exit fullscreen mode

The get_instance_name function queries AWS to describe the specified instance by its ID and iterates through the tags to find the Name tag. If the Name tag is not present, it returns 'N/A'.

2. Extracting Instance IDs from Snapshot Descriptions

Snapshots in AWS often contain the instance ID in their descriptions. We can use a regular expression to extract these IDs.

import re

def extract_instance_id(description):
    match = re.search(r'i-[a-f0-9]+', description)
    if match:
        return match.group(0)
    return 'N/A'
Enter fullscreen mode Exit fullscreen mode

The extract_instance_id function uses a regular expression to search for instance IDs (which match the pattern i-[a-f0-9]+) within the snapshot description. If a match is found, it returns the instance ID; otherwise, it returns 'N/A'.

3. Exporting Snapshot Information to CSV

Combining the previous functions, we can now gather the snapshot information and export it to a CSV file.

import csv
import boto3

def export_snapshots_info_to_csv():
    ec2 = boto3.client('ec2')  # Connect to EC2 service
    snapshots = ec2.describe_snapshots(OwnerIds=['self'])['Snapshots']

    with open('ec2_snapshots.csv', mode='w', newline='') as csv_file:
        fieldnames = ['Instance Name', 'Snapshot ID', 'Volume Size (GiB)', 'Snapshot Date Started']
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        writer.writeheader()

        for snapshot in snapshots:
            instance_id = extract_instance_id(snapshot['Description'])
            instance_name = get_instance_name(ec2, instance_id)
            snapshot_id = snapshot['SnapshotId']
            volume_size = snapshot['VolumeSize']
            snapshot_date = snapshot['StartTime'].strftime("%Y-%m-%d %H:%M:%S")

            writer.writerow({
                'Instance Name': instance_name,
                'Snapshot ID': snapshot_id,
                'Volume Size (GiB)': volume_size,
                'Snapshot Date Started': snapshot_date
            })

    print("Snapshot information has been written to ec2_snapshots.csv.")
Enter fullscreen mode Exit fullscreen mode

The export_snapshots_info_to_csv function performs the following steps:

  1. Connect to the EC2 Service: Initializes a connection to the EC2 service using Boto3.
  2. Retrieve Snapshots: Fetches a list of snapshots owned by the account.
  3. Open CSV File: Opens a CSV file for writing.
  4. Iterate Through Snapshots: For each snapshot, it extracts the instance ID, retrieves the instance name, and collects other snapshot details.
  5. Write to CSV: Writes the gathered information to the CSV file.

Running the Script

To run the script, save it to a file (e.g., ec2_snapshot_info.py) and execute it using Python:

python ec2_snapshot_info.py
Enter fullscreen mode Exit fullscreen mode

This command will generate a CSV file (ec2_snapshots.csv) in the same directory, containing detailed information about your EC2 snapshots.

Detailed Explanation of Script Components

AWS EC2 Client Initialization

The boto3.client('ec2') call initializes a client to interact with the EC2 service. This client will be used to make API calls to AWS.

ec2 = boto3.client('ec2')
Enter fullscreen mode Exit fullscreen mode

Describing Snapshots

The describe_snapshots method fetches details about the snapshots. We specify OwnerIds=['self'] to retrieve snapshots owned by the account.

snapshots = ec2.describe_snapshots(OwnerIds=['self'])['Snapshots']
Enter fullscreen mode Exit fullscreen mode

Writing to CSV

The CSV module in Python simplifies writing tabular data to files. We use csv.DictWriter to write rows of dictionaries to the CSV file. Each dictionary represents a row in the CSV.

with open('ec2_snapshots.csv', mode='w', newline='') as csv_file:
    fieldnames = ['Instance Name', 'Snapshot ID', 'Volume Size (GiB)', 'Snapshot Date Started']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    writer.writeheader()

    for snapshot in snapshots:
        instance_id = extract_instance_id(snapshot['Description'])
        instance_name = get_instance_name(ec2, instance_id)
        snapshot_id = snapshot['SnapshotId']
        volume_size = snapshot['VolumeSize']
        snapshot_date = snapshot['StartTime'].strftime("%Y-%m-%d %H:%M:%S")

        writer.writerow({
            'Instance Name': instance_name,
            'Snapshot ID': snapshot_id,
            'Volume Size (GiB)': volume_size,
            'Snapshot Date Started': snapshot_date
        })
Enter fullscreen mode Exit fullscreen mode

Handling Missing Information

The script gracefully handles missing information. If an instance ID or name is not found, it returns 'N/A'. This ensures that the script does not break and provides a complete CSV output.

Time Formatting

The strftime method formats the snapshot start time into a human-readable string. This makes it easier to interpret the snapshot creation dates in the CSV file.

snapshot_date = snapshot['StartTime'].strftime("%Y-%m-%d %H:%M:%S")
Enter fullscreen mode Exit fullscreen mode

Conclusion

Automating EC2 snapshot management with Python significantly enhances your AWS infrastructure management. This script provides a reliable, repeatable process for documenting and analyzing your snapshots, ensuring your backup strategy is robust and well-documented.

Incorporate this script into your regular AWS maintenance routines to gain better visibility into your snapshot strategy, optimize your backup processes, and free up time for more critical tasks. By leveraging automation, you can ensure your data is secure and readily available, enhancing the overall efficiency and reliability of your AWS environment.

Feel free to customize and expand this script to suit your specific needs, such as adding more snapshot details or integrating with other AWS services. Automation is a powerful tool, and this script is just the beginning of what you can achieve with AWS and Python.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .