You can conditionally show or hide template parameters based on the value of another parameter. For example, you can display the period unit and subscription period only when the billing method is set to subscription.
Template syntax
Specify AssociationProperty and AssociationPropertyMetadata for a parameter to control its visibility based on another parameter's value. In AssociationPropertyMetadata, define a Visible field that uses a function to determine whether to display the parameter. The syntax is as follows:
Visible:
Condition: Object # Condition to display this parameter.
The Condition field supports the following functions:
Template example
In this example, AssociationProperty is set on the targetInstanceChargeType parameter to make it the controlling parameter for the billing method. AssociationPropertyMetadata is then used on dependent parameters to define their visibility conditions. This enables dynamic parameter filtering during stack creation.
ROSTemplateFormatVersion: '2015-09-01'
Description:
zh-cn: 单实例自定义镜像模板示例,支持创建基础资源配置(VPC、VSwitch、SecurityGroup)或者选择已有基础资源配置,并支持配置ECS的计费方式为包年包月或者按量付费。
en: An example template for creating a single ECS instance from a custom image. You can either create new base resources (VPC, VSwitch, security group) or use existing ones. Supports both subscription and pay-as-you-go billing methods for the ECS instance.
Parameters:
targetInstanceChargeType:
Description:
name-en: targetInstanceChargeType
name-zh-cn: 计费方式
Type: String
AssociationProperty: ChargeType
targetPeriodUnit:
Description:
name-en: The unit of the subscription period
name-zh-cn: 包年包月计费方式的时长单位
Type: String
AllowedValues:
- Week
- Month
Default: Month
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Not:
Fn::Equals:
- ${targetInstanceChargeType}
- PostPaid
targetPeriod:
Description:
name-en: The subscription period of the instance
name-zh-cn: 购买包年包月资源的时长
en: 'Valid values: 1, 2, 3, and 4 if PeriodUnit is Week. 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 24, 36, 48, and 60 if PeriodUnit is Month.'
zh-cn: 取值:PeriodUnit取值为Week时:1、2、3、4。PeriodUnit取值为Month时:1、2、3、4、5、6、7、8、9、12、24、36、48、60。
Type: Number
AllowedValues:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 12
- 24
- 36
- 48
- 60
Default: 1
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Not:
Fn::Equals:
- ${targetInstanceChargeType}
- PostPaid
Parameters:
targetInstanceChargeType: The billing method.AssociationPropertyis set toChargeType, which means the visibility of other parameters depends on this parameter's value.targetPeriodUnit: The period unit.AssociationPropertyMetadatais set to{"Visible": {"Condition": {"Fn::Not": {"Fn::Equals": ["${targetInstanceChargeType}","PostPaid"]}}}}. This condition displays thetargetPeriodUnitparameter only whentargetInstanceChargeTypeis not set toPostPaid.Note The targetInstanceChargeType parameter accepts PrePaid for the subscription billing method and PostPaid for the pay-as-you-go billing method.targetPeriod: The subscription period.AssociationPropertyMetadatais set to{"Visible": {"Condition": {"Fn::Not": {"Fn::Equals": ["${targetInstanceChargeType}","PostPaid"]}}}}. This condition displays thetargetPeriodparameter only whentargetInstanceChargeTypeis not set toPostPaid.
Configuration in the console
When you create a stack from this template with the subscription billing method, the ROS console displays the following dependent parameters: PeriodUnit (with options for Week or Month) and Subscription period. If PeriodUnit is set to Week, the valid values for the subscription period are 1, 2, 3, and 4. If PeriodUnit is set to Month, the valid values are 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 24, 36, 48, and 60.