如何用python编写aws cli命令

2024-05-08 01:15:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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))

Tags: 命令awsyamlpackageoutputs3bucketsam
2条回答

使用操作系统库可以很容易地完成。代码中给出了最简单的方法

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 SDK

您可以检查这个aws-cli命令is implemented,因为它已经全部在Python中了。基本上aws cloudformation package会将模板上载到S3,因此您可以使用注释中提到的boto3执行相同的操作

相关问题 更多 >