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

Resource Orchestration Service:ECS インスタンスグループを作成し、CLB インスタンスにアタッチする

最終更新日:Jun 19, 2026

このドキュメントでは、Resource Orchestration Service (ROS) テンプレートを編集して Elastic Compute Service (ECS) インスタンスグループを作成し、Classic Load Balancer (CLB) インスタンスにアタッチする方法について説明します。

前提条件

ROSテンプレートの構文と構造に精通しています。 詳細については、「テンプレートの使用開始方法」をご参照ください。

シナリオ例

Virtual Private Cloud (VPC) に ECS インスタンスグループを作成し、インスタンスグループ内に Nginx サービスをデプロイしてから、インスタンスグループを CLB インスタンスにアタッチします。

1

使用上の注意

上記のリソースのリソースタイプを表示して、各リソースプロパティの詳細を取得できます。 詳細については、「リソースタイプの表示」をご参照ください。

リソースタイプは、タイプ、必要かどうか、編集可能かどうかなど、リソースのプロパティを宣言します。 プロパティが必要な場合は、テンプレートの [リソース] セクションの [プロパティ] 部分でプロパティを宣言する必要があります。 プロパティがオプションの場合は、プロパティを空のままにすることができます。 プロパティが編集可能な場合は、スタックテンプレートのクラウドリソースにプロパティを指定した後で、新しいテンプレートのプロパティを変更できます。 次に、新しいテンプレートを使用してスタックとリソースを更新できます。 それ以外の場合は、プロパティを変更できません。

テンプレートの編集

リソースタイプリファレンスで必要なリソースタイプを検索してください。詳細については、「リソースタイプリファレンス」をご参照ください。

このシナリオでは、VPC (ALIYUN::ECS::VPC)、ECS インスタンスグループ (ALIYUN::ECS::InstanceGroup)、Server Load Balancer (SLB) インスタンス (ALIYUN::SLB::LoadBalancer)、SLB リスナー (ALIYUN::SLB::Listener)、Elastic IP アドレス (EIP) (ALIYUN::VPC::EIP)、vSwitch (ALIYUN::ECS::VSwitch)、およびセキュリティグループ (ALIYUN::ECS::SecurityGroup) のリソースが必要です。ECS インスタンスグループを作成するときに、ALIYUN::ECS::InstanceGroup のユーザーデータパラメーターを使用して初期化コマンドを実行できます。待機条件リソース (ALIYUN::ROS::WaitCondition) と待機条件ハンドルリソース (ALIYUN::ROS::WaitConditionHandle) を使用して、ECS インスタンスグループ内の実行フローを制御できます。

これらのリソースをテンプレートの Resources セクションで定義します。

テンプレートリソースとその依存関係の定義

基本ネットワークリソースの定義

VpcVSwitchEcsSecurityGroup などの基本ネットワークリソースを定義します。

  • Ref 関数と ALIYUN::StackName 疑似パラメーターを使用してスタック名を取得し、リソースプロパティの値として使用します。たとえば、この方法を、Vpc リソースの VpcName プロパティと VSwitch リソースの VSwitchName プロパティで使用します。詳細については、「関数」および「ALIYUN::StackName」をご参照ください。

  • Ref 関数を使用して、指定したリソースの戻り値を取得します。たとえば、VSwitch リソースの Ref: Vpc は、Vpc リソースの VpcId 戻り値を取得します。戻り値は通常、リソース ID です。詳細については、「関数」をご参照ください。

Resources:
  Vpc:
    Type: ALIYUN::ECS::VPC
    Properties:
      CidrBlock: 192.168.0.0/16
      VpcName:
        Ref: ALIYUN::StackName
  VSwitch:
    Type: ALIYUN::ECS::VSwitch
    Properties:
      VSwitchName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      ZoneId:
        Ref: ZoneId
      CidrBlock: 192.168.0.0/24
  EcsSecurityGroup:
    Type: ALIYUN::ECS::SecurityGroup
    Properties:
      SecurityGroupName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      SecurityGroupIngress:
        - PortRange: 80/80
          Priority: 1
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
          NicType: internet
      SecurityGroupEgress:
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: internet
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: intranet

SLB インスタンスと EIP リソースの定義

SLB インスタンス Slb、SLB リスナー SlbListener、および EIP リソース EipSlbAddress を定義します。

Fn::Sub 関数を使用して、ALIYUN::StackName 疑似パラメーターを新しい文字列に連結し、リソースプロパティの値として使用します。詳細については、「ALIYUN::StackName」をご参照ください。

Resources:
  Slb:
    Type: ALIYUN::SLB::LoadBalancer
    Properties:
      VpcId:
        Ref: Vpc
      VSwitchId:
        Ref: VSwitch
      LoadBalancerName:
        Fn::Sub: slb-${ALIYUN::StackName}
      PayType:
        Ref: PayType
      PricingCycle:
        Ref: PayPeriodUnit
      Duration:
        Ref: PayPeriod
      AddressType: intranet
      LoadBalancerSpec:
        Ref: LoadBalancerSpec
      AutoPay: true
  SlbListener:
    DependsOn:
      - Slb
    Type: ALIYUN::SLB::Listener
    Properties:
      Persistence:
        CookieTimeout: 60
        StickySession: 'on'
        PersistenceTimeout: 180
        XForwardedFor: 'off'
        StickySessionType: insert
      ListenerPort: 80
      Bandwidth: -1
      HealthCheck:
        HttpCode: http_2xx,http_3xx,http_4xx,http_5xx
        HealthCheckType: tcp
        UnhealthyThreshold: 3
        Timeout: 5
        HealthyThreshold: 3
        Port: 80
        URI: /
        Interval: 2
      LoadBalancerId:
        Ref: Slb
      BackendServerPort: 80
      Protocol: tcp
  EipSlbAddress:
    Type: ALIYUN::VPC::EIP
    Properties:
      Name:
        Ref: ALIYUN::StackName
      InternetChargeType: PayByTraffic
      Bandwidth:
        Ref: Bandwidth

