When you create a template in Resource Orchestration Service (ROS), you can use local variables in the Locals section to effectively abstract and encapsulate common feature logic or value calculation rules. This improves maintainability and consistency and reduces code redundancy for your template.
Syntax
The Locals section consists of local variable names and local variable fields.
A local variable name must contain letters and digits, and cannot be the same as other local variable names, parameter names, or resource names in a template.
The following table describes local variable fields.
Local variable field | Required | Description |
Local variable field | Required | Description |
Type | No | The type of the local variable. Valid values: Macro (default): macro substitution. The system directly substitutes the values of local variables without calculating the actual values. Eval: value calculation. The system substitutes the values of local variables with calculating the actual values. DataSource resource that you specify: data source resource. You can use data source resources to query the resource data of cloud services. The queried data can be used only by local variables. For example, you can specify Type: DATASOURCE::VPC::Vpcs for a local variable.
|
Value | No | The value of the local variable. You must specify this field when the Type field of the local variable is set to Macro or Eval. |
Properties | No | The properties of the data source resource. |
Note
The following limits are imposed on the Locals section:
You cannot use the Locals section for nested stacks.
The Locals section supports up to five levels of nesting.
You can use only Ref to reference a local variable. The ${LocalName} method is not supported.
When the Type field in the Locals section is set to Eval, the Value field can contain references only to parameters in the Parameters section and local variables in the Locals section, and functions can be used.
Differences between Parameters and Locals: The parameters in the Parameters section are external inputs that are used to receive user or environment configurations. The local variables in the Locals section are internal calculations that are used to configure storage and reuse values in ROS.
Examples
Macro substitution (Macro)
In this example, a value is specified for the Description local variable, and the value is used as the value of the Description property of a virtual private cloud (VPC) when you create the VPC. This improves maintainability and consistency and reduces code redundancy for your template.
ROSTemplateFormatVersion: 2015-09-01
Locals:
Description:
Value: test
Resources:
Vpc:
Type: ALIYUN::ECS::VPC
Properties:
Description:
Ref: Description
In this example, the value of the VpcCount local variable is calculated by using the Fn::Add function. That is, the variable value is the sum of the P1 and P2 parameters. The local variable is an internal calculation in the template other than a user input.
ROSTemplateFormatVersion: 2015-09-01
Parameters:
P1:
Type: Number
Default: 1
P2:
Type: Number
Default: 2
Locals:
VpcCount:
Value:
Fn::Add:
- Ref: P1
- Ref: P2
Resources:
Vpc:
Type: ALIYUN::ECS::VPC
Count:
Ref: VpcCount
Value calculation (Eval)
In this example, the Vpcs
local variable of the data source resource type is specified to query the data of a VPC for which the VpcName
property is set to test
. The value of the CreateVpc
local variable is calculated to determine whether the VPC can be created.
ROSTemplateFormatVersion: "2015-09-01"
Locals:
Vpcs:
Type: DATASOURCE::VPC::Vpcs
Properties:
VpcName: test
CreateVpc:
Type: Eval
Value:
Fn::Equals:
- Fn::Length:
Ref: Vpcs
- 0
Conditions:
CreateVpc:
Ref: CreateVpc
Resources:
Vpc:
Condition: CreateVpc
Type: ALIYUN::ECS::VPC