Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use awscli inside python script?

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>)
like image 974
e271p314 Avatar asked May 21 '14 08:05

e271p314


People also ask

Can I use AWS CLI command in Python?

The AWS CLI and AWS SDK for Python will require Python 2.7+ or 3.4+ as their Python runtime. On January 10, 2020, in order to continue supporting our customers with tools that are secure and maintainable, AWS will publish a minor version bump of the AWS CLI and AWS SDK for Python (Boto3 and Botocore).


2 Answers

You can do it with brilliant sh package. You could mimic python package with sh doing wrapping for you.

import sh
s3 = sh.bash.bake("aws s3")
s3.put("file","s3n://bucket/file")
like image 62
smokeny Avatar answered Oct 19 '22 00:10

smokeny


The CLI would be more suited for the shell prompt, for a better python API, check the boto library. This example shows how to launch an instance: http://boto.readthedocs.org/en/latest/ec2_tut.html

like image 18
Julio Faerman Avatar answered Oct 19 '22 01:10

Julio Faerman