如何在python脚本中使用awscli?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23778442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use awscli inside python script?
提问by e271p314
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
我正在将 aws ec2 服务与 awscli 一起使用。现在我想将我在控制台中键入的所有命令放入一个 python 脚本中。我看到如果我import awscli
在 python 脚本中编写它可以正常工作,但我不明白如何在脚本中使用它。例如,我如何在之后执行aws ec2 run-instances <arguments>
python 脚本中的命令import awscli
?为了清楚起见,我不是在寻找类似的解决方案os.system('aws ec2 run-instances <arguments>')
,而是在寻找类似的解决方案
import awscli
awscli.ec2_run-instances(<arguments>)
采纳答案by Julio Faerman
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
CLI 更适合 shell 提示,要获得更好的 Python API,请查看 boto 库。此示例显示如何启动实例:http: //boto.readthedocs.org/en/latest/ec2_tut.html
回答by smokeny
回答by ddtraveller
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.
Boto3 没有 cli 的所有功能,因此您可能需要在脚本中使用 cli 中的某些内容。例如,我在 boto3 中找不到 aws deploy push 的模拟,所以这里是我如何使用 python 脚本中的 cli 推送到 s3。尽管就胡里奥而言,我将 boto 用于其他一切。
import subprocess
cmd='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