Kittystone Kittystone - 5 months ago 21
Python Question

How to check if the snapshot is now 'completed'

i am using boto3 to create snapshot of a id and i just need to check if the snapshot is finished creating , but the following loop doesn't do that and just runs to infinity .

regions = ['eu-central-1']
for region in regions:
ec2 = boto3.resource('ec2', region, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY, )
snapshot = ec2.create_snapshot(VolumeId='vol-f9e7d220', Description='fra01-he-trial-ansible01')
print snapshot.id

get_all_snapshots = ec2.snapshots.filter(snap_id=['SnapshotIds'])
print get_all_snapshots

while snapshot.state != 'completed':
## put a condition here to get all snapshot or update the state !
print snapshot.progress
print "Snapshot under creation"
time.sleep(10)
else:
print "snapshot READY"

Answer

You should call snapshot.load() before the loop and after your sleep statement.

Just tested it on an AWS micro instance and it works when added to your code.

snapshot = ec2.create_snapshot(VolumeId='vol-#####', Description='snapshotID')
print snapshot.id
snapshot.load()
while snapshot.state != 'completed':
      print snapshot.progress
      print "Snapshot under creation"
      time.sleep(10)
      snapshot.load()
else:
    print "snapshot READY"
Comments