Conditions - Amazon CloudFormation
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

Conditions

可选的 Conditions 部分包含一些声明,以定义在哪些情况下创建或配置实体。例如,您可以创建一个条件,然后将其与某个资源或输出关联,以便 Amazon CloudFormation 仅在条件为 true 时创建该资源或输出。同样,您可以将条件与一个属性关联,以便 Amazon CloudFormation 仅在条件为 true 时将该属性设置为特定的值。如果条件为 false,则 Amazon CloudFormation 将该属性设置为您指定的不同值。

当您需要重新使用可在不同环境 (如测试环境与生产环境) 中创建资源的模板时,可能会使用条件。在模板中,您可以添加 EnvironmentType 输入参数,它接受 prodtest 以作为输入。对于生产环境,您可以包括带特定功能的 Amazon EC2 实例;但对于测试环境,您需要使用更少的功能来节约资金。使用条件,您可以定义对每个环境类型创建哪些资源以及如何配置它们。

条件是根据您在创建或更新堆栈时指定的预定义 pseudo parameters 或输入参数值计算的。在每个条件中都可以引用其他条件、参数值或映射。定义所有条件后,您可以在模板的 ResourcesOutputs 部分将它们与资源和资源属性关联起来。

在创建或更新堆栈时,Amazon CloudFormation 先计算模板中的所有条件,然后再创建资源。将创建与 true 条件关联的资源。将忽略与 false 条件关联的资源。在每次堆栈更新时,Amazon CloudFormation 还会在更新任何资源之前重新计算这些条件。仍与 true 条件关联的资源将会得以更新。目前与 false 条件关联的资源将被删除。

重要

堆栈更新期间,您无法更新条件本身。您只能在包含添加、修改或删除资源的更改时更新条件。

条件使用方式概述

根据您要有条件地创建或配置的实体,您必须在以下模板部分中包含声明:

Parameters 部分

定义您希望条件计算的输入。根据这些输入参数的值,条件计算结果为 true 或 false。如果您希望条件计算虚拟参数,则不需要在该部分中定义虚拟参数;虚拟参数是由 Amazon CloudFormation 预定义的。

Conditions 部分

使用内部条件函数定义条件。这些条件确定 Amazon CloudFormation 何时创建关联的资源。

ResourcesOutputs 部分

将条件与要有条件地创建的资源或输出相关联。Amazon CloudFormation 创建与 true 条件关联的实体,并忽略与 false 条件关联的实体。使用 Condition 键和条件的逻辑 ID 将其关联到资源或输出。要有条件地指定属性,请使用 Fn::If 函数。有关更多信息,请参阅条件函数

语法

Conditions 部分包括键名称 Conditions。每个条件声明均包括一个逻辑 ID 和多个在创建或更新堆栈时计算的内部函数。以下伪模板概述了 Conditions 部分:

JSON

"Conditions" : { "Logical ID" : {Intrinsic function} }

YAML

Conditions: Logical ID: Intrinsic function

条件内部函数

您可以使用以下内部函数定义条件:

有关每个函数的语法和信息,请参阅 条件函数

注意

仅在模板的 ResourcesOutputs 部分中的元数据属性、更新策略属性和属性值中支持 Fn::If

示例

简单条件

以下示例模板包含一个 EnvType 输入参数,在这里可以指定 prod 来创建生产堆栈,或指定 test 来创建测试堆栈。对于生产环境,Amazon CloudFormation 会创建一个 Amazon EC2 实例并向该实例附加一个卷。对于测试环境,Amazon CloudFormation 只创建 Amazon EC2 实例。

如果 CreateProdResources 参数与 true 相等,EnvType 条件将计算为 prod。在示例模板中,NewVolumeMountPoint 资源与 CreateProdResources 条件关联。因此,仅当 EnvType 参数等于 prod 时才会创建资源。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Parameters": { "EnvType": { "Description": "Environment type.", "Default": "test", "Type": "String", "AllowedValues": [ "prod", "test" ], "ConstraintDescription": "must specify prod or test." } }, "Conditions": { "CreateProdResources": { "Fn::Equals": [ { "Ref": "EnvType" }, "prod" ] } }, "Resources": { "EC2Instance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId": "ami-0ff8a91507f77f867" } }, "MountPoint": { "Type": "AWS::EC2::VolumeAttachment", "Condition": "CreateProdResources", "Properties": { "InstanceId": { "Ref": "EC2Instance" }, "VolumeId": { "Ref": "NewVolume" }, "Device": "/dev/sdh" } }, "NewVolume": { "Type": "AWS::EC2::Volume", "Condition": "CreateProdResources", "Properties": { "Size": 100, "AvailabilityZone": { "Fn::GetAtt": [ "EC2Instance", "AvailabilityZone" ] } } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Parameters: EnvType: Description: Environment type. Default: test Type: String AllowedValues: - prod - test ConstraintDescription: must specify prod or test. Conditions: CreateProdResources: !Equals - !Ref EnvType - prod Resources: EC2Instance: Type: 'AWS::EC2::Instance' Properties: ImageId: ami-0ff8a91507f77f867 MountPoint: Type: 'AWS::EC2::VolumeAttachment' Condition: CreateProdResources Properties: InstanceId: !Ref EC2Instance VolumeId: !Ref NewVolume Device: /dev/sdh NewVolume: Type: 'AWS::EC2::Volume' Condition: CreateProdResources Properties: Size: 100 AvailabilityZone: !GetAtt - EC2Instance - AvailabilityZone

嵌套条件

以下示例模板引用了另一个条件中的条件。您可以创建一个堆栈来创建 s3 存储桶。对于部署在生产环境中的堆栈,Amazon CloudFormation 会为 S3 存储桶创建策略。

JSON
{ "Parameters": { "EnvType": { "Type": "String", "AllowedValues": [ "prod", "test" ] }, "BucketName": { "Default": "", "Type": "String" } }, "Conditions": { "IsProduction": { "Fn::Equals": [ { "Ref": "EnvType" }, "prod" ] }, "CreateBucket": { "Fn::Not": [ { "Fn::Equals": [ { "Ref": "BucketName" }, "" ] } ] }, "CreateBucketPolicy": { "Fn::And": [ { "Condition": "IsProduction" }, { "Condition": "CreateBucket" } ] } }, "Resources": { "Bucket": { "Type": "AWS::S3::Bucket", "Condition": "CreateBucket" }, "Policy": { "Type": "AWS::S3::BucketPolicy", "Condition": "CreateBucketPolicy", "Properties": { "Bucket": { "Ref": "Bucket" }, "PolicyDocument": "..." } } } }
YAML
Parameters: EnvType: Type: String AllowedValues: - prod - test BucketName: Default: '' Type: String Conditions: IsProduction: !Equals - !Ref EnvType - prod CreateBucket: !Not - !Equals - !Ref BucketName - '' CreateBucketPolicy: !And - !Condition IsProduction - !Condition CreateBucket Resources: Bucket: Type: 'AWS::S3::Bucket' Condition: CreateBucket Policy: Type: 'AWS::S3::BucketPolicy' Condition: CreateBucketPolicy Properties: Bucket: !Ref BucketName PolicyDocument: ...