Ref

更新时间:2025-04-17 06:56:44

调用内部函数Ref,返回指定参数或资源的值。

说明

不支持在Ref函数中使用任何函数,必须指定为资源名称或参数的字符串。

函数声明

  • JSON

    {
     "Ref": "logicalName"
    }
  • YAML

    • 完整函数的语法。

      Ref: logicalName
    • 缩写形式。

      !Ref logicalName

参数信息

logicalName:要引用的资源或参数的名称。

返回值

资源或者参数对应的值。

使用示例

引用资源名称
引用参数

使用Ref来引用VPC ID,如下是使用Ref函数指定VSwitch绑定的VPC实例ID。

YAML
JSON
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作为WebServerRegionMap的区域参数。

YAML
JSON
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"
          }
        ]
      }
    }
  }
}
  • 本页导读 (1)
  • 函数声明
  • 参数信息
  • 返回值
  • 使用示例
AI助理

点击开启售前

在线咨询服务

你好,我是AI助理

可以解答问题、推荐解决方案等