ECS インスタンスグループ、待機条件リソース、および待機条件ハンドルリソースの定義

ECS インスタンスグループ EcsInstanceGroup、待機条件リソース WaitCondition、および待機条件ハンドルリソース WaitConditionHandle を定義します。

  • Fn::GetAtt 関数を使用して、テンプレート内のリソースから属性の値を取得します。たとえば、WaitConditionHandle リソースの CurlCli 戻り値を取得できます。詳細については、「関数」をご参照ください。

  • Fn::Sub 関数を使用してコマンド文字列を構築し、入力文字列内の変数を指定された値に置き換えます。たとえば、EcsInstanceGroup リソースのユーザーデータプロパティでは、${CurlCli}CurlCli の値に置き換えられます。詳細については、「関数」をご参照ください。

  • 以下のリソースの組み合わせが、ECS インスタンス内の初期化プロセスを制御します。

    1. 待機条件リソース (ALIYUN::ROS::WaitCondition) を作成します。

    2. 待機条件ハンドルリソース (ALIYUN::ROS::WaitConditionHandle) を作成します。

    3. ECS インスタンスのユーザーデータでスクリプトを実行し、シグナルを送信します。たとえば、EcsInstanceGroup リソースのユーザーデータコンテンツには、ECS インスタンスの初期化スクリプトが含まれています。スクリプトが正常に実行された後、WaitConditionHandle リソースにコールバックを送信します。これにより、WaitCondition リソースの待機状態が終了します。

Resources:
  WaitCondition:
    Type: ALIYUN::ROS::WaitCondition
    Properties:
      Count: 1
      Handle:
        Ref: WaitConditionHandle
      Timeout: 300
  WaitConditionHandle:
    Type: ALIYUN::ROS::WaitConditionHandle
  EcsInstanceGroup:
    Type: ALIYUN::ECS::InstanceGroup
    Properties:
      InstanceChargeType:
        Ref: PayType
      PeriodUnit:
        Ref: PayPeriodUnit
      Period:
        Ref: PayPeriod
      IoOptimized: optimized
      SystemDiskCategory:
        Ref: SystemDiskCategory
      SystemDiskSize:
        Ref: SystemDiskSize
      DiskMappings:
        - Category:
            Ref: DataDiskCategory
          Size:
            Ref: DataDiskSize
      VpcId:
        Ref: Vpc
      SecurityGroupId:
        Ref: EcsSecurityGroup
      VSwitchId:
        Ref: VSwitch
      MaxAmount: 2
      ImageId: centos_7
      InstanceType:
        Ref: EcsInstanceType
      Password:
        Ref: InstancePassword
      AllocatePublicIP: false
      UserData:
        Fn::Sub:
          - |
            #!/bin/bash
            # ディスクを /disk1 にマウントする
            cat >> /root/InitDataDisk.sh << EOF
            #!/bin/bash
            echo "p
            n
            p
            w
            " |  fdisk -u /dev/vdb
            EOF
            /bin/bash /root/InitDataDisk.sh
            rm -f /root/InitDataDisk.sh
            rm -f InitDataDisk.sh
            mkfs -t ext4 /dev/vdb1
            cp /etc/fstab /etc/fstab.bak
            mkdir /disk1
            echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\\\"//g'` /disk1 ext4 defaults 0 0 >> /etc/fstab
            mount -a
            # インストールスクリプトを設定する
            yum install -y nginx
            # 起動スクリプトを設定する
            systemctl start nginx.service
            # WaitConditionHandle に成功シグナルを送信して WaitCondition の待機を終了させる
            ${CurlCli} -d "{\"Data\" : \"Success\", \"status\" : \"SUCCESS\"}"
          - CurlCli:
              Fn::GetAtt:
                - WaitConditionHandle
                - CurlCli
      

ECS インスタンスグループ、SLB インスタンス、およびバックエンドサーバーのアタッチメントの依存関係の定義

ECS インスタンスグループ EcsInstanceGroup、SLB インスタンス Slb、およびバックエンドサーバーのアタッチメント SlbBackendServerAttachment を定義します。

  • SlbBackendServerAttachment を使用して、ECS インスタンスグループを CLB インスタンスにアタッチします。詳細については、「ALIYUN::SLB::BackendServerAttachment」をご参照ください。

    ALIYUN::SLB::BackendServerAttachment で、BackendServerWeightList の値を指定しない場合、BackendServerList 内のすべての ECS インスタンスの重みはデフォルトで 100 になります。BackendServerWeightList のエントリ数が BackendServerList より少ない場合、BackendServerWeightList の最後の重みの値が BackendServerList の残りのすべての ECS インスタンスに適用されます。

  • DependsOn プロパティを使用して、あるリソースが別のリソースの作成後にのみ作成されるように指定します。詳細については、「DependsOn」をご参照ください。

  • Fn::GetAtt 関数を使用して、リソースの出力プロパティの値を取得します。たとえば、EcsInstanceGroup リソースから InstanceIds を取得できます。詳細については、「関数」をご参照ください。

