Is it possible to create an ec2 instance using boto3 in python? Boto3 document is not helping here, and I couldn't find any helping documents online. please provide some sample codes/links.
6 Answers
The API has changed but it's right there in the documentation
# Boto 3
ec2.create_instances(ImageId='<ami-image-id>', MinCount=1, MaxCount=5)
Link to the documentation:
-
Do you know if there is a keyword argument to specify the security group? That way I can create the instance, and set the right security group that will let me ssh into the server. – applecider Oct 11 '15 at 21:55
-
2It should accept a `SecurityGroups=['secgroup', ..]` keyword argument. This [issue](https://github.com/boto/boto3/issues/136) seems to suggest that `create_instances` is running the classic boto2 `run_instances` behind the scenes so the original parameters for `run_instances` should be valid. See the [documentation](http://botocore.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.run_instances) – gbs Oct 11 '15 at 22:10
-
Thanks mate, will give it a shot – applecider Oct 12 '15 at 06:47
-
Hi. The docs are pretty skimpy and vague. If I want the new instances to be c3.xlarge, how do I set the parameters? – Moses Liao GZ Nov 04 '15 at 03:35
-
1Add this keyword argument to `create_instances`: `InstanceType="c3.xlarge"` – gbs Nov 04 '15 at 13:47
-
How can you specify instance name? Instances are created without name... – Brian Dec 01 '17 at 16:59
-
@Brian, the name of an instance is just a tag with the key "Name". See `create_tags` for usage info. – gbs Jan 24 '18 at 15:13
-
Why isn't Amazon's own cli as simple as Boto3? Are they thinking of creating a new job branch(or already created?)? – huseyin tugrul buyukisik Jan 19 '19 at 11:22
-
`ec2` is `boto3.client` or `boto3.resource` type? – L F Apr 12 '22 at 17:07
You can run the code I used from the boto3 docs. You can add or remove parameters as per your requirements, but this is what you would normally require:
import boto3
client = boto3.client('ec2', region_name='us-west-2')
response = client.run_instances(
BlockDeviceMappings=[
{
'DeviceName': '/dev/xvda',
'Ebs': {
'DeleteOnTermination': True,
'VolumeSize': 8,
'VolumeType': 'gp2'
},
},
],
ImageId='ami-6cd6f714',
InstanceType='t3.micro',
MaxCount=1,
MinCount=1,
Monitoring={
'Enabled': False
},
SecurityGroupIds=[
'sg-1f39854x',
],
)

- 4,107
- 5
- 50
- 60
-
How will I know if the instance has been created and ready for login? – Macindows Jan 07 '20 at 09:25
-
You would have to run a DescribeInstances API call with the Instance ID you get from the RunInstances API call. – captainblack Nov 25 '20 at 11:16
-
However there are no API calls that would let you know if the instance is ready to login. – captainblack Nov 25 '20 at 11:17
-
1
The link you're really looking for in the documentation is the create_instances()
method of the ServiceResource object. This is the type of object you are calling if you create an EC2 resource like this:
s = boto3.Session(region_name="us-west-1")
ec2 = s.resource('ec2')
...
instance = ec2.create_instances(**y_kwargs)
This contains a more detailed example and a longer list of available parameters.
You can also get parameter values for AWS instances that are already running using the AWS command line interface:
$ aws ec2 describe-instances
This prints out a JSON file from which relevant parameters can be extracted and passed to the create_instances()
method. (Or, you can use a boto client and call the describe_instances()
method.)
(Note: If you're wondering what the difference is between the Client and the Resource, they serve different purposes for the same end - the client is a lower-level interface while the Resource is a higher-level interface.)

- 4,360
- 4
- 30
- 52
-
2
-
How did you figure this out? I saw ec2.create_instances in the documentation but I didn't know what ec2 was. – user3180 Apr 09 '19 at 22:46
Refer to API docs has all available options to create instance
http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Subnet.create_instances

- 1,743
- 2
- 20
- 28
If your running from your windows computer you need configure AWS Cli with proper EC2 permisssion to launch instance.
#import boto3
ec2 = boto3.resource('ec2')
instance = ec2.create_instances(
ImageId='ami-5eb63a32',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
)
print(instance[0].id)

- 56
- 2
import boto3
ec2 = boto3.client('ec2', region_name='')
conn = ec2.run_instances(ImageId='<ami-image-id>',
InstanceType='<instance-type>',
MinCount=1,
MaxCount=5
)

- 332
- 1
- 3
- 14