All Products
Search
Document Center

Resource Orchestration Service:Ref

Last Updated:Mar 15, 2024

The built-in function Ref references the value of a parameter or resource.

Note

You cannot use other functions in the Ref function. You must set Ref to a string that represents the name of a resource or parameter.

Declaration

  • JSON

    {
     "Ref": "logicalName"
    }
  • YAML

    • Syntax for the full function name:

      Ref: logicalName
    • Syntax for the short form:

      !Ref logicalName

Parameters

logicalName: the name of the resource or parameter that you want to reference.

Return value

The value of the resource or parameter.

Examples

Reference a resource name

In the following example, the Ref function is used to reference the ID of an Elastic Compute Service (ECS) to associate the instance with an elastic IP address (EIP):

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"
      }

Reference a parameter name

In the following example, the Ref function is used to reference regionParam as the region parameter for RegionMap of WebServer:

ROSTemplateFormatVersion: '2015-09-01'
Parameters:
  regionParam:
    Description: the region where you want to create the ECS instance
    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": "the region where you want to create the ECS instance",
      "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"
          }
        ]
      }
    }
  }
}