Resources:
  EcsInstanceGroup:
    Type: ALIYUN::ECS::InstanceGroup
    Properties:
      InstanceChargeType:
        Ref: PayType
      PeriodUnit:
        Ref: PayPeriodUnit
      Period:
        Ref: PayPeriod
      IoOptimized: optimized
      SystemDiskCategory:
        Ref: SystemDiskCategory
      SystemDiskSize:
        Ref: SystemDiskSize
      DiskMappings:
        - Category:
            Ref: DataDiskCategory
          Size:
            Ref: DataDiskSize
      VpcId:
        Ref: Vpc
      SecurityGroupId:
        Ref: EcsSecurityGroup
      VSwitchId:
        Ref: VSwitch
      MaxAmount: 2
      ImageId: centos_7
      InstanceType:
        Ref: EcsInstanceType
      Password:
        Ref: InstancePassword
      AllocatePublicIP: false
  Slb:
    Type: ALIYUN::SLB::LoadBalancer
    Properties:
      VpcId:
        Ref: Vpc
      VSwitchId:
        Ref: VSwitch
      LoadBalancerName:
        Fn::Sub: slb-${ALIYUN::StackName}
      PayType:
        Ref: PayType
      PricingCycle:
        Ref: PayPeriodUnit
      Duration:
        Ref: PayPeriod
      AddressType: intranet
      LoadBalancerSpec:
        Ref: LoadBalancerSpec
      AutoPay: true
  SlbBackendServerAttachment:
    DependsOn:
      - EcsInstanceGroup
      - Slb
    Type: ALIYUN::SLB::BackendServerAttachment
    Properties:
      BackendServerList:
        Fn::GetAtt:
          - EcsInstanceGroup
          - InstanceIds
      LoadBalancerId:
        Ref: Slb
      BackendServerWeightList:
        - 100
        - 50

完全なテンプレートサンプル

ROSTemplateFormatVersion: '2015-09-01'
Description:
  en: Create a new VPC and vSwitch, create one CLB instance, two ECS instances, and attach all ECS instances to the CLB instance.
  ja: 新しい VPC と vSwitch を作成し、CLB インスタンスを 1 つ、ECS インスタンスを 2 つ作成し、すべての ECS インスタンスを CLB インスタンスにアタッチします。
  zh-cn: Create a new VPC and vSwitch, create one CLB instance, two ECS instances, and attach all ECS instances to the CLB instance.
