調用內部函數Ref,返回指定參數或資源的值。
說明
不支持在Ref函數中使用任何函數,必須指定為資源名稱或參數的字符串。
函數聲明
JSON
{ "Ref": "logicalName" }
YAML
完整函數的語法。
Ref: logicalName
縮寫形式。
!Ref logicalName
參數信息
logicalName
:要引用的資源或參數的名稱。
返回值
資源或者參數對應的值。
使用示例
引用資源名稱
使用Ref來引用資源實例ID,如下是使用Ref函數指定EIP綁定的ECS實例ID。
Parameters:
MyEcsInstance:
Type: String
AssociationProperty: ALIYUN::ECS::Instance::InstanceId
Resources:
EIPAS:
Type: ALIYUN::VPC::EIPAssociation
Properties:
InstanceId: !Ref MyEcsInstance
"Parameters": {
"MyEcsInstance": {
"Type": "String",
"AssociationProperty": "ALIYUN::ECS::Instance::InstanceId"
}
},
"Resources": {
"EIPAS": {
"Type": "ALIYUN::VPC::EIPAssociation",
"Properties": {
"InstanceId": {
"Ref":"MyEcsInstance"
}
引用參數
下列使用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"
}
]
}
}
}
}
文檔內容是否對您有幫助?