Returns the value of a specified parameter, resource, or local variable.
The Ref function does not accept other functions as input. The value must be a string that specifies the name of a resource or parameter.
Declaration
-
JSON
{ "Ref": "logicalName" } -
YAML
-
Syntax for the full function name:
Ref: logicalName -
Syntax 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
The following example uses Ref to obtain the VPC ID and 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
The following example uses Ref to pass the regionParam parameter to the FindInMap function, which looks up the image ID for the WebServer instance:
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)
The following example uses Ref to reference the local variable VpcCount, whose value is computed by Fn::Add from 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"
}
}
}
}