Parameters:
  ZoneId:
    Type: String
    AssociationProperty: ALIYUN::ECS::Instance::ZoneId
    Label:
      en: vSwitch Zone
      ja: vSwitch ゾーン
      zh-cn: vSwitch Zone
  VpcCidrBlock:
    Default: 192.168.0.0/16
    Label:
      zh-cn: VPC CIDR Block
      en: VPC CIDR Block
      ja: VPC CIDR ブロック
    Type: String
    Description:
      zh-cn: The CIDR block for the new VPC. The following CIDR blocks are recommended<font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>
      en: The CIDR block for the new VPC. The following CIDR blocks are recommended<br><font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>
      ja: 新しい VPC の CIDR ブロック。次の CIDR ブロックを推奨します:<font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>
  VSwitchCidrBlock:
    Default: 192.168.0.0/24
    Type: String
    Description:
      zh-cn: Must be a subnet of the VPC and not used by other vSwitches.
      en: Must be a subnet of the VPC and not used by other vSwitches.
      ja: VPC のサブネットであり、他の vSwitch で使用されていないことが必要です。
    Label:
      zh-cn: vSwitch CIDR Block
      en: vSwitch CIDR Block
      ja: vSwitch CIDR ブロック
  EcsInstanceType:
    Type: String
    Label:
      en: Instance Type
      ja: インスタンスタイプ
      zh-cn: Instance Type
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceChargeType: ${InstanceChargeType}
  SystemDiskCategory:
    Type: String
    Description:
      en: '<font color=''blue''><b>Valid values:</font>[cloud_efficiency: <font color=''green''>ultra disk</font>]<br>[cloud_ssd: <font color=''green''>standard SSD</font>]<br>[cloud_essd: <font color=''green''>enterprise SSD</font>]<br>[cloud: <font color=''green''>basic disk</font>]<br>[ephemeral_ssd: <font color=''green''>local SSD</font>]'
      ja: '<font color=''blue''><b>有効値:</b></font><br>[cloud_efficiency: <font color=''green''>ウルトラディスク</font>]<br>[cloud_ssd: <font color=''green''>標準 SSD</font>]<br>[cloud_essd: <font color=''green''>エンタープライズ SSD</font>]<br>[cloud: <font color=''green''>ベーシックディスク</font>]<br>[ephemeral_ssd: <font color=''green''>ローカル SSD</font>]'
      zh-cn: '<font color=''blue''><b>Valid values:</font>[cloud_efficiency: <font color=''green''>ultra disk</font>]<br>[cloud_ssd: <font color=''green''>standard SSD</font>]<br>[cloud_essd: <font color=''green''>enterprise SSD</font>]<br>[cloud: <font color=''green''>basic disk</font>]<br>[ephemeral_ssd: <font color=''green''>local SSD</font>]'
    AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceType: ${EcsInstanceType}
    Label:
      en: System Disk Type
      ja: システムディスクタイプ
      zh-cn: System Disk Type
  SystemDiskSize:
    Default: 40
    Type: Number
    Description:
      zh-cn: The size of the system disk. Valid values: 40 to 500. Unit: GB.
      ja: システムディスクのサイズ。有効値:40~500。単位: GB。
      en: The size of the system disk. Valid values: 40 to 500. Unit: GB.
    Label:
      zh-cn: System Disk Size
      en: System Disk Size
      ja: システムディスクサイズ
  DataDiskCategory:
    AssociationProperty: ALIYUN::ECS::Disk::DataDiskCategory
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceType: ${EcsInstanceType}
    Type: String
    Description:
      zh-cn: '<font color=''blue''><b>Valid values:</font>[cloud_efficiency: <font color=''green''>ultra disk</font>]<br>[cloud_ssd: <font color=''green''>standard SSD</font>]<br>[cloud_essd: <font color=''green''>enterprise SSD</font>]<br>[cloud: <font color=''green''>basic disk</font>]'
      ja: '<font color=''blue''><b>有効値:</b></font><br>[cloud_efficiency: <font color=''green''>ウルトラディスク</font>]<br>[cloud_ssd: <font color=''green''>標準 SSD</font>]<br>[cloud_essd: <font color=''green''>エンタープライズ SSD</font>]<br>[cloud: <font color=''green''>ベーシックディスク</font>]'
      en: '<font color=''blue''><b>Valid values:</font>[cloud_efficiency: <font color=''green''>ultra disk</font>]<br>[cloud_ssd: <font color=''green''>standard SSD</font>]<br>[cloud_essd: <font color=''green''>enterprise SSD</font>]<br>[cloud: <font color=''green''>basic disk</font>]'
    Label:
      zh-cn: Data Disk Type
      en: Data Disk Type
      ja: データディスクタイプ
  DataDiskSize:
    Description:
      zh-cn: The size of the data disk for the ECS instance. Unit: GiB. Valid values: 20 to 32768.
      ja: ECS インスタンスのデータディスクのサイズ。単位: GiB。有効値:20~32768。
      en: The size of the data disk for the ECS instance. Unit: GiB. Valid values: 20 to 32768.
    Default: 100
    MaxValue: 32768
    MinValue: 20
    Label:
      zh-cn: Data Disk Size
      en: Data Disk Size
      ja: データディスクサイズ
    Type: Number
  InstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: The logon password for the server. The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters from the following set: ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/.
      ja: サーバーのログオンパスワード。パスワードは 8~30 文字で、大文字、小文字、数字、および次の特殊文字セット:()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ のうち少なくとも 3 種類を含める必要があります。
      zh-cn: The logon password for the server. The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters from the following set: ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/.
    AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
    Label:
      en: Instance Password
      ja: インスタンスのパスワード
      zh-cn: Instance Password
    ConstraintDescription:
      en: The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters from the following set: ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/.
      ja: パスワードは 8~30 文字で、大文字、小文字、数字、および次の特殊文字セット:()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ のうち少なくとも 3 種類を含める必要があります。
      zh-cn: The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters from the following set: ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/.
    MinLength: 8
    MaxLength: 30
    AssociationProperty: ALIYUN::ECS::Instance::Password
  PayType:
    Type: String
    Label:
      en: Billing Method
      ja: 課金方法
      zh-cn: Billing Method
    Default: PostPaid
    AllowedValues:
      - PostPaid
      - PrePaid
    AssociationProperty: ChargeType
    AssociationPropertyMetadata:
      LocaleKey: InstanceChargeType
  PayPeriodUnit:
    Type: String
    Description:
      en: The subscription duration unit. Valid values: Week and Month. The default value is Month.<br><b><font color='red'>This parameter is valid only when the billing method is Subscription.</font>
      ja: サブスクリプション期間の単位。有効値: Month および Year。デフォルト値は Month です。<br><b><font color='red'>このパラメーターは、課金方法がサブスクリプションの場合にのみ有効です。</font>
      zh-cn: The subscription duration unit. Valid values: Week and Month. The default value is Month.<b><font color='red'>This parameter is valid only when the billing method is Subscription.</font>
    Label:
      en: Subscription Duration Unit
      ja: サブスクリプション期間の単位
      zh-cn: Subscription Duration Unit
    Default: Month
    AllowedValues:
      - Month
      - Year
    AssociationProperty: PayPeriodUnit
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Not:
            Fn::Equals:
              - ${PayType}
              - PostPaid
  PayPeriod:
    Type: Number
    Label:
      en: Subscription Duration
      ja: サブスクリプション期間
      zh-cn: Subscription Duration
    Default: 1
    AllowedValues:
      - 1
      - 2
      - 3
      - 4
      - 5
      - 6
      - 7
      - 8
      - 9
    AssociationProperty: PayPeriod
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Not:
            Fn::Equals:
              - ${PayType}
              - PostPaid
  LoadBalancerSpec:
    Type: String
    AssociationProperty: ALIYUN::SLB::Instance::InstanceType
    Label:
      en: SLB Instance Edition
      ja: SLB インスタンス Edition
      zh-cn: SLB Instance Edition
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
  Bandwidth:
    Description:
      zh-cn: Valid values: 0 to 1000. Unit: Mbps.
      ja: '有効値:1~1000。単位: Mbps。'
      en: 'Valid values: 0 to 1000. Unit: Mbps.'
    Default: 10
    MaxValue: 1000
    Label:
      zh-cn: EIP Bandwidth for SLB
      en: EIP Bandwidth for SLB
      ja: SLB の EIP 帯域幅
    MinValue: 1
    Type: Number
