すべてのプロダクト
Search
ドキュメントセンター

Resource Orchestration Service:Ref

最終更新日:Oct 16, 2025

組み込み関数 Ref は、パラメーターまたはリソースの値を参照します。

説明

Ref 関数では他の関数を使用できません。Ref は、リソースまたはパラメーターの名前を表す文字列に設定する必要があります。

宣言

  • JSON

    {
     "Ref": "logicalName"
    }
  • YAML

    • 完全な関数名の構文:

      Ref: logicalName
    • 短縮形の構文:

      !Ref logicalName

パラメーター

logicalName: 参照するリソースまたはパラメーターの名前。

戻り値

リソースまたはパラメーターの値。

リソース名を参照する

次の例では、Ref 関数を使用して仮想プライベートクラウド (VPC) の ID を参照し、VPC を vSwitch に関連付けています。

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 関数を使用して、WebServer の RegionMap のリージョンパラメーターとして regionParam を参照しています。

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": "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 の数を指定します。`Fn::Add` 関数は、`P1` と `P2` パラメーターの値を加算して `VpcCount` の値を計算します。

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