python - 我如何在python中编写aws cli命令
问题描述
我是 AWS 和 Python 的新手。
AWS CLI,以下命令运行良好:
aws cloudformation package --template-file sam.yaml --output-template-file output-sam.yaml --s3-bucket <<bucket_Name>>
目标是创建将运行上述命令的自动化 python 脚本。我试图用谷歌搜索它,但没有一个解决方案对我有用。
以下是我尝试过但无法将工件上传到 S3 存储桶的解决方案。
test.py 文件:
import subprocess
command= ["aws","cloudformation","package","--template-file","sam.yaml","--output-template-file","output-sam.yaml","--s3-bucket","<<bucket_name>>"]
print(subprocess.check_output(command, stderr=subprocess.STDOUT))
解决方案
它可以使用 os 库轻松完成。代码中给出了最简单的方法。
import os
os.system("aws cloudformation package --template-file sam.yaml --output-template-file output-sam.yaml --s3-bucket <<bucket_name>>")
但是,子流程可以用于稍微复杂的任务。您还可以查看 boto3 库以执行此类任务。Boto 是适用于 Python 的 AWS 开发工具包。
推荐阅读
- java - 向数据库添加时间戳时,它只会设置当前时间,忽略值
- java - Android中使用ArrayList的ArrayList打印随机Toast消息
- c# - 如何使用来自 mongoDB 的查询和使用属性来检索数据?
- svg - 将 SVG 移动到外部文件时,模式不再起作用
- google-sheets - 即使删除了列,Google 表格公式如何保持不变?
- python - Python获取当前分区数据
- python - 停止执行 multiprocessing.pool()
- php - 在 foreach 循环中如何确定当前键是否设置了值?
- python - 为什么 Python 登录在没有登录名/密码的情况下工作
- docker - 使用 Docker 并希望 HTTP 到 HTTPS 使用 NGINX 反向代理到 HTTP Grafana