Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS cloudformation package in python

I just want to ask if there is any way that I can make the aws cloudformation package in python? Here is the code in awscli:

aws cloudformation package \
        --region us-east-1 \
        --profile $Profile \
        --template-file templates/$TEMPLATE.yml \
        --s3-bucket $ARTIFACT_BUCKET \
        --output-template-file $d-$TEMPLATE-$Profile.packaged.yml

I want this to have in python? Thanks! any help would be appreciated.

like image 271
Tine Avatar asked Jun 12 '26 11:06

Tine


1 Answers

There are two way:

  1. Use Python Objective Oriented Programming Library for CloudFormation and let the library to handle your nested stack and packaging. here's an example: https://cottonformation.readthedocs.io/en/latest/01-cottonformation-by-example/index.html#nested-stack-template
  2. Use subprocess standard library to call shell command from Python:
import os
import subprocess

subprocess.run(
    [
        "aws", "cloudformation", "package",
        "--region", "us-east-1"
        "--profile", os.environ["Profile"], # use os.environ to get the env var value
        "--template-file", f'templates/{os.environ["TEMPLATE"]}.yml',
        "--s3-bucket", os.environ["ARTIFACT_BUCKET"],
        "--output-template-file", f'{os.environ["d"]}-{os.environ["TEMPLATE"]}-{os.environ["Profile"]}.packaged.yml',
    ],
    check=True, # if failed, stop the script immediately, prevent the sub sequence command to run
)
like image 194
MacSanhe Avatar answered Jun 13 '26 23:06

MacSanhe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!