调用内部函数Ref,返回指定参数或资源的值。
说明 
不支持在Ref函数中使用任何函数,必须指定为资源名称或参数的字符串。
函数声明
- JSON - { "Ref": "logicalName" }
- YAML - 完整函数的语法。 - Ref: logicalName
- 缩写形式。 - !Ref logicalName
 
参数信息
logicalName:要引用的资源或参数的名称。
返回值
资源或者参数对应的值。
使用示例
引用资源名称
使用Ref来引用VPC ID,如下是使用Ref函数指定VSwitch绑定的VPC实例ID。
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作为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"
          }
        ]
      }
    }
  }
}引用局部变量(Locals)
使用Ref来引用局部变量VpcCount,从而实现创建VPC的个数由Locals的VpcCount中的Fn::Add函数计算得出的,即参数P1和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"
      }
    }
  }
}该文章对您有帮助吗?