用于管理cloudformation模板和堆栈的工具和模块
cftdeplo的Python项目详细描述
CFT部署
用于管理aws cloudformation堆栈的python模块和脚本
基本原理
cloudformation是一个很有价值的工具,但是它在通过cli管理堆栈方面有一些不足。
aws cloudformation create-stack
命令的选项和参数数量非常大,在使用cli/sdk时,您需要一种一致的方法来管理这些选项和参数。以下是aws cli中的选项:
--stack-name [--template-body ][--template-url ][--parameters ][--disable-rollback | --no-disable-rollback][--timeout-in-minutes ][--notification-arns ][--capabilities ][--resource-types ][--role-arn ][--on-failure ][--stack-policy-body ][--stack-policy-url ][--tags ]
- 虽然堆栈可以导出值,但导出值的键在区域中必须是唯一的。此外,不能将相应导入值的键作为参数提供。它必须在cft中硬编码。因此,您不能有一个适用于dev、test&prod的模板,该模板利用不同的导出值。
- 如果一个堆栈导出一个值,那么修改该堆栈会对您造成很大的影响。aws将这些保护设置为您自己的保护,但是在某些情况下,您可能不想在修改导出堆栈之前删除依赖堆栈。
aws cloudformation create-stack
是一种失火和遗忘操作。是的,您可以使用cli设置服务生。但让我们面对现实吧,这很复杂。
cft deploy旨在帮助解决其中的一些问题。
- cft deploy使用manifest文件的概念来存储所有参数和堆栈选项。这些清单文件在yaml中,可以保存在修订控制中
- cft deploy可以从另一个堆栈的参数、资源或输出中查找参数。例如,如果您有一个部署vpc的模板,并且需要在部署实例或rds的模板中引用vpcid、子网和安全组,则此选项非常有用。
- cft deploy的参数查找不需要区域范围的全局变量。清单文件可以包含要引用的堆栈的名称
- cft deploy将显示堆栈创建和更新的状态,类似于aws控制台中的进度显示方式。
- cft deploy支持清单文件中的stack policies。
- CFT部署可以从模板生成清单文件(并且将来将从现有堆栈生成清单文件)
安装
对于测试,安装过程是
pip install -e .
对于生产使用,可以从pypi安装cft deploy。(尚未实施)
用法
清单文件
脚本
- cft validate-将使用aws cloudformation服务验证模板
- cft upload-将把cft上载到s3,如果模板的大小超过certian大小,则需要此功能
- cft generate manifest-将采用本地或s3托管模板,并生成manifest文件
- {STR 1 } $CFT验证清单将执行所有参数替换,并验证依赖项存在
- cft deploy-将获取清单(和可选的命令行参数)并创建或更新堆栈(提供类似于tail-f的事件体验)
- cft delete-将删除指定的堆栈(提供类似tail-f的删除事件体验)
python模块
python模块由三个主要类组成。所有类都支持传递会话,该会话将支持跨帐户角色假设(除其他外)。
所有模块都使用python记录器进行调试和信息事件。这些可以根据需要重写。
cf模板
cfTemplate表示云信息模板
class CFTemplate(builtins.object)
| CFTemplate(template_body, filename=None, s3url=None, session=None)
|
| Class to represent a CloudFormation Template
|
| Methods defined here:
|
| __init__(self, template_body, filename=None, s3url=None, session=None)
| Constructs a CFTemplate from the template_body (json or yaml).
|
| diff(self, other_template)
| prints out the differences between this template and another one.
|
| generate_manifest(self, manifest_file_name, substitutions=None)
| Generates a stub manifest file for this template and writes it to manifest_file_name.
| If substitutions are specified, these are populated into the stub manifest file.
|
| upload(self, bucket, object_key)
| Upload the template to S3.
|
| validate(self)
| Validate the template's syntax by sending to CloudFormation Service. Returns json from AWS.
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| download(bucket, object_key, session=None)
| Downloads the template from S3 and then initialize.
|
| parse_s3_url(s3url)
| Parse an s3url (s3://bucket/object_key) and return the bucket and object_key
|
| read(filename, session=None)
| Read the template from filename and then initialize.
异常cftemplatetoolargeerror是在aws cloudformation服务使用模板之前必须将其上载到s3的位置定义的。
cfmanifest
class CFManifest(builtins.object)
| CFManifest(manifest_filename, session=None)
|
| Class to represent a CloudFormation Template
|
| Methods defined here:
|
| __init__(self, manifest_filename, session=None)
| Constructs a CFManifest from the manifest file.
|
| build_cft_payload(self)
| Generate the CFT Payload
|
| create_stack(self, override=None)
| Creates a Stack based on this manifest.
|
| estimate_cost(self)
| Return a url to the simple monthly cost estimator for this template / parameter set.
|
| fetch_parameters(self, override=None)
| Based on the manifest's Sourced Parameters, find all the parameters and populate them.
|
| override_option(self, key, value)
| If options are passed in on he command line, these will override the manifest file's value
|
| validate(self, override=None)
| Validate the template's syntax by sending to CloudFormation Service. Returns json from AWS.
cfstack
class CFStack(builtins.object)
| CFStack(stack_name, region, session=None)
|
| Class to represent a CloudFormation Template
|
| Methods defined here:
|
| __init__(self, stack_name, region, session=None)
| Constructs a CFTemplate from the template_body (json or yaml).
|
| create_changeset(self, changeset_name)
| Trigger the creation of the changeset.
|
| delete(self)
| Deletes this stack.
|
| describe_changeset(self, changeset_name)
| Get the details of changes from a previously created changeset.
|
| detect_drift(self)
| Triggers Drift Detection for this stack.
|
| get(self)
| Fetch the latest set of data for this stack from AWS and update properties of the instance.
|
| get_outputs(self)
| Return a dict of each output of this stack.
|
| get_parameters(self)
| Return a dict of each parameter to this stack.
|
| get_resources(self)
| Return all the PhysicalResourceIds for each LogicalId in the template
|
| get_stack_events(self, last_event_id=None)
| Return all stack events since last_event_id.
|
| get_status(self)
| Fetch the value of StackStatus from AWS CF API for this stack
|
| get_template(self)
| Return as a CFTemplate the current template for this stack.
|
| list_changesets(self)
| List all active changesets for this stack.
|
| update(self, manifest)
| Updates a Stack based on this manifest.
为此类定义的异常是
路线图
- 支持cloudformation变更集,因此可以在执行之前查看更新将对堆栈执行什么操作。
- 支持云层漂移探测。
- 支持生成现有堆栈模板的UNIX类差异,并建议对模板进行更新。
- 支持从现有堆栈生成清单文件。