Use the Conditions section to define conditional logic that controls whether resources, properties, and outputs are created. Define condition statements with Fn::And, Fn::Or, Fn::Not, or Fn::Equals — referencing parameters, mappings, or other conditions — then associate each condition with resources or outputs using the Condition key or Fn::If.
Syntax
How conditions work
To use conditions in a template, follow these steps:
Define input parameters — Declare the values your conditions will evaluate in the Parameters section.
Define conditions — Write condition statements in the Conditions section using the supported condition functions listed below. Each condition name must be unique and is of the String type. Conditions can reference other conditions.
Associate conditions with resources or outputs — Reference conditions in the Resources section (via
Conditionkey orFn::If) and in the Outputs section (viaConditionkey) to control what gets created.
Condition functions
Use the following functions to define condition statements:
The following functions are supported inside condition statements but cannot be the outermost function in a condition:
Fn::Select, Fn::Join, Fn::Split, Fn::Replace, Fn::Base64Encode, Fn::Base64Decode, Fn::MemberListToMap, Fn::If, Fn::ListMerge, Fn::GetJsonValue, Fn::MergeMapToList, Fn::SelectMapList, Fn::Add, Fn::Avg, Fn::Str, Fn::Calculate, Ref (parameter references only), and Fn::FindInMap.
Examples
-
Define conditions
The following example defines four conditions based on an
EnvTypeparameter.DevEnvandUTEnveach check for a specific environment value.PREEnvis true when neitherDevEnvnorUTEnvis true.ProdEnvcombines an equality check withPREEnvusingFn::And, showing how to compose conditions from other conditions.Conditions: DevEnv: Fn::Equals: - Dev - Ref: EnvType UTEnv: Fn::Equals: - UT - Ref: EnvType PREEnv: Fn::Not: Fn::Or: - DevEnv - UTEnv ProdEnv: Fn::And: - Fn::Equals: - Prod - Ref: EnvType - PREEnv -
Associate conditions with resources and outputs
The following example uses
EnvTypeto control resource creation. WhenEnvTypeisprod, the template creates data disks for the Elastic Compute Service (ECS) instance and provisions an Object Storage Service (OSS) bucket. WhenEnvTypeis any other value, neither the data disks nor the OSS bucket are created, and the corresponding output is suppressed. The example demonstrates three ways to use a condition: -Fn::Ifon a property value — Conditionally setsDiskMappingson the ECS instance (WebServer). -Conditionkey on a resource — Gates creation of the entireOssBucketresource. -Conditionkey on an output — SuppressesOssDomainwhen the bucket does not exist.ROSTemplateFormatVersion: '2015-09-01' Parameters: EnvType: Default: pre Type: String Conditions: CreateProdRes: Fn::Equals: - prod - Ref: EnvType Resources: WebServer: Type: ALIYUN::ECS::Instance Properties: DiskMappings: Fn::If: - CreateProdRes - - Category: cloud_efficiency DiskName: FirstDataDiskName Size: 40 - Category: cloud_ssd DiskName: SecondDataDiskName Size: 40 - Ref: ALIYUN::NoValue VpcId: vpc-2zew9pxh2yirtzqxd**** SystemDiskCategory: cloud_efficiency SecurityGroupId: sg-2zece6wcqriejf1v**** SystemDiskSize: 40 ImageId: centos_6_8_64_40G_base_2017****.vhd IoOptimized: optimized VSwitchId: vsw-2zed9txvy7h2srqo6**** InstanceType: ecs.n1.medium OssBucket: Type: ALIYUN::OSS::Bucket Condition: CreateProdRes Properties: AccessControl: private BucketName: myprodbucket Outputs: InstanceId: Value: Fn::GetAtt: - WebServer - InstanceId OssDomain: Condition: CreateProdRes Value: Fn::GetAtt: - OssBucket - DomainName