AWS has an SDK for Python called Boto3 that will be perfect for what you are trying to achieve. All you have to do is install Boto3 library in Python along with AWS CLI tool using 'pip'. One Boto3 is installed, it will provide direct access to AWS services like EC2.
To get you started with Boto3 I have given an example below that shows how to create a key pair and launch an instance using Python script.
import boto3
ec2 = boto3.resource('ec2')
# creating a file to store the key locally
outfile = open('ec2-keypair.pem','w')
# call the boto ec2 function to create a key pair
My_Key_Pair = ec2.create_key_pair(KeyName='ec2-keypair')
# capture the key and store it in a file
KeyPairOut = str(My_Key_Pair.key_material)
print(KeyPairOut)
outfile.write(KeyPairOut)
This script will create a key pair and save it on your local machine as well.
import boto3
ec2 = boto3.resource('ec2')
# create a new EC2 instance
New_instances = ec2.create_instances(
ImageId='ami-00b6a8a2bd28daf19',
MinCount=1,
MaxCount=2,
InstanceType='t2.micro',
KeyName='ec2-keypair'
This script will create an EC2 instance. The image ID is AMI ID.
To read more about Amazon Elastic Compute Cloud, you can visit AWS EC2.
For more information, kindly refer to our Python course.