規則(Rules)用於檢驗在建立或更新資源棧時傳遞給模板的參數值是否符合預期。
文法
您可以在ROS模板中使用關鍵字Rules來聲明模板規則。
您可以在ROS中定義多個規則,每個規則由規則名稱和規則描述組成。如果聲明多個規則項,規則項之間用半形逗號(,)分隔。
Rules:
規則1名稱:
RuleCondition: 規則條件內容
Assertions:
- Assert: 斷言內容
AssertDescription: 斷言描述
- Assert: 斷言內容
AssertDescription: 斷言描述
規則2名稱:
Assertions:
- Assert: 斷言內容 RuleCondition(可選):定義RuleCondition規則條件。如果未定義規則條件,則斷言始終生效。Assertions:定義Assertions斷言列表。由多個Assert和AssertDescription組成,用於描述斷言內容。說明Assertions斷言列表最多由100個Assert和AssertDescription組成。Assert:定義Assert斷言內容。AssertDescription(可選):定義AssertDescription斷言描述,當斷言結果為false時給出使用者提示。
每個規則中最多隻能定義一個RuleCondition,如果未定義RuleCondition或定義結果為true時,Assertions屬性生效,反之則不生效。
如果Assert返回結果為true,表示規則校正通過,您可以繼續完成資源棧的預覽、建立和更新操作。
重要
規則條件內容與斷言內容不支援引用Datasource資源類型做邏輯判斷。
樣本
當您在生產環境建立ECS執行個體時,ECS執行個體的公網頻寬必須為0。在預發環境建立ECS執行個體時,ECS執行個體的計費方式必須是隨用隨付。
ROSTemplateFormatVersion: '2015-09-01'
Rules:
PublicNet:
RuleCondition:
Fn::Equals:
- Ref: Environment
- prod
Assertions:
- Assert:
Fn::Equals:
- Ref: InternetMaxBandwidthOut
- 0
AssertDescription: ECS instance should be intranet when the environment is prod.
ChargeType:
RuleCondition:
Fn::Equals:
- Ref: Environment
- pre
Assertions:
- Assert:
Fn::Equals:
- Ref: InstanceChargeType
- PayAsYouGo
AssertDescription: ECS instance should be postpaid when the environment is pre.
Parameters:
Environment:
Type: String
AllowedValues:
- prod
- pre
InternetMaxBandwidthOut:
Type: Number
MaxValue: 10
MinValue: 0
InstanceChargeType:
Type: String
AllowedValues:
- PayAsYouGo
- Subscription
Resources:
ECS:
Type: ALIYUN::ECS::InstanceGroup
Properties:
InternetMaxBandwidthOut:
Ref: InternetMaxBandwidthOut
InstanceChargeType:
Ref: InstanceChargeType
#省略其他屬性: null