The Locals section in a Resource Orchestration Service (ROS) template lets you abstract and encapsulate common logic or value calculations into reusable local variables. This improves template maintainability, consistency, and reduces code redundancy.
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
Type
No
The type of the local variable. Valid values:
-
Macro (default): macro substitution. The system substitutes the values of local variables directly without calculating actual values.
-
Eval: value calculation. The system calculates the actual values of local variables before substitution.
-
DataSource resource that you specify: data source resource. Data source resources query resource data from cloud services, and the queried data can be used only by local variables. For example, you can specify
Type: DATASOURCE::VPC::Vpcsfor 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.
-
The Locals section has the following limits:
-
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: Parameters are external inputs that receive user or environment configurations. Local variables are internal calculations that store and reuse computed values within the template.
Examples
Macro substitution (Macro)
-
In this example, the Description local variable stores a value that is referenced as the Description property of a virtual private cloud (VPC). This avoids hardcoding the same value in multiple places.
ROSTemplateFormatVersion: 2015-09-01 Locals: Description: Value: test Resources: Vpc: Type: ALIYUN::ECS::VPC Properties: Description: Ref: Description -
In this example, the VpcCount local variable is calculated by using the Fn::Add function to sum the P1 and P2 parameters. Unlike parameters, this local variable is an internal calculation rather 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 uses a data source resource to query VPCs where VpcName is test. The CreateVpc local variable then evaluates whether a new VPC needs to 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