Harshit Kothari
Harshit Kothari

Reputation: 11

How do i write aws cli commands in python

I am new to AWS as well as Python.

AWS CLI, the below command is working perfectly fine:

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. 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.

test.py file:

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

Upvotes: 0

Views: 4675

Answers (2)

Ahsan Kazmi
Ahsan Kazmi

Reputation: 111

It can easily be done using the os library. 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. Boto is AWS SDK for Python.

Upvotes: 4

Yann
Yann

Reputation: 2532

You can check how this aws-cli command is implemented as it's all in Python already. Basically aws cloudformation package uploads the template to S3, so you can do the same with boto3 as mentioned in the comments.

Upvotes: 0

Related Questions