Resources:
  Vpc:
    Type: ALIYUN::ECS::VPC
    Properties:
      CidrBlock:
        Ref: VpcCidrBlock
      VpcName:
        Ref: ALIYUN::StackName
  VSwitch:
    Type: ALIYUN::ECS::VSwitch
    Properties:
      VSwitchName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      ZoneId:
        Ref: ZoneId
      CidrBlock:
        Ref: VSwitchCidrBlock
  EcsSecurityGroup:
    Type: ALIYUN::ECS::SecurityGroup
    Properties:
      SecurityGroupName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      SecurityGroupIngress:
        - PortRange: 80/80
          Priority: 1
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
          NicType: internet
      SecurityGroupEgress:
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: internet
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: intranet
  WaitCondition:
    Type: ALIYUN::ROS::WaitCondition
    Properties:
      Count: 1
      Handle:
        Ref: WaitConditionHandle
      Timeout: 300
  WaitConditionHandle:
    Type: ALIYUN::ROS::WaitConditionHandle
  EcsInstanceGroup:
    Type: ALIYUN::ECS::InstanceGroup
    Properties:
      InstanceChargeType:
        Ref: PayType
      PeriodUnit:
        Ref: PayPeriodUnit
      Period:
        Ref: PayPeriod
      IoOptimized: optimized
      SystemDiskCategory:
        Ref: SystemDiskCategory
      SystemDiskSize:
        Ref: SystemDiskSize
      DiskMappings:
        - Category:
            Ref: DataDiskCategory
          Size:
            Ref: DataDiskSize
      VpcId:
        Ref: Vpc
      SecurityGroupId:
        Ref: EcsSecurityGroup
      VSwitchId:
        Ref: VSwitch
      MaxAmount: 2
      ImageId: centos_7
      InstanceType:
        Ref: EcsInstanceType
      Password:
        Ref: InstancePassword
      AllocatePublicIP: false
      UserData:
        Fn::Sub:
          - |
            #!/bin/bash
            # ディスクを /disk1 にマウントする
            cat >> /root/InitDataDisk.sh << EOF
            #!/bin/bash
            echo "p
            n
            p
            w
            " |  fdisk -u /dev/vdb
            EOF
            /bin/bash /root/InitDataDisk.sh
            rm -f /root/InitDataDisk.sh
            rm -f InitDataDisk.sh
            mkfs -t ext4 /dev/vdb1
            cp /etc/fstab /etc/fstab.bak
            mkdir /disk1
            echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\\\"//g'` /disk1 ext4 defaults 0 0 >> /etc/fstab
            mount -a
            # インストールスクリプトを設定する
            yum install -y nginx
            # 起動スクリプトを設定する
            systemctl start nginx.service
            # WaitConditionHandle に成功シグナルを送信して WaitCondition の待機を終了させる
            ${CurlCli} -d "{\"Data\" : \"Success\", \"status\" : \"SUCCESS\"}"
          - CurlCli:
              Fn::GetAtt:
                - WaitConditionHandle
                - CurlCli
  Slb:
    Type: ALIYUN::SLB::LoadBalancer
    Properties:
      VpcId:
        Ref: Vpc
      VSwitchId:
        Ref: VSwitch
      LoadBalancerName:
        Fn::Sub: slb-${ALIYUN::StackName}
      PayType:
        Ref: PayType
      PricingCycle:
        Ref: PayPeriodUnit
      Duration:
        Ref: PayPeriod
      AddressType: intranet
      LoadBalancerSpec:
        Ref: LoadBalancerSpec
      AutoPay: true
  EipSlbAddress:
    Type: ALIYUN::VPC::EIP
    Properties:
      Name:
        Ref: ALIYUN::StackName
      InternetChargeType: PayByTraffic
      Bandwidth:
        Ref: Bandwidth
  EipSlbAddressAssociation:
    Type: ALIYUN::VPC::EIPAssociation
    Properties:
      InstanceId:
        Ref: Slb
      AllocationId:
        Ref: EipSlbAddress
  SlbBackendServerAttachment:
    DependsOn:
      - EcsInstanceGroup
    Type: ALIYUN::SLB::BackendServerAttachment
    Properties:
      BackendServerList:
        Fn::GetAtt:
          - EcsInstanceGroup
          - InstanceIds
      LoadBalancerId:
        Ref: Slb
      BackendServerWeightList:
        - 100
        - 50
  SlbListener:
    DependsOn:
      - Slb
    Type: ALIYUN::SLB::Listener
    Properties:
      Persistence:
        CookieTimeout: 60
        StickySession: 'on'
        PersistenceTimeout: 180
        XForwardedFor: 'off'
        StickySessionType: insert
      ListenerPort: 80
      Bandwidth: -1
      HealthCheck:
        HttpCode: http_2xx,http_3xx,http_4xx,http_5xx
        HealthCheckType: tcp
        UnhealthyThreshold: 3
        Timeout: 5
        HealthyThreshold: 3
        Port: 80
        URI: /
        Interval: 2
      LoadBalancerId:
        Ref: Slb
      BackendServerPort: 80
      Protocol: tcp
Outputs:
  Endpoint:
    Description:
      zh-cn: The public IP address.
      ja: パブリック IP アドレス。
      en: The public IP address.
    Value:
      Fn::Sub:
        - http://${ServerAddress}
        - ServerAddress:
            Fn::GetAtt:
              - EipSlbAddress
              - EipAddress

パラメーターグループの追加とパラメーター設定の動的取得

EcsInstanceGroup リソースの InstanceType および SystemDiskCategory プロパティと、Slb リソースの LoadBalancerSpec プロパティはパラメーターを参照します。異なるリージョンでスタックを作成する場合、デプロイごとにこれらのプロパティに異なる値を指定する必要があります。

テンプレートに Parameters セクションを追加して、柔軟性と再利用性を高めます。

パラメーターグループの追加

Metadata セクションを使用して、Parameters セクションで定義されたパラメーターをグループ化し、パラメーターグループのラベルを定義します。

テンプレートのリソースとパラメーターを定義した後、対応するリソースに基づいてパラメーターをグループ化します。

