調用內建函式Ref,返回指定參數或資源的值。
說明
不支援在Ref函數中使用任何函數,必須指定為資源名稱或參數的字串。
函式宣告
JSON
{ "Ref": "logicalName" }YAML
完整函數的文法。
Ref: logicalName縮寫形式。
!Ref logicalName
參數資訊
logicalName:要引用的資源或參數的名稱。
傳回值
資源或者參數對應的值。
使用樣本
引用資源名稱
使用Ref來引用VPC ID,如下是使用Ref函數指定VSwitch綁定的VPC執行個體ID。
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"
}
}
}
}引用參數
下列使用Ref函數指定regionParam作為WebServer的RegionMap的地區參數。
ROSTemplateFormatVersion: '2015-09-01'
Parameters:
regionParam:
Description: 選擇建立ECS的地區
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": "選擇建立ECS的地區",
"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"
}
]
}
}
}
}引用局部變數(Locals)
使用Ref來引用局部變數VpcCount,從而實現建立VPC的個數由Locals的VpcCount中的Fn::AddFunction Compute得出的,即參數P1和P2的值相加的結果。
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"
}
}
}
}