Fungsi bawaan Ref digunakan untuk merujuk nilai parameter atau sumber daya.
Anda tidak dapat menggunakan fungsi lain di dalam fungsi Ref. Harus menetapkan Ref ke string yang mewakili nama sumber daya atau parameter.
Deklarasi
JSON
{ "Ref": "logicalName" }YAML
Sintaks untuk nama fungsi lengkap:
Ref: logicalNameSintaks untuk bentuk pendek:
!Ref logicalName
Parameter
logicalName: Nama sumber daya atau parameter yang dirujuk.
Nilai kembali
Nilai dari sumber daya atau parameter.
Contoh
Merujuk nama sumber daya
Pada contoh berikut, fungsi Ref digunakan untuk merujuk ID dari virtual private cloud (VPC) guna mengaitkan VPC dengan 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"
}
}
}
}Merujuk nama parameter
Pada contoh berikut, fungsi Ref digunakan untuk merujuk regionParam sebagai parameter wilayah untuk RegionMap dari WebServer:
ROSTemplateFormatVersion: '2015-09-01'
Parameters:
regionParam:
Description: wilayah tempat Anda ingin membuat Instance 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": "wilayah tempat Anda ingin membuat Instance 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"
}
]
}
}
}
}Rujuk variabel lokal (Locals)
Dalam contoh ini, fungsi `Ref` digunakan untuk merujuk variabel lokal `VpcCount`, yang menentukan jumlah VPC yang akan dibuat. Fungsi `Fn::Add` menghitung nilai `VpcCount` dengan menambahkan nilai dari parameter `P1` dan `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"
}
}
}
}