Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an ec2 instance using boto3

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.

like image 791
MikA Avatar asked Sep 30 '15 10:09

MikA


People also ask

What is Boto3 EC2?

The AWS SDK for Python (Boto3) provides a Python API for AWS infrastructure services. Using the SDK for Python, you can build applications on top of Amazon S3, Amazon EC2, Amazon DynamoDB, and more.

How do I create a VPC in Boto3?

To create a custom VPC, you can use the create_vpc() method from the Boto3 EC2 resource. In the example below, we are creating the custom VPC. To read more about the Boto3 client and Boto3 resource, check out our article Introduction to the Boto3 library.


2 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: http://boto3.readthedocs.org/en/latest/guide/migrationec2.html#launching-new-instances

like image 99
gbs Avatar answered Sep 20 '22 20:09

gbs


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',     ], ) 
like image 30
captainblack Avatar answered Sep 20 '22 20:09

captainblack