Skip to content Skip to sidebar Skip to footer

Aws Boto - How To Create Instance And Return Instance_id

I want to create a python script where I can pass arguments/inputs to specify instance type and later attach an extra EBS (if needed). ec2 = boto3.resource('ec2','us-east-1') hddSi

Solution 1:

in boto3, create_instances returns a list so to get instance id that was created in the request, following works:

ec2_client = boto3.resource('ec2','us-east-1')
response = ec2_client.create_instances(ImageId='ami-12345', MinCount=1, MaxCount=1)
instance_id = response[0].instance_id

Solution 2:

The docs state that the call to create_instances()

https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.ServiceResource.create_instances

Returns list(ec2.Instance). So you should be able to get the instance ID(s) from the 'id' property of the object(s) in the list.


Solution 3:

you can the following

def createInstance():
    instance = ec2.create_instances(
        ImageId=AMI, 
        InstanceType = instType,  
        SubnetId='subnet-31d3ad3', 
        DisableApiTermination=True,
        SecurityGroupIds=['sg-sa4q36fc'],
        KeyName='key'
     )
     # return response
     return instance.instance_id

actually create_instances returns an ec2.instance instance


Post a Comment for "Aws Boto - How To Create Instance And Return Instance_id"