The built-in function Fn::If returns one of two possible values. The function returns one value if a specified condition is evaluated to true, and returns the other value if not.
The property values in the Resources and Outputs sections of templates support the Fn::If function. You can use the pseudo parameter ALIYUN::NoValue
as the return value to delete the 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 the condition in the Conditions section. You can reference a condition by using the condition name.value_if_true
: the value returned when the specified condition is evaluated to true.value_if_false
: the value returned when the specified condition is evaluated to false.
Examples
In the following example, whether a data disk can be created is determined 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"
}
]
}
}
}
}
}