Fn::If returns one of two values based on whether a specified condition evaluates to true or false.
Note
The Resources and Outputs sections of templates support Fn::If. You can use the pseudo parameter ALIYUN::NoValue as the return value to remove a property.
Declaration
-
JSON
{ "Fn::If": [ "condition_name", "value_if_true", "value_if_false" ] } -
YAML
-
Syntax for the full function name:
Fn::If: - condition_name - value_if_true - value_if_false -
Syntax for the short form:
!If [condition_name, value_if_true, value_if_false]
-
Parameters
-
condition_name: the name of a condition defined in the Conditions section. -
value_if_true: the value returned when the condition evaluates to true. -
value_if_false: the value returned when the condition evaluates to false.
Examples
The following example determines whether to create data disks based on input parameters:
ROSTemplateFormatVersion: '2015-09-01'
Parameters:
EnvType:
Default: pre
Type: String
Conditions:
CreateDisk:
!Equals
- prod
- !Ref EnvType
Resources:
WebServer:
Type: ALIYUN::ECS::Instance
Properties:
DiskMappings:
!If
- CreateDisk
- - Category: cloud_efficiency
DiskName: FirstDataDiskName
Size: 40
- Category: cloud_ssd
DiskName: SecondDataDiskName
Size: 40
- !Ref ALIYUN::NoValue{
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"EnvType": {
"Default": "pre",
"Type": "String"
}
},
"Conditions": {
"CreateDisk": {
"Fn::Equals": [
"prod",
{
"Ref": "EnvType"
}
]
}
},
"Resources": {
"WebServer": {
"Type": "ALIYUN::ECS::Instance",
"Properties": {
"DiskMappings": {
"Fn::If": [
"CreateDisk",
[
{
"Category": "cloud_efficiency",
"DiskName": "FirstDataDiskName",
"Size": 40
},
{
"Category": "cloud_ssd",
"DiskName": "SecondDataDiskName",
"Size": 40
}
],
{
"Ref": "ALIYUN::NoValue"
}
]
}
}
}
}
}