全部產品
Search
文件中心

Resource Orchestration Service:Ref

更新時間:Feb 05, 2024

調用內建函式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"
          }
        ]
      }
    }
  }
}