0
votes

I have write to generate list for all the available snapshots in a region. I got 45 snapshots as output. Actually , I have only 5 snapshots in my region as well as in my account.

I have tried with python version 3.6, Boto3 and CentOS7.

Could you anyone wakeup me, Please.

Thanks, Karan

Here is my simple script for list the available snapshots in a region.

#!/usr/bin/python3.6
import boto3
region='xx-xxxx-x'
ec2=boto3.resource('ec2',region_name=region)
try:
        snapshos=ec2.snapshots.all()
        for snap in snapshos:
                print(snap.id,snap.state)
except AttributeError as e:
        print('Error', e)

output:

snap-01ccbc648c4ad8014 completed
snap-0c2d3543fce41724a completed
snap-0826e8a53b2774cbe completed
snap-0168deea3f5b58f2d completed
snap-02ad4595811226db2 completed
snap-0d726327a37e8f7ab completed
snap-0bdf56e9597463f4a completed
snap-0a57341307d009304 completed
snap-0f23409e560e2f059 completed
snap-0e546d94f2102776e completed
snap-0fe985769cf0737ed completed
snap-0f955fd5d111e3729 completed
snap-08009b0042cf41b36 completed
snap-029f0e8d87851f7e7 completed
snap-0fe2e56cfddd4b179 completed
snap-0d61b03807ea7968e completed
snap-0fb534b55d3969d8c completed
snap-02be9fb0c4b6f2d46 completed
snap-07379ad09c8e9aac2 completed
snap-0a1cdabc059f3f7a0 completed

2

2 Answers

1
votes

This might help..

import boto3

client = boto3.client('ec2', region_name='us-east-1')

response = client.describe_images(Owners=['self'])
for ami in response['Images']:
  print (ami['ImageId'])
0
votes

Those snapshots are probably the EBS volume snapshots of the AMIs that you have access to. This includes the public AMIs too.

What you need to do is filter to just the snapshots that you own. To do this use the describe_snapshots API of the ec2 client.

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_snapshots