Ref

调用内部函数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"
          }
        ]
      }
    }
  }
}