简体   繁体   English

我如何在 python 中编写 aws cli 命令

[英]How do i write aws cli commands in python

I am new to AWS as well as Python.我是 AWS 和 Python 的新手。

AWS CLI, the below command is working perfectly fine: AWS CLI,以下命令运行良好:

aws cloudformation package --template-file sam.yaml --output-template-file output-sam.yaml --s3-bucket <<bucket_Name>>

The goal is to create automate python script which will run the above command.目标是创建将运行上述命令的自动化 python 脚本。 I tried to google it but none of the solutions is working for me.我试图用谷歌搜索它,但没有一个解决方案对我有用。

Below are the solution that I have tried but not able to upload the artifacts onto the S3 bucket.以下是我尝试过但无法将工件上传到 S3 存储桶的解决方案。

test.py file: 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))

It can easily be done using the os library.使用 os 库可以轻松完成。 The simplest way of doing it is given in the code.代码中给出了最简单的方法。

import os
os.system("aws cloudformation package --template-file sam.yaml --output-template-file output-sam.yaml --s3-bucket <<bucket_name>>")

However, subprocess can be used for a little complicated tasks.但是,子流程可用于稍微复杂的任务。 You can also check out boto3 library for such tasks.您还可以查看 boto3 库以进行此类任务。 Boto is AWS SDK for Python. Boto 是适用于 Python 的 AWS 开发工具包。

You can check how this aws-cli command is implemented as it's all in Python already.您可以检查这个aws-cli命令如何实现的,因为它已经全部在 Python 中实现了。 Basically aws cloudformation package uploads the template to S3, so you can do the same with boto3 as mentioned in the comments.基本上aws cloudformation package将模板上传到 S3,因此您可以使用boto3中提到的boto3执行相同操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM