python - 如何使用 boto 从正在运行的实例创建 EC2 镜像?

标签 python amazon-ec2 boto

我正在尝试为我的 EC2 实例创建一个简单的 python 备份脚本。该脚本的目的是创建当前机器的每日/每周快照(参见 this question on ServerFault )。我正在使用 boto用于 EC2 API 的 python 包,并希望从给定实例创建 EBS AMI(如 ElasticFox 的“创建图像”操作)

# This script will look up all your running EC2 images, find the current one, and back it up by creating an AMI 

# Configuration
accessKeyId = "..."
accessKeySecret = "..."
target = "..."

def resolveIp(target):
    import socket
    ip = repr(socket.gethostbyname_ex(target)[2][0])
    return ip

def find_target(target, connection) :
    ip = resolveIp(target)
    print "Finding instance for " + target + " (IP " + ip + ")"
    reservations = connection.get_all_instances();
    for reservation in reservations:
        instances = reservation.instances
        if len(instances) != 1:
            print "Skipping reservation " + reservation
            continue
        instance = instances[0]
        instanceIp = resolveIp(instance.dns_name)
        if instanceIp == ip:
            return instance

    raise Exception("Can't find instance with IP " + ip)

from boto.ec2.connection import EC2Connection

print "Connecting to EC2"
conn = EC2Connection(accessKeyId, accessKeySecret)
print "Connected to EC2"

instance = find_target(target, conn)
print "Backing up instance '{}'".format(instance)

# Now, I'd like to create a new image out of this instance
# Can you help?

(也报告为 an issue on the boto project page ,因为我没有找到邮件列表)

最佳答案

您需要 EC2Connection 对象的“create_image”方法。请参阅文档 here .您也可以在 boto-users 上提问谷歌集团。

关于python - 如何使用 boto 从正在运行的实例创建 EC2 镜像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5256665/

相关文章:

python - 如何在 python 中制作多边形雷达(蜘蛛)图

python - 分类数据是/否到 0/1 python - 这是正确的方法吗?

python - 寻找一个配置最少的轻量级 Python Web 框架

hadoop - ssh:无法解析主机名。名称或服务未知

python - 具有相似主体的覆盖方法

docker - RabbitMQ Docker容器不适用于AWS EC2实例

python - “phantomjs”可执行文件可能具有错误的权限

python - 检查dynamodb中的唯一hash_key - python

python - 如何使用 python boto 获取给定安全组的资源?