如何在创建AMI图像时应用计数器

时间:2015-04-18 18:46:01

标签: python python-2.7 amazon-web-services boto

我正在编写脚本来创建正在运行的实例的图像,并且实例具有Retention Count和Frequency的标签,然后将从该运行实例中选择该值,否则它将设置提供的默认值。创建的图像数量取决于频率。

enter image description here

但是我得到了这个输出。 enter image description here

我已经在脚本中设置了频率并且我已经应用了计数器,但它没有增加count的值,而是给出了相同的值。目前保留计数可能会被删除,因为我现在还没有使用它。

1 个答案:

答案 0 :(得分:0)

import boto 
import time 
from boto import ec2
connection = ec2.connect_to_region('us-east-1')
count=0
reservations = connection.get_all_reservations(filters={ "tag-key" : "Automatic Backup" , "tag-value" : "True"})
A='Retention Count'
B='Frequency'
for reservation in reservations:
    for instance in reservation.instances:
            instance_tag=instance.tags
            try:
                rc=instance_tag[A];
            except:
                rc=30
            try:
                fq=instance_tags[B];
            except:
                fq=1    
            ID=instance.id
               if fq <= '2':
                    curdate = time.strftime("%d/%m/%Y")
                    curtime=time.strftime("%H/%I/%M")
                    all_ami=connection.get_all_images(owners="xxxxxxxx")
                    for img in all_ami:
                        print img
                        val=img.description.find(curdate)
                        if val>0:
                            if count <= fq:
                                count=count+1
                                print count
                                connection.create_image(ID , name=ID+curdate+str(curtime), description=ID+curdate)
                                print "ami created"

                        if val < '0':
                            print count 
                            connection.create_image(ID , name=ID+curdate+str(curtime), description=ID+curdate)