Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+5 votes
2 views
in AWS by (12.9k 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

+6 votes
by (18.2k 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.

For more information, kindly refer to our Python course.

How to use awscli inside python script?
Intellipaat-community
by (29.3k points)
The CLI is more suited for the shell prompt.
For more details: https://boto.readthedocs.io/en/latest/ec2_tut.html
by (107k points)
This helps! Thank you so much for pointing towards CLI.
by (33.1k points)
It worked for me.
Thanks!
by (44.4k points)
Thanks for the doc link, that helped too!
by (19.9k points)
Nicely Explained. Thanks.
by (32.1k points)
Thanks for the explanation!
+1 vote
by (106k 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")

How to use awscli inside python script?
Intellipaat-community
by (29.5k 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 (41.4k 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.

Related questions

0 votes
1 answer
Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
asked Mar 19, 2020 in AWS by chandra (29.3k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...