0

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))
7
  • 2
    You can use the boto3 library for python. Commented Jan 11, 2020 at 12:53
  • Yep, under the hood the aws-cli leverages boto3 anyway. Commented Jan 11, 2020 at 13:44
  • Do you get any output when you run the program? Commented Jan 11, 2020 at 15:12
  • @Dmitry There is no native support for the cloudformation package functions in boto3 afaik. You'd have to implement the same logic that the awscli implements using change sets etc. Commented Jan 11, 2020 at 19:21
  • @HarshitKothari you didn't explain what is going wrong. What fails? Is there an error message? Commented Jan 11, 2020 at 19:23

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.