You can use Fn::And, Fn::Or, Fn::Not, or Fn::Equals to define a condition statement. In each condition, you can reference another condition, a parameter value, or a mapping. You can associate conditions with resources in the Resources and Outputs sections of a template. To associate conditions with resources, you can use the Fn::If function or the Condition property of the resources.
Syntax
The declaration of a condition consists of a condition name and a condition statement. The condition name is of the String type. You can use the following functions to define condition statements. You can reference other conditions in a condition. Each condition name must be unique.
You can use the following functions to define conditions, but cannot use the functions as the outermost functions:
"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"(only for parameter references), and "Fn::FindInMap".
Examples
The following example shows how to define 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
The following example shows how to associate conditions with resources.
In this example, the EnvType value is used as a condition to determine whether to create a data disk and an Object Storage Service (OSS) bucket for an Elastic Compute Service (ECS) instance.
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