且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在VPC从previous EC2实例的AMI启动一个EC2实例的完全相同的副本

更新时间:2023-11-22 19:59:22

下面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