Retorna o valor de um parâmetro, recurso ou variável local especificado.
Nota
A função Ref não aceita outras funções como entrada. O valor deve ser uma string que especifique o nome de um recurso ou parâmetro.
Declaração
-
JSON
{ "Ref": "logicalName" } -
YAML
-
Sintaxe do nome completo da função:
Ref: logicalName -
Sintaxe da forma abreviada:
!Ref logicalName
-
Parâmetros
logicalName: Nome do recurso ou parâmetro a referenciar.
Valor de retorno
O valor do recurso ou parâmetro.
Exemplos
Referenciar um nome de recurso
O exemplo a seguir usa Ref para obter o ID da VPC e associá-la a um 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"
}
}
}
}
Referenciar um nome de parâmetro
O exemplo a seguir usa Ref para passar o parâmetro regionParam à função FindInMap, que consulta o ID da imagem da instância 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"
}
]
}
}
}
}
Referenciar uma variável local (Locals)
O exemplo a seguir usa Ref para referenciar a variável local VpcCount, cujo valor é calculado por Fn::Add com base nos parâmetros P1 e 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"
}
}
}
}