How to create an Ec2 instance using Python boto3?

+1 vote
I want to create an Ec2 instance using Python boto3?

Can anyone help me with the code?
Dec 3, 2018 in AWS by Nitesh
• 2,870 points
1,271 views

1 answer to this question.

0 votes

You can use the following code, it worked for me perfectly.

import boto3
ec2 = boto3.resource('ec2')
instance = ec2.create_instances(
    ImageId = 'ami-009d6802948d06e52',
    MinCount = 1,
    MaxCount = 1,
    InstanceType = 't2.micro',
    KeyName = 'nabarupa',
    SubnetId = 'subnet-0yhg678990d56c277')
print (instance[0].id)

Hope this helps.

answered Dec 3, 2018 by Nabarupa
What happens if I do not mention a subnetID?
If you don't specify a subnet-id then it will create a EC2 in the default VPC.
Thank you. That makes sense.

Related Questions In AWS

0 votes
1 answer

How to create an EC2 instance using Python?

Hey JunDevOps, Have a look these scripts: 1. ...READ MORE

answered Jun 24 in AWS by Aysha

edited Jun 25 252 views
0 votes
1 answer

How to Pass the VPC ID while creating the Ec2 instance in AWS using Python Boto3

import boto3 ec2 = boto3.resource('ec2') instance = ec2.create_instances( ...READ MORE

answered Jan 29 in AWS by Priyaj
• 56,160 points
202 views
0 votes
1 answer
0 votes
1 answer

AWS S3 uploading hidden files by default

versioning is enabled in your bucket. docs.aws.amazon.com/AmazonS3/latest/user-guide/….... the ...READ MORE

answered Oct 4, 2018 in AWS by Priyaj
• 56,160 points
141 views
0 votes
1 answer

How to decrypt the encrypted S3 file using aws-encryption-cli --decrypt

Use command : aws s3 presign s3://mybucket/abc_count.png you get ...READ MORE

answered Oct 22, 2018 in AWS by Priyaj
• 56,160 points
266 views
0 votes
1 answer

Import my AWS credentials using python script

Using AWS Cli  Configure your IAM user then ...READ MORE

answered Nov 16, 2018 in AWS by Jino
• 5,550 points
194 views
0 votes
2 answers
+1 vote
1 answer