次の表に、この例のリソースをグループ化する方法を示します。

パラメーターカテゴリ

リソース名

パラメーター名

基本ネットワーク設定

VpcVSwitchEcsSecurityGroup

ZoneIdVpcCidrBlockVSwitchCidrBlock

SLB 設定

SlbEipSlbAddress

LoadBalancerSpecBandwidth

ECS 設定

EcsInstanceGroup

PayTypePayPeriodUnitPayPeriodEcsInstanceTypeSystemDiskCategorySystemDiskSizeDataDiskCategoryDataDiskSizeInstancePassword

パラメーター設定の動的取得

EcsInstanceType を例に取ります。フィルター条件を設定し、ROS コンソールで設定を動的に選択するには、サポートされている AssociationProperty 値を検索してください。リソースタイプ ALIYUN::ECS::InstanceGroup に基づくと、値は ALIYUN::ECS::Instance::InstanceType です。次に、ZoneIdInstanceChargeType などのフィルター条件を使用して AssociationPropertyMetadata 値をクエリしてください。詳細については、「AssociationProperty と AssociationPropertyMetadata」をご参照ください。

完全なテンプレートサンプル

ROSTemplateFormatVersion: '2015-09-01'
Description:
  en: Create one CLB instance and two ECS instances in a VPC and vSwitch, and attach all ECS instances to the CLB instance.
  ja: VPC と vSwitch に CLB インスタンスを 1 つ、ECS インスタンスを 2 つ作成し、すべての ECS インスタンスを CLB インスタンスにアタッチします。
  zh-cn: Create one CLB instance and two ECS instances in a VPC and vSwitch, and attach all ECS instances to the CLB instance.
