How to Launch a exact same replica of a EC2 instance in VPC from the AMI of a previous EC2 instance

寵の児 提交于 2019-12-12 03:39:20

问题


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__)

回答1:


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


来源:https://stackoverflow.com/questions/32816924/how-to-launch-a-exact-same-replica-of-a-ec2-instance-in-vpc-from-the-ami-of-a-pr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!