Bootstrap FreeKB - Amazon Web Services (AWS) - List Elastic Block Storage (EBS) Volume Snapshots using Python boto3
Amazon Web Services (AWS) - List Elastic Block Storage (EBS) Volume Snapshots using Python boto3


This assumes you are familar with the basic configurations needed to connect to Amazon Web Services (AWS) using Python boto3. If not, check out my article Python (Scripting) - Getting Started with Amazon Web Services (AWS) boto3.

Here is the minimal boilterplate code without error handling to list your Elastic Block Storage (EBS) Volume Snapshots.

#!/usr/bin/python3
import boto3

client = boto3.client('ec2')
response = client.describe_snapshots()
print(response)

 

Almost always, you are going to want to limit the output, perhaps with owner-id.

#!/usr/bin/python3
import boto3

client = boto3.client('ec2')
response = client.describe_snapshots(Filters=[
        {
            'Name': 'owner-id',
            'Values': [
                '123456789012',
            ]
        },
    ])
print(response)

 

Or limit the output with volume-id.

#!/usr/bin/python3
import boto3

client = boto3.client('ec2')
response = client.describe_snapshots(Filters=[
        {
            'Name': 'volume-id',
            'Values': [
                'vol-123456789abcdefg',
                'vol-987654321xzynvbd'
            ]
        },
    ])
print(response)

 

Here is a more practical example, with try/except/else error handling.

#!/usr/bin/python3
import boto3
import sys

try:
  client = boto3.client('ec2')
except Exception as exception:
  print(exception)
  sys.exit(1)

try:
  response = client.describe_snapshots(Filters=[
        {
            'Name': 'volume-id',
            'Values': [
                'vol-123456789abcdefg',
                'vol-987654321xzynvbd'
            ]
        },
    ])
except Exception as exception:
  print(exception)

try:
  response['Snapshots']
except KeyError as keyerror:
  print(keyerror)
else:
  for snapshot in response['Snapshots']:
    for key in snapshot:
      print(f"{key} = {snapshot[key]}")

 

Something like this should be returned

Description =
Encrypted = False
OwnerId = 123456789012
Progress = 100%
SnapshotId = snap-09e46801b783ce255
StartTime = 2024-04-03 01:49:02.888000+00:00
State = completed
VolumeId = vol-0d989fe3bad4dd2f9
VolumeSize = 8
StorageTier = standard

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 0a4585 in the box below so that we can be sure you are a human.