When you create or update a stack based on a template, you can use the Rules section to check whether the values that are passed to the parameters in the template are valid.

Syntax

You can declare one or more rules in the Rules section of a Resource Orchestration Service (ROS) template.

Each rule consists of a rule name and a rule description. Separate multiple rules with commas (,).

Rules:
  Rule Name 1:
    RuleCondition: the condition of the rule
    Assertions:
      - Assert: the assertion of the rule
        AssertDescription: the description of the assertion
      - Assert: the assertion of the rule
        AssertDescription: the description of the assertion
  Rule Name 2:
    Assertions:
      - Assert: the assertion of the rule
  • RuleCondition: Optional. The condition that determines when the rule takes effect. If you leave the RuleCondition parameter empty, the assertions of the rule take effect all the time.
  • Assertions: the list of assertions that declare the values you can specify for a parameter.
    Note The list of assertions consists of up to 100 pairs of Assert and AssertDescription parameters.
  • Assert: the assertion of the rule.
  • AssertDescription: Optional. The description of the assertion. The assertion description is prompted if the result of the assertion is false.

You can define only one RuleCondition parameter for each rule. The Assertions parameter takes effect only when you leave the RuleCondition parameter empty or the result of the RuleCondition parameter is true.

If the result of the Assert parameter is true, the verification is passed. In this case, you can continue to preview, create, and update the stack.

Example

The following sample code provides an example on how to configure the Rules section. In this example, the public bandwidth of the Elastic Compute Service (ECS) instance that you want to create in the production environment must be 0, and the pay-as-you-go billing method must be used for the ECS instance that you want to create in the staging environment.
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
      #Other properties are omitted: null