Template & Stack(模板与堆栈)

模板(Template)

CloudFormation template(模版)是创建堆栈(stack)资源的文件声明。

模板以JSON或YAML格式存储为文本文件, 可以在任何文本编辑器中创建和编辑它们,并在源代码管理系统中对其进行管理。

在整个Workshop中,代码示例将使用YAML格式。

以下示例显示了一个CloudFormation YAML模板结构和它的顶级对象(top-level objects):

AWSTemplateFormatVersion: 'version date' (optional) # version of the CloudFormation template. Only accepted value is '2010-09-09'

Description: 'String' (optional) # a text description of the Cloudformation template

Metadata: 'template metadata' (optional) # objects that provide additional information about the template

Parameters: 'set of parameters' (optional) # a set of inputs used to customize the template

Rules: 'set of rules' (optional) # a set of rules to validate the parameters provided at deployment/update

Mappings: 'set of mappings' (optional) # a mapping of keys and associated values

Conditions: 'set of conditions' (optional) # conditions that control whether certain resources are created

Transform: 'set of transforms' (optional) # for serverless applications

Resources: 'set of resources' (required) # a components of your infrastructure

Hooks: 'set of hooks' (optional) # Used for ECS Blue/Green Deployments

Outputs: 'set of outputs' (optional) # values that are returned whenever you view your stack's properties

CloudFormation文件中唯一必须包含的顶级对象是Resources对象,必须声明至少一个资源。 每个对象的定义都可以在Template Anatomy 文档中找到

堆栈(Stack)

堆栈是CloudFormation模板的部署,其中所有的资源均由CloudFormation模板定义。

堆栈是一个AWS资源的集合,并将其作为一个单元进行管理:

  • 如果无法完全创建或更新堆栈,则AWS CloudFormation会将其回滚,并自动删除创建的所有资源。
  • 如果无法删除资源,则将保留所有剩余资源,直到可以成功删除堆栈。