How to use awscli inside python script? How to use awscli inside python script? python python

How to use awscli inside python script?


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

import shs3 = sh.bash.bake("aws s3")s3.put("file","s3n://bucket/file")


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


Boto3 doesn't have everything the cli has so you may have to use something from the cli in a script once in a blue moon. I can't find an analog for aws deploy push in boto3 for example so here is how I push to s3 with the cli from a python script. Although to Julio's point, I use boto for everything else.

import subprocesscmd='aws deploy push --application-name SomeApp --s3-location  s3://bucket/Deploy/db_schema.zip --ignore-hidden-files' push=subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE)print push.returncode