The built-in function Ref references the value of a parameter or resource.
You cannot use other functions in the Ref function. You must set Ref to a string that represents the name of a resource or parameter.
Declaration
JSON
{ "Ref": "logicalName" }YAML
Syntax for the full function name:
Ref: logicalNameSyntax for the short form:
!Ref logicalName
Parameters
logicalName: The name of the resource or parameter to reference.
Return value
The value of the resource or parameter.
Examples
Reference a resource name
In the following example, the Ref function is used to reference the ID of a virtual private cloud (VPC) to associate the VPC with a vSwitch:
ROSTemplateFormatVersion: '2015-09-01'
Parameters:
Resources:
Vpc:
Type: ALIYUN::ECS::VPC
Properties:
CidrBlock: 192.168.0.0/16
VpcName: MyVPC
OtherVSwitch:
Type: ALIYUN::ECS::VSwitch
Properties:
ZoneId: cn-beijing-h
VpcId: !Ref Vpc
CidrBlock: 192.168.1.0/24
VSwitchName: OtherSubnet{
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": null,
"Resources": {
"Vpc": {
"Type": "ALIYUN::ECS::VPC",
"Properties": {
"CidrBlock": "192.168.0.0/16",
"VpcName": "MyVPC"
}
},
"OtherVSwitch": {
"Type": "ALIYUN::ECS::VSwitch",
"Properties": {
"ZoneId": "cn-beijing-h",
"VpcId": {
"Ref": "Vpc"
},
"CidrBlock": "192.168.1.0/24",
"VSwitchName": "OtherSubnet"
}
}
}
}Reference a parameter name
In the following example, the Ref function is used to reference regionParam as the region parameter for RegionMap of WebServer:
ROSTemplateFormatVersion: '2015-09-01'
Parameters:
regionParam:
Description: the region where you want to create the ECS instance
Type: String
AllowedValues:
- hangzhou
- beijing
Mappings:
RegionMap:
hangzhou:
'32': m-25l0rcfjo
'64': m-25l0rcfj1
beijing:
'32': m-25l0rcfj2
'64': m-25l0rcfj3
Resources:
WebServer:
Type: ALIYUN::ECS::Instance
Properties:
ImageId:
!FindInMap
- RegionMap
- !Ref regionParam
- '32'
InstanceType: ecs.t1.small
SecurityGroupId: sg-25zwc****
ZoneId: cn-beijing-b
Tags:
- Key: tiantt
Value: ros
- Key: tiantt1
Value: ros1{
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"regionParam": {
"Description": "the region where you want to create the ECS instance",
"Type": "String",
"AllowedValues": [
"hangzhou",
"beijing"
]
}
},
"Mappings": {
"RegionMap": {
"hangzhou": {
"32": "m-25l0rcfjo",
"64": "m-25l0rcfj1"
},
"beijing": {
"32": "m-25l0rcfj2",
"64": "m-25l0rcfj3"
}
}
},
"Resources": {
"WebServer": {
"Type": "ALIYUN::ECS::Instance",
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionMap",
{
"Ref": "regionParam"
},
"32"
]
},
"InstanceType": "ecs.t1.small",
"SecurityGroupId": "sg-25zwc****",
"ZoneId": "cn-beijing-b",
"Tags": [
{
"Key": "key1",
"Value": "value1"
},
{
"Key": "key2",
"Value": "value2"
}
]
}
}
}
}Reference a local variable (Locals)
In this example, the `Ref` function is used to reference the local variable `VpcCount`, which specifies the number of VPCs to create. The `Fn::Add` function calculates the value of `VpcCount` by adding the values of the `P1` and `P2` parameters.
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{
"ROSTemplateFormatVersion": {},
"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"
}
}
}
}