def create_ec2_instance(env, conn, vpc, ec2Group): print "Starting Instance creation process.." #Launch an instance: new_instaces_ids, dryRun = {}, True if env != "test": dryRun = False for ec2 in ec2Group: if ec2.id == "new": dev_sda1 = boto.ec2.blockdevicemapping.EBSBlockDeviceType() if len(ec2.volumes) <= 1: #instance has only one volume #TODO - specify that disk should be deleted on instance termination dev_sda1.size = ec2.volumes[0] dev_sda1.delete_on_termination=True bdm = boto.ec2.blockdevicemapping.BlockDeviceMapping() bdm['/dev/sda1'] = dev_sda1 #TODO increment number of disks provisioned: #else: # for in xrange( len(ec2.volumes) -1): # conn.attach_volume (vol.id, inst.id, "/dev/sdx") sg_id = [ sg.id for sg in vpc.get_all_security_groups() if sg.name == ec2.security_groups ] if len(sg_id) > 0: #print ec2.private_ip_address interface = boto.ec2.networkinterface.NetworkInterfaceSpecification(subnet_id = ec2.subnet_id, private_ip_address= ec2.private_ip_address, groups= sg_id, associate_public_ip_address = True) interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection(interface) print "Creating new ec2 instance - " + str(ec2) try: new_ec2 = conn.run_instances( image_id= ec2.image_id , key_name=ec2.key_name, instance_type=ec2.instance_type, #the following two arguments are provided in the network_interface #instead at the global level !! #security_group_ids=sg_id, #subnet_id = ec2.subnet_id, network_interfaces = interfaces, monitoring_enabled=ec2.monitoring_enabled, disable_api_termination= ec2.disable_api_termination, additional_info=ec2.additional_info, block_device_map =bdm, dry_run = dryRun) if new_ec2: instance = new_ec2.instances[0] instance.update() while instance.state == 'pending': print instance, instance.state time.sleep(5) instance.update() if instance.state == 'running': new_instaces_ids[ new_ec2.instances[0] ] = ec2.additional_info instance.add_tag(ec2.name, ec2.additional_info) else: print "There was a problem, and instance could not be created.." print instance except Exception, e: print e else: print "Could not provision ec2 instance %s because SG %s does not exist! (tip: maybe its creation failed!)" % (str(ec2), ec2.security_groups)
def create_ec2_instance(env, conn, vpc, ec2Group): print "Starting Instance creation process.." #Launch an instance: new_instaces_ids, dryRun = {}, True if env != "test": dryRun = False for ec2 in ec2Group: if ec2.id == "new": dev_sda1 = boto.ec2.blockdevicemapping.EBSBlockDeviceType() if len(ec2.volumes) <= 1: #instance has only one volume #TODO - specify that disk should be deleted on instance termination dev_sda1.size = ec2.volumes[0] dev_sda1.delete_on_termination = True bdm = boto.ec2.blockdevicemapping.BlockDeviceMapping() bdm['/dev/sda1'] = dev_sda1 #TODO increment number of disks provisioned: #else: # for in xrange( len(ec2.volumes) -1): # conn.attach_volume (vol.id, inst.id, "/dev/sdx") sg_id = [ sg.id for sg in vpc.get_all_security_groups() if sg.name == ec2.security_groups ] if len(sg_id) > 0: #print ec2.private_ip_address interface = boto.ec2.networkinterface.NetworkInterfaceSpecification( subnet_id=ec2.subnet_id, private_ip_address=ec2.private_ip_address, groups=sg_id, associate_public_ip_address=True) interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection( interface) print "Creating new ec2 instance - " + str(ec2) try: new_ec2 = conn.run_instances( image_id=ec2.image_id, key_name=ec2.key_name, instance_type=ec2.instance_type, #the following two arguments are provided in the network_interface #instead at the global level !! #security_group_ids=sg_id, #subnet_id = ec2.subnet_id, network_interfaces=interfaces, monitoring_enabled=ec2.monitoring_enabled, disable_api_termination=ec2.disable_api_termination, additional_info=ec2.additional_info, block_device_map=bdm, dry_run=dryRun) if new_ec2: instance = new_ec2.instances[0] instance.update() while instance.state == 'pending': print instance, instance.state time.sleep(5) instance.update() if instance.state == 'running': new_instaces_ids[ new_ec2.instances[0]] = ec2.additional_info instance.add_tag(ec2.name, ec2.additional_info) else: print "There was a problem, and instance could not be created.." print instance except Exception, e: print e else: print "Could not provision ec2 instance %s because SG %s does not exist! (tip: maybe its creation failed!)" % ( str(ec2), ec2.security_groups)
# aws_access_key_id=aws_conf['AWS_ACCESS_KEY_ID'], # aws_secret_access_key=aws_conf['AWS_SECRET_ACCESS_KEY']) #Booting up.. conn = boto.ec2.connect_to_region(region) #Make sure VPC exists to bring some order vpc = boto.vpc.connect_to_region(region) try: provision_vpc("production", vpc) except Exception, e: print e #Prepare Security Groups already_existing_sgs = vpc.get_all_security_groups() #Make sure Security Groups already Exist and/or are up to date try: create_or_update_security_groups("production", conn, vpc, already_existing_sgs) except Exception, e: print e #Fire new ec2 instances print "Current ec2 instances:" list_all_ec2_instances(env, conn) create_ec2_instance(env, conn, vpc, ec2Instances) if __name__ == "__main__": boostrapper(my_env)
# aws_access_key_id=aws_conf['AWS_ACCESS_KEY_ID'], # aws_secret_access_key=aws_conf['AWS_SECRET_ACCESS_KEY']) #Booting up.. conn = boto.ec2.connect_to_region(region) #Make sure VPC exists to bring some order vpc = boto.vpc.connect_to_region(region) try: provision_vpc("production", vpc) except Exception, e: print e #Prepare Security Groups already_existing_sgs = vpc.get_all_security_groups() #Make sure Security Groups already Exist and/or are up to date try: create_or_update_security_groups("production", conn, vpc, already_existing_sgs) except Exception, e: print e #Fire new ec2 instances print "Current ec2 instances:" list_all_ec2_instances(env, conn) create_ec2_instance(env, conn, vpc, ec2Instances) if __name__=="__main__": boostrapper(my_env)