1

I'm new to python, trying to write a script for taking daily amazon ebs snapshots. Below is the script which lists out the volumes and input them to snapshot command in a for loop.


#!/usr/bin/python
#Script for purging AWS Ebs Snapshots.

from boto.ec2 import EC2Connection
import time

My_access_key = "xxxxxxxxxxxxxxx"
My_secret_key = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"


conn = EC2Connection(My_access_key, My_secret_key)

# List out the volumes
vol_id = conn.get_all_volumes(volume_ids=None, filters=None)
print vol_id

for i in vol_id:
snapshot = conn.create_snapshot(i, 'Daily-Snapshot')
print "Creating Snapshot:", snapshot

Problem is when i'm list volumes its listing this way "[Volume:vol-a50057e8, Volume:vol-ba693ef7]"

and create snapshot command will take only this as valid input "vol-a50057e8". I tried to trim but that din't work.

Thanks, Swaroop.

2 Answers2

2
volumes = conn.get_all_volumes(volume_ids=None, filters=None)
# what you get here is a list of volume objects (not just IDs of those)
for volume in volumes:
   # each volume object has a field "id" which contains what you need:
   snapshot = conn.create_snapshot(volume.id, "Daily-Snapshot")
Alfe
  • 56,346
  • 20
  • 107
  • 159
  • Instead of commenting your thankyous, feel free to upvote any answer which helped you and if one solved your issue completely, you should accept it. – Alfe Jan 21 '14 at 11:56
0

That's just the "textual" representation of the Volume objects get_all_volumes returns Volume object so you can probably do

for vol in conn.get_all_volumes(...):
   do_stuff(vol.id)

Ref: http://docs.pythonboto.org/en/latest/ref/ec2.html#module-boto.ec2.volume

bakkal
  • 54,350
  • 12
  • 131
  • 107