Rules

更新时间:
复制 MD 格式

Use the Rules section in a Resource Orchestration Service (ROS) template to validate parameter values before a stack is created or updated. Rules catch invalid configurations early — for example, when two parameters must be consistent with each other, such as requiring a pay-as-you-go billing method in a staging environment.

Syntax

Declare one or more named rules in the Rules section of a template. 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.      
  • (Optional) RuleCondition: The condition that determines when the rule takes effect. If omitted, the assertions always apply.

  • Assertions: One or more statements that define the acceptable values for a parameter.

    Note

    Each Assertions block can contain up to 100 Assert and AssertDescription pairs.

  • Assert: The assertion expression. Must evaluate to true or false.

  • (Optional) AssertDescription: The message shown when an assertion fails. Use an actionable message that tells users what value to provide — for example, "The ECS instance must use pay-as-you-go billing in the staging environment."

You can define only one RuleCondition for each rule. Assertions takes effect only when RuleCondition is omitted or evaluates to true. When Assert evaluates to true, the parameter values pass validation and the stack can proceed to preview, create, or update.

Important

DataSource resources cannot be referenced in rule conditions or assertions.

Example

Environment-based parameter constraints

The following example enforces environment-specific constraints using two rules:

  • In the production environment (prod), the ECS instance must use intranet-only access (Internet bandwidth set to 0).

  • In the staging environment (pre), the ECS instance must use pay-as-you-go billing.

    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 (omitted here): null.

Each rule uses Fn::Equals to check the Environment parameter and activates its assertions only when the condition is met. If a user sets Environment to prod but specifies a non-zero bandwidth, stack creation fails with the AssertDescription message.