+6 votes
1 view
in AWS by (11.7k points)

I'm using aws ec2 service with awscli. Now I want to put all the commands I type in the console into a python script. I see that if I write import awscli inside a python script it works fine but I don't understand how to use it inside the script. For instance how do I execute the commands aws ec2 run-instances <arguments> inside the python script after import awscli? Just to make it clear, I'm not looking for a solution like, os.system('aws ec2 run-instances <arguments>') I'm looking for something like

import awscli

awscli.ec2_run-instances(<arguments>)

closed

2 Answers

+7 votes
by (18.6k points)
selected by
 
Best answer

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.

by (29.9k points)
The CLI is more suited for the shell prompt.
For more details: https://boto.readthedocs.io/en/latest/ec2_tut.html
by (105k points)
This helps! Thank you so much for pointing towards CLI.
by (33.2k points)
It worked for me.
Thanks!
by (44.6k points)
Thanks for the doc link, that helped too!
by (19.2k points)
Nicely Explained. Thanks.
by (31.8k points)
Thanks for the explanation!
+2 votes
by (107k points)

There are many ways to solve this problem some of the important one I am mentioning here. You can use the CLI, it would be more suited for the shell prompt, for a better python API, check the boto library you can check the following link which shows how to launch an instance: http://boto.readthedocs.org/en/latest/ec2_tut.html.

Another thing you can do it with the brilliant sh package.  See the code below:-

import sh

s3 = sh.bash.bake("aws s3")

s3.put("file","s3n://bucket/file")

by (29.8k points)
The sh package is definitely interesting. Thanks for the pointer. But it is not for Windows though you can use it for *nix platforms
by (38.9k points)
Great! This answer helped.
by (47.2k points)
The tutorial completely focuses on the boto interface to the Elastic Compute Cloud from Amazon Web Services and helped me a lot.
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...