Automating AWS Backup Checks with Python: A Simple Guide

In the vast expanse of AWS infrastructure, keeping track of which instances are marked for backup can be a daunting task. Whether you're managing a handful of instances or overseeing a sprawling cloud environment, the importance of a robust backup strategy cannot be overstated. Today, we're diving into how a simple Python script can streamline this process, ensuring your critical AWS EC2 instances are always backed up.

The Need for Backup Automation

Backup strategies are the safety nets of the digital world, protecting against data loss due to system failures, human errors, or malicious attacks. In AWS, tagging resources allows for organized management, including which instances require backups. However, manually checking these tags to confirm backup compliance is inefficient and prone to error, especially as your environment grows. This is where automation comes in.

Python and Boto3: A Powerful Duo

Python, with its simplicity and the powerful AWS SDK package Boto3, makes automating AWS tasks accessible and efficient. Boto3 allows Python scripts to interact with AWS services directly, leveraging the full range of AWS APIs. For our task, we'll use Boto3 to query EC2 instances, focusing on those tagged with backup=true.

Step-by-Step Guide to Listing Backup-Tagged Instances

Here's a concise guide to creating a Python script that lists all your EC2 instances marked for backup.

Step 1: Setting Up

Ensure you have Python and Boto3 installed. If Boto3 isn't installed yet, you can add it to your environment using pip:

pip install boto3

Also, make sure your AWS CLI is configured for access, especially if you're using AWS Single Sign-On (SSO).

Step 2: The Script

Our script initializes an EC2 client, filters instances by the backup=true tag, and prints out details for each matching instance.

import boto3

def list_backup_enabled_instances():
    ec2 = boto3.client('ec2')
    filters = [{'Name': 'tag:backup', 'Values': ['true']}]

    instances = ec2.describe_instances(Filters=filters)['Reservations']
    for reservation in instances:
        for instance in reservation['Instances']:

def print_details(instance):
    instance_id = instance['InstanceId']
    name_tag = next((tag['Value'] for tag in instance.get('Tags', []) if tag['Key'] == 'Name'), None)
    state = instance['State']['Name']
    instance_type = instance['InstanceType']
    launch_time = instance['LaunchTime'].strftime('%Y-%m-%d %H:%M:%S')

    print(f"Instance ID: {instance_id}")
    print(f"Name: {name_tag}")
    print(f"State: {state}")
    print(f"Instance Type: {instance_type}")
    print(f"Launch Time: {launch_time}")
    print("-" * 60)

if __name__ == "__main__":

Step 3: Running the Script

Save the script as and run it using:



This simple yet effective script exemplifies how automating routine checks can save time, reduce errors, and ensure compliance with your backup strategy. As your AWS infrastructure grows, incorporating such scripts into your operational toolkit becomes increasingly beneficial, allowing you to focus on innovation rather than manual oversight.

Remember, while the script provides a snapshot of your backup compliance, it's part of a broader disaster recovery strategy. Always ensure your backups are tested regularly for integrity and restorability. Happy coding, and here's to robust, automated backups!

🚀 **Support Our DevOps Blog with Your Amazon Shopping!** 🚀 Love shopping on Amazon? Now you can fuel your shopping spree *and* support our blog at no extra cost! Just use our link for your next purchase: **[Shop on Amazon & Support Us!] Browse Stuff on Amazon Every click helps us keep sharing the DevOps love. Happy shopping!

Leave a comment

Your email address will not be published. Required fields are marked *