如何在VPC从previous EC2实例的AMI启动一个EC2实例的完全相同的副本 [英] How to Launch a exact same replica of a EC2 instance in VPC from the AMI of a previous EC2 instance

查看:117
本文介绍了如何在VPC从previous EC2实例的AMI启动一个EC2实例的完全相同的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EC2实例一,绝不能重新启动,但问题是,这将是停机维护。我基本上是用我的code创建这个实例的AMI如下:

 进口boto.ec2
进口时间
进口SYS
康恩= boto.ec2.connect_to_region(AP-东南-1)

image_id = conn.create_image(sys.argv中[1],尼特,说明=测试,no_reboot = TRUE,block_device_mapping =无,dry_run = FALSE)
图象= conn.get_all_images(image_ids = [image_id])[0]

!而image.state ='可用':
    time.sleep(10)
    image.update()
    打印图像被创建,请稍等!状态:%s的%(image.state)

如果image.state =='可用':
   打印AMI成功创建与AMI ID =%s的%image_id
其他:
   打印出事了!
 

以上脚本工作正常,并创建我提供作为系统参数的实例的AMI。我需要启动实例A的完全相同的翻版,即需要启动需要有相同的VPC,上电集团,键名等实例.. 我想,我需要存储在一个变量实例A的详细信息,然后利用它们发动的AMI或一个新的实例类似的东西。

PS:该follwing code有助于获取实例A的细节:

 预订= conn.get_all_instances(sys.argv中[1])
实例= [我对R的预订我的r.instances]
因为我的情况:
    打印(我.__ dict__中)
 

解决方案

下面code基本上会采取系统,您将提供实例ID的AMI,然后将其取出现有系统的细节,以及劳克新的副本系统

 从boto.regioninfo进口*
从boto.ec2.connection进口EC2Connection

#进入实例ID在这里您要复制
实例ID ='I-XXXXXXXX

#AWS连接信息
aws_access_key_id ='########## AWS访问密钥############
aws_secret_access_key ='########### AWS秘密钥匙############
=的region_name'AP-东南-1'
region_ec2_endpoint ='ec2.ap-southeast-1.amazonaws.com

#连接EC2
aws_region = RegionInfo(名称=的region_name,终点= region_endpoint)
康恩= EC2Connection(aws_access_key_id,aws_secret_access_key,区域= aws_region)

#创建AMI
打印第1步:创建AMI
ami_id = conn.create_image(INSTANCE_ID,testami,no_reboot =真)
ami_status =待定
打印AMI正在推出的+ ami_id

#check_ami_status
图像= conn.get_image(ami_id)
而image.state ==待定:
    time.sleep(10)
    图像= conn.get_image(ami_id)
    打印AMI处于待定状态,下次检查之前等待10秒

图像= conn.get_image(ami_id)
打印图像现在是+ image.state

保留= conn.get_all_instances(instance_ids = [实例ID])
实例= [我对R的预订我的r.instances]
因为我的情况:
    KEY_NAME = i.key_name
    security_group = []
    对于每个在i.groups:
        security_group.append(each.id)
    INSTANCE_TYPE = i.instance_type
    子网名称= i.subnet_id
    vpc_id = i.vpc_id
    储备= conn.run_instances(image_id = ami_id,subnet_id =子网名称,KEY_NAME = KEY_NAME,INSTANCE_TYPE = INSTANCE_TYPE,security_group_ids = security_group)
    打印新的副本系统ID为+ reserve.instances [0] .ID
 

I have a EC2 instance A which must NOT be rebooted, but the problem is that it's going to be down for maintenance. I basically create a AMI of this instance using my code as follows:

import boto.ec2
import time
import sys 
conn = boto.ec2.connect_to_region("ap-southeast-1")

image_id = conn.create_image(sys.argv[1], "nits", description="Testing", no_reboot=True, block_device_mapping=None, dry_run=False)
image = conn.get_all_images(image_ids=[image_id])[0]

while image.state != 'available':
    time.sleep(10)
    image.update()
    print "The image is being Created, Please wait!! state:%s" % (image.state) 

if image.state == 'available':
   print "AMI CREATED SUCCESSFULLY with AMI id = %s" % image_id   
else:
   print "Something Went Wrong!!" 

The above script works fine and creates an AMI of the instance that I provide as a system argument. I need to launch a EXACT SAME REPLICA of the instance "A" , i.e the instance that need to be launched needs to have the same VPC, sec group, key name etc.. i am thinking that i need to store the instance A's details in a variable and then use them to launch a new instance from the AMI or something like that..

P.S: The follwing code helps to get the details of the instance A:

reservations=conn.get_all_instances(sys.argv[1])
instances = [i for r in reservations for i in r.instances]
for i in instances:
    print(i.__dict__)

解决方案

Below code will basically take ami of the system for which you will provide instance id, then it fetches details of the existing system , and lauch new replica system

from boto.regioninfo import *
from boto.ec2.connection import EC2Connection

# Enter Instance ID here for which you want replication
instance_id = 'i-XXXXXXXX'

# AWS connect info
aws_access_key_id='########## AWS Access Key ############'
aws_secret_access_key='########### AWS Secret Key ############'
region_name='ap-southeast-1'
region_ec2_endpoint='ec2.ap-southeast-1.amazonaws.com'

# Connect EC2
aws_region = RegionInfo(name=region_name, endpoint=region_endpoint)
conn = EC2Connection(aws_access_key_id,aws_secret_access_key,region=aws_region)

# create ami
print "Step 1 : Creating ami"
ami_id = conn.create_image(instance_id,"testami",no_reboot=True)
ami_status = "Pending"
print "ami is being launched " + ami_id

# check_ami_status
image = conn.get_image(ami_id)
while image.state == "pending":
    time.sleep(10)
    image = conn.get_image(ami_id)
    print "ami is in pending state, waiting for 10 sec before next check"

image = conn.get_image(ami_id) 
print "Image is now " + image.state

reservations = conn.get_all_instances(instance_ids=[instance_id])
instances = [i for r in reservations for i in r.instances]
for i in instances:
    key_name = i.key_name
    security_group = []
    for each in i.groups:
        security_group.append(each.id)
    instance_type = i.instance_type
    subnet_name = i.subnet_id
    vpc_id = i.vpc_id
    reserve = conn.run_instances(image_id=ami_id,subnet_id=subnet_name ,key_name=key_name,instance_type=instance_type,security_group_ids =security_group)
    print "new replica system id is " + reserve.instances[0].id

这篇关于如何在VPC从previous EC2实例的AMI启动一个EC2实例的完全相同的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