Skip to content

Instantly share code, notes, and snippets.

@ashutoshkarna03
Created May 18, 2020 18:20
Show Gist options
  • Save ashutoshkarna03/a8cc5e9d9ce39d64167d7bfad8ae6941 to your computer and use it in GitHub Desktop.
Save ashutoshkarna03/a8cc5e9d9ce39d64167d7bfad8ae6941 to your computer and use it in GitHub Desktop.
def launch_instance(image_id, instance_id, client_src, client_des, ec2_resource_des):
# create waiter for destination instance to check if instance status is `ok`
waiter = client_des.get_waiter('instance_status_ok')
# get the source instance details needed for launching the instance
instance_detail = get_instance_details(instance_id, client_src)
# launch instance using create_instances() method of ec2 resource
try:
response = ec2_resource_des.create_instances(
ImageId=image_id,
InstanceType=instance_detail['instance_type'],
MaxCount=1,
MinCount=1,
DryRun=False,
Monitoring={'Enabled': False},
)
instance_id = str(response[0]).split("'")[1]
print('Creating instance, this may take few minutes...')
waiter.wait(InstanceIds=[instance_id], DryRun=False)
return dict(success=True)
except Exception as e:
print('Error in launching intance with error: ' + str(e))
return dict(success=False, error_msg=str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment