Search code examples
amazon-ec2boto

How to create EC2 instance through boto python code


requests = [conn.request_spot_instances(price=0.0034, image_id='ami-6989a659', count=1,type='one-time', instance_type='m1.micro')]

I used the following code. But it is not working.


Solution

  • Use the following code to create instance from python command line.

    import boto.ec2
    
    conn = boto.ec2.connect_to_region(
        "us-west-2",
        aws_access_key_id="<aws access key>",
        aws_secret_access_key="<aws secret key>",
    )
    conn = boto.ec2.connect_to_region("us-west-2")
    conn.run_instances(
        "<ami-image-id>",
        key_name="myKey",
        instance_type="t2.micro",
        security_groups=["your-security-group-here"],
    )