Parameters:
  ZoneId:
    Type: String
    AssociationProperty: ALIYUN::ECS::Instance::ZoneId
    Label:
      en: vSwitch Zone
      ja: vSwitch ゾーン
      zh-cn: vSwitch Zone
  VpcCidrBlock:
    Default: 192.168.0.0/16
    Label:
      zh-cn: VPC CIDR Block
      en: VPC CIDR Block
      ja: VPC CIDR ブロック
    Type: String
    Description:
      zh-cn: The CIDR block for the new VPC. The following CIDR blocks are recommended<font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>
      en: The CIDR block for the new VPC. The following CIDR blocks are recommended<br><font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>
      ja: 新しい VPC の CIDR ブロック。次の CIDR ブロックを推奨します:<font color='green'>[10.0.0.0/8]</font><br><font color='green'>[172.16.0.0/12]</font><br><font color='green'>[192.168.0.0/16]</font>
  VSwitchCidrBlock:
    Default: 192.168.0.0/24
    Type: String
    Description:
      zh-cn: Must be a subnet of the VPC and not used by other vSwitches.
      en: Must be a subnet of the VPC and not used by other vSwitches.
      ja: VPC のサブネットであり、他の vSwitch で使用されていないことが必要です。
    Label:
      zh-cn: vSwitch CIDR Block
      en: vSwitch CIDR Block
      ja: vSwitch CIDR ブロック
  EcsInstanceType:
    Type: String
    Label:
      en: Instance Type
      ja: インスタンスタイプ
      zh-cn: Instance Type
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceChargeType: ${InstanceChargeType}
  SystemDiskCategory:
    Type: String
    Description:
      en: '<font color=''blue''><b>Valid values:</font>[cloud_efficiency: <font color=''green''>ultra disk</font>]<br>[cloud_ssd: <font color=''green''>standard SSD</font>]<br>[cloud_essd: <font color=''green''>enterprise SSD</font>]<br>[cloud: <font color=''green''>basic disk</font>]<br>[ephemeral_ssd: <font color=''green''>local SSD</font>]'
      ja: '<font color=''blue''><b>有効値:</b></font><br>[cloud_efficiency: <font color=''green''>ウルトラディスク</font>]<br>[cloud_ssd: <font color=''green''>標準 SSD</font>]<br>[cloud_essd: <font color=''green''>エンタープライズ SSD</font>]<br>[cloud: <font color=''green''>ベーシックディスク</font>]<br>[ephemeral_ssd: <font color=''green''>ローカル SSD</font>]'
      zh-cn: '<font color=''blue''><b>Valid values:</font>[cloud_efficiency: <font color=''green''>ultra disk</font>]<br>[cloud_ssd: <font color=''green''>standard SSD</font>]<br>[cloud_essd: <font color=''green''>enterprise SSD</font>]<br>[cloud: <font color=''green''>basic disk</font>]<br>[ephemeral_ssd: <font color=''green''>local SSD</font>]'
    AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceType: ${EcsInstanceType}
    Label:
      en: System Disk Type
      ja: システムディスクタイプ
      zh-cn: System Disk Type
  SystemDiskSize:
    Default: 40
    Type: Number
    Description:
      zh-cn: 'The size of the system disk. Valid values: 40 to 500. Unit: GB.'
      ja: 'システムディスクのサイズ。有効値:40~500。単位: GB。'
      en: 'The size of the system disk. Valid values: 40 to 500. Unit: GB.'
    Label:
      zh-cn: System Disk Size
      en: System Disk Size
      ja: システムディスクサイズ
  DataDiskCategory:
    AssociationProperty: ALIYUN::ECS::Disk::DataDiskCategory
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceType: ${EcsInstanceType}
    Type: String
    Description:
      zh-cn: '<font color=''blue''><b>Valid values:</font>[cloud_efficiency: <font color=''green''>ultra disk</font>]<br>[cloud_ssd: <font color=''green''>standard SSD</font>]<br>[cloud_essd: <font color=''green''>enterprise SSD</font>]<br>[cloud: <font color=''green''>basic disk</font>]'
      ja: '<font color=''blue''><b>有効値:</b></font><br>[cloud_efficiency: <font color=''green''>ウルトラディスク</font>]<br>[cloud_ssd: <font color=''green''>標準 SSD</font>]<br>[cloud_essd: <font color=''green''>エンタープライズ SSD</font>]<br>[cloud: <font color=''green''>ベーシックディスク</font>]'
      en: '<font color=''blue''><b>Valid values:</font>[cloud_efficiency: <font color=''green''>ultra disk</font>]<br>[cloud_ssd: <font color=''green''>standard SSD</font>]<br>[cloud_essd: <font color=''green''>enterprise SSD</font>]<br>[cloud: <font color=''green''>basic disk</font>]'
    Label:
      zh-cn: Data Disk Type
      en: Data Disk Type
      ja: データディスクタイプ
  DataDiskSize:
    Description:
      zh-cn: 'The size of the data disk for the ECS instance. Unit: GiB. Valid values: 20 to 32768.'
      ja: 'ECS インスタンスのデータディスクのサイズ。単位: GiB。有効値:20~32768。'
      en: 'The size of the data disk for the ECS instance. Unit: GiB. Valid values: 20 to 32768.'
    Default: 100
    MaxValue: 32768
    MinValue: 20
    Label:
      zh-cn: Data Disk Size
      en: Data Disk Size
      ja: データディスクサイズ
    Type: Number
  InstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: The logon password for the server. The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters from the following set: ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/.
      ja: サーバーのログオンパスワード。パスワードは 8~30 文字で、大文字、小文字、数字、および次の特殊文字セット:()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ のうち少なくとも 3 種類を含める必要があります。
      zh-cn: The logon password for the server. The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters from the following set: ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/.
    AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
    Label:
      en: Instance Password
      ja: インスタンスのパスワード
      zh-cn: Instance Password
    ConstraintDescription:
      en: The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters from the following set: ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/.
      ja: パスワードは 8~30 文字で、大文字、小文字、数字、および次の特殊文字セット:()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ のうち少なくとも 3 種類を含める必要があります。
      zh-cn: The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters from the following set: ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/.
    MinLength: 8
    MaxLength: 30
    AssociationProperty: ALIYUN::ECS::Instance::Password
  PayType:
    Type: String
    Label:
      en: Billing Method
      ja: 課金方法
      zh-cn: Billing Method
    Default: PostPaid
    AllowedValues:
      - PostPaid
      - PrePaid
    AssociationProperty: ChargeType
    AssociationPropertyMetadata:
      LocaleKey: InstanceChargeType
  PayPeriodUnit:
    Type: String
    Description:
      en: The subscription duration unit. Valid values: Week and Month. The default value is Month.<br><b><font color='red'>This parameter is valid only when the billing method is Subscription.</font>
      ja: サブスクリプション期間の単位。有効値: Month および Year。デフォルト値は Month です。<br><b><font color='red'>このパラメーターは、課金方法がサブスクリプションの場合にのみ有効です。</font>
      zh-cn: The subscription duration unit. Valid values: Week and Month. The default value is Month.<b><font color='red'>This parameter is valid only when the billing method is Subscription.</font>
    Label:
      en: Subscription Duration Unit
      ja: サブスクリプション期間の単位
      zh-cn: Subscription Duration Unit
    Default: Month
    AllowedValues:
      - Month
      - Year
    AssociationProperty: PayPeriodUnit
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Not:
            Fn::Equals:
              - ${PayType}
              - PostPaid
  PayPeriod:
    Type: Number
    Label:
      en: Subscription Duration
      ja: サブスクリプション期間
      zh-cn: Subscription Duration
    Default: 1
    AllowedValues:
      - 1
      - 2
      - 3
      - 4
      - 5
      - 6
      - 7
      - 8
      - 9
    AssociationProperty: PayPeriod
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Not:
            Fn::Equals:
              - ${PayType}
              - PostPaid
  LoadBalancerSpec:
    Type: String
    AssociationProperty: ALIYUN::SLB::Instance::InstanceType
    Label:
      en: SLB Instance Edition
      ja: SLB インスタンス Edition
      zh-cn: SLB Instance Edition
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
  Bandwidth:
    Description:
      zh-cn: 'Valid values: 0 to 1000. Unit: Mbps.'
      ja: '有効値:1~1000。単位: Mbps。'
      en: 'Valid values: 0 to 1000. Unit: Mbps.'
    Default: 10
    MaxValue: 1000
    Label:
      zh-cn: EIP Bandwidth for SLB
      en: EIP Bandwidth for SLB
      ja: SLB の EIP 帯域幅
    MinValue: 1
    Type: Number
Resources:
  Vpc:
    Type: ALIYUN::ECS::VPC
    Properties:
      CidrBlock:
        Ref: VpcCidrBlock
      VpcName:
        Ref: ALIYUN::StackName
  VSwitch:
    Type: ALIYUN::ECS::VSwitch
    Properties:
      VSwitchName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      ZoneId:
        Ref: ZoneId
      CidrBlock:
        Ref: VSwitchCidrBlock
  EcsSecurityGroup:
    Type: ALIYUN::ECS::SecurityGroup
    Properties:
      SecurityGroupName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      SecurityGroupIngress:
        - PortRange: 80/80
          Priority: 1
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
          NicType: internet
      SecurityGroupEgress:
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: internet
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: intranet
  WaitCondition:
    Type: ALIYUN::ROS::WaitCondition
    Properties:
      Count: 1
      Handle:
        Ref: WaitConditionHandle
      Timeout: 300
  WaitConditionHandle:
    Type: ALIYUN::ROS::WaitConditionHandle
  EcsInstanceGroup:
    Type: ALIYUN::ECS::InstanceGroup
    Properties:
      InstanceChargeType:
        Ref: PayType
      PeriodUnit:
        Ref: PayPeriodUnit
      Period:
        Ref: PayPeriod
      IoOptimized: optimized
      SystemDiskCategory:
        Ref: SystemDiskCategory
      SystemDiskSize:
        Ref: SystemDiskSize
      DiskMappings:
        - Category:
            Ref: DataDiskCategory
          Size:
            Ref: DataDiskSize
      VpcId:
        Ref: Vpc
      SecurityGroupId:
        Ref: EcsSecurityGroup
      VSwitchId:
        Ref: VSwitch
      MaxAmount: 2
      ImageId: centos_7
      InstanceType:
        Ref: EcsInstanceType
      Password:
        Ref: InstancePassword
      AllocatePublicIP: false
      UserData:
        Fn::Sub:
          - |
            #!/bin/bash
            # ディスクを /disk1 にマウントする
            cat >> /root/InitDataDisk.sh << EOF
            #!/bin/bash
            echo "p
            n
            p
            w
            " |  fdisk -u /dev/vdb
            EOF
            /bin/bash /root/InitDataDisk.sh
            rm -f /root/InitDataDisk.sh
            rm -f InitDataDisk.sh
            mkfs -t ext4 /dev/vdb1
            cp /etc/fstab /etc/fstab.bak
            mkdir /disk1
            echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\\\"//g'` /disk1 ext4 defaults 0 0 >> /etc/fstab
            mount -a
            # インストールスクリプトを設定する
            yum install -y nginx
            # 起動スクリプトを設定する
            systemctl start nginx.service
            # WaitConditionHandle に成功シグナルを送信して WaitCondition の待機を終了させる
            ${CurlCli} -d "{\"Data\" : \"Success\", \"status\" : \"SUCCESS\"}"
          - CurlCli:
              Fn::GetAtt:
                - WaitConditionHandle
                - CurlCli
  Slb:
    Type: ALIYUN::SLB::LoadBalancer
    Properties:
      VpcId:
        Ref: Vpc
      VSwitchId:
        Ref: VSwitch
      LoadBalancerName:
        Fn::Sub: slb-${ALIYUN::StackName}
      PayType:
        Ref: PayType
      PricingCycle:
        Ref: PayPeriodUnit
      Duration:
        Ref: PayPeriod
      AddressType: intranet
      LoadBalancerSpec:
        Ref: LoadBalancerSpec
      AutoPay: true
  EipSlbAddress:
    Type: ALIYUN::VPC::EIP
    Properties:
      Name:
        Ref: ALIYUN::StackName
      InternetChargeType: PayByTraffic
      Bandwidth:
        Ref: Bandwidth
  EipSlbAddressAssociation:
    Type: ALIYUN::VPC::EIPAssociation
    Properties:
      InstanceId:
        Ref: Slb
      AllocationId:
        Ref: EipSlbAddress
  SlbBackendServerAttachment:
    DependsOn:
      - EcsInstanceGroup
    Type: ALIYUN::SLB::BackendServerAttachment
    Properties:
      BackendServerList:
        Fn::GetAtt:
          - EcsInstanceGroup
          - InstanceIds
      LoadBalancerId:
        Ref: Slb
      BackendServerWeightList:
        - 100
        - 50
  SlbListener:
    DependsOn:
      - Slb
    Type: ALIYUN::SLB::Listener
    Properties:
      Persistence:
        CookieTimeout: 60
        StickySession: 'on'
        PersistenceTimeout: 180
        XForwardedFor: 'off'
        StickySessionType: insert
      ListenerPort: 80
      Bandwidth: -1
      HealthCheck:
        HttpCode: http_2xx,http_3xx,http_4xx,http_5xx
        HealthCheckType: tcp
        UnhealthyThreshold: 3
        Timeout: 5
        HealthyThreshold: 3
        Port: 80
        URI: /
        Interval: 2
      LoadBalancerId:
        Ref: Slb
      BackendServerPort: 80
      Protocol: tcp
Outputs:
  Endpoint:
    Description:
      zh-cn: The public IP address
      ja: パブリック IP アドレス
      en: The public IP address
    Value:
      Fn::Sub:
        - http://${ServerAddress}
        - ServerAddress:
            Fn::GetAtt:
              - EipSlbAddress
              - EipAddress
Metadata:
  ALIYUN::ROS::Interface:
    ParameterGroups:
      - Parameters:
          - ZoneId
          - VpcCidrBlock
          - VSwitchCidrBlock
        Label:
          default:
            zh-cn: Basic Network Configuration
            en: Basic Network Configuration
            ja: 基本ネットワーク設定
      - Parameters:
          - PayType
          - PayPeriodUnit
          - PayPeriod
        Label:
          default:
            en: Billing Method Configuration
            zh-cn: Billing Method Configuration
            ja: 課金方法設定
      - Parameters:
          - EcsInstanceType
          - SystemDiskCategory
          - SystemDiskSize
          - DataDiskCategory
          - DataDiskSize
          - InstancePassword
        Label:
          default:
            en: ECS Instance Configuration
            zh-cn: ECS Instance Configuration
            ja: ECS インスタンス設定
      - Parameters:
          - LoadBalancerSpec
          - Bandwidth
        Label:
          default:
            en: SLB Configuration
            zh-cn: SLB Configuration
            ja: SLB 設定