简体   繁体   English

AWS cloudformation package 在 python

[英]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?我只想问有没有什么方法可以在python中制作aws cloudformation package? Here is the code in awscli:这是 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?我希望这个在 python 中有吗? Thanks.谢谢。 any help would be appreciated.任何帮助,将不胜感激。

There are two way:有两种方式:

  1. Use Python Objective Oriented Programming Library for CloudFormation and let the library to handle your nested stack and packaging.为 CloudFormation 使用 Python 面向目标的编程库,并让该库处理您的嵌套堆栈和打包。 here's an example: https://cottonformation.readthedocs.io/en/latest/01-cottonformation-by-example/index.html#nested-stack-template这是一个例子: 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:使用子流程标准库从 Python 调用 shell 命令:
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
)

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

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