全部產品
Search
文件中心

Resource Orchestration Service:建立ECS執行個體組並掛載到CLB執行個體

更新時間:Jun 19, 2024

本文以建立ECS執行個體組並掛載到傳統型負載平衡(CLB)執行個體為例,由簡入難的向您介紹如何編輯ROS模板。

前提條件

請您提前瞭解模板文法和結構。更多資訊,請參見模板快速入門

情境樣本

在阿里雲專用網路中建立ECS執行個體組,並在ECS執行個體組中部署Nginx服務,然後將建立的ECS執行個體組掛載到CLB執行個體上。

1

使用須知

您可以訪問對應的資源類型查看屬性詳情。具體操作,請參見查看資源類型

資源類型為每個屬性定義了類型、是否必須、是否允許更新等資訊。如果為必須,則要求必須在模板Resources的Properties中聲明該屬性;反之,則為非必須。如果為允許更新,則可以在新模板中修改該屬性,然後使用修改後的模板更新資源棧以達到更新雲資源屬性的目的;反之,則不允許更新。

編輯模板

您可以通過資源類型索引文檔尋找所需的資源類型。更多資訊,請參見資源類型索引

例如:當前情境中需要建立專用網路(ALIYUN::ECS::VPC)、ECS執行個體組(ALIYUN::ECS::InstanceGroup)、Server Load Balancer執行個體(ALIYUN::SLB::LoadBalancer)、負載平衡監聽(ALIYUN::SLB::Listener)、Elastic IP Address(ALIYUN::VPC::EIP),交換器(ALIYUN::ECS::VSwitch)、安全性群組(ALIYUN::ECS::InstanceGroup)。在建立ECS執行個體組時,需要使用ALIYUN::ECS::InstanceGroup中的UserData參數執行資料初始化命令,並使用等待訊號的資源(ALIYUN::ROS::WaitCondition)和接受訊號的資源(ALIYUN::ROS::WaitConditionHandle)控制ECS執行個體組內部的執行流程。

根據以上資訊,您可以在模板中定義需要建立的資源(Resources)。

定義模板資源及其依賴關係

定義基礎網路資源

您可以通過模板定義基礎網路資源VpcVSwitchEcsSecurityGroup

  • 使用Ref與偽參數ALIYUN::StackName擷取資源棧名稱作為資源屬性的屬性值。例如:Vpc中的VpcNameVSwitch中的VSwitchName。更多資訊,請參見函數(Functions)ALIYUN::StackName

  • 使用Ref擷取返回指定參數的值。例如:VSwitch中的Ref: Vpc,擷取的是Vpc資源的傳回值VpcId。一般返回指定參數的值都為資源ID。更多資訊,請參見函數(Functions)

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

定義Server Load Balancer執行個體和彈性公網資源

您可以通過模板定義Server Load Balancer執行個體Slb、負載平衡監聽SlbListener和Elastic IP Address資源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。更多資訊,請參見函數(Functions)

  • 使用Fn::Sub函數用於拼接命令操作字串及將輸入字串中的變數替換為您指定的值。例如:EcsInstanceGroup中的UserData,使用CurlCli替換${CurlCli}。更多資訊,請參見函數(Functions)

  • 控制ECS執行個體內部初始化的資源群組合如下。

    1. 建立一個等待訊號資源(ALIYUN::ROS::WaitCondition)。

    2. 建立一個接收訊號資源(ALIYUN::ROS::WaitConditionHandle)。

    3. 在ECS內部UserData中執行指令碼並發送資訊。例如:EcsInstanceGroup中的UserData內容,UserData中可以傳入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執行個體、Server Load Balancer執行個體和添加後端伺服器依賴關係

您可以通過模板定義ECS執行個體組EcsInstanceGroup、負載平衡SlbSlbBackendServerAttachment

  • 通過SlbBackendServerAttachment將ECS執行個體組掛載到CLB執行個體上。更多資訊,請參見ALIYUN::SLB::BackendServerAttachment

    在ALIYUN::SLB::BackendServerAttachment中,如果不指定BackendServerWeightList的值,則BackendServerList中所有ECS執行個體權重都是100;當BackendServerWeightList長度小於BackendServerList時,則使用BackendServerWeightList中的最後一個值配置BackendServerList中剩餘ECS執行個體的權重。

  • 設定DependsOn屬性可以指定資源緊跟在另一個資源後建立。為某個資源添加DependsOn屬性後,該資源僅在DependsOn屬性中指定的資源後建立。更多資訊,請參見DependsOn

  • 使用Fn::GetAtt函數擷取資源輸出屬性值。例如:EcsInstanceGroup中的InstanceIds。更多資訊,請參見函數(Functions)

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 new VPC and vSWitch, create one CLB, two ECS instances, and bind all ECS instances to the CLB.
  zh-cn: 建立VPC、vSWitch,建立1個CLB、2個ECS執行個體,並將所有ECS執行個體綁定到CLB上。
Parameters:
  ZoneId:
    Type: String
    AssociationProperty: ALIYUN::ECS::Instance::ZoneId
    Label:
      en: vSwitch Availability Zone
      zh-cn: 交換器可用性區域
  VpcCidrBlock:
    Default: 192.168.0.0/16
    Label:
      zh-cn: 專用網路網段
      en: VPC CIDR Block
    Type: String
    Description:
      zh-cn: 建立專用網路IP位址區段範圍,推薦使用以下的IP位址區段<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>
      en: New proprietary network IP address segment range, recommended use of the following IP address segments<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>
  VSwitchCidrBlock:
    Default: 192.168.0.0/24
    Type: String
    Description:
      zh-cn: 必須是所屬專用網路的子網段,並且沒有被其他交換器佔用。
      en: Must be a sub-network segment of the proprietary network and is not occupied by other vSwitches.
    Label:
      zh-cn: 交換器網段
      en: vSwitch CIDR Block
  EcsInstanceType:
    Type: String
    Label:
      en: Instance Type
      zh-cn: 執行個體類型
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceChargeType: ${InstanceChargeType}
  SystemDiskCategory:
    Type: String
    Description:
      en: '<font color=''blue''><b>Optional values:</b></font><br>[cloud_efficiency: <font color=''green''>Efficient Cloud Disk</font>]<br>[cloud_ssd: <font color=''green''>SSD Cloud Disk</font>]<br>[cloud_essd: <font color=''green''>ESSD Cloud Disk</font>]<br>[cloud: <font color=''green''>Cloud Disk</font>]<br>[ephemeral_ssd: <font color=''green''>Local SSD Cloud Disk</font>]'
      zh-cn: '<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''>ESSD雲端硬碟</font>]<br>[cloud: <font color=''green''>普通雲端硬碟</font>]<br>[ephemeral_ssd: <font color=''green''>本地SSD盤</font>]'
    AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceType: ${EcsInstanceType}
    Label:
      en: System Disk Type
      zh-cn: 系統硬碟類型
  SystemDiskSize:
    Default: 40
    Type: Number
    Description:
      zh-cn: 系統硬碟大小, 取值範圍:[40, 500], 單位:GB。
    Label:
      zh-cn: 系統硬碟空間
      en: System Disk Space
  DataDiskCategory:
    AssociationProperty: ALIYUN::ECS::Disk::DataDiskCategory
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceType: ${EcsInstanceType}
    Type: String
    Description:
      zh-cn: '<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''>ESSD雲端硬碟</font>]<br>[cloud: <font color=''green''>普通雲端硬碟</font>]'
      en: '<font color=''blue''><b>Optional values:</b></font><br>[cloud_efficiency: <font color=''green''>Efficient Cloud Disk</font>]<br>[cloud_ssd: <font color=''green''>SSD Cloud Disk</font>]<br>[cloud_essd: <font color=''green''>ESSD Cloud Disk</font>]<br>[cloud: <font color=''green''>Cloud Disk</font>]'
    Label:
      zh-cn: 資料盤類型
      en: Data Disk Type
  DataDiskSize:
    Description:
      zh-cn: ECS執行個體資料盤大小,單位為GiB。取值範圍:20~32768。
    Default: 100
    MaxValue: 32768
    MinValue: 20
    Label:
      zh-cn: 資料盤空間
      en: Data Disk Space
    Type: Number
  InstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: Server login password, Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).。
      zh-cn: 伺服器登入密碼,長度8~30,必須包含三項(大寫字母、小寫字母、數字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符號)。
    AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
    Label:
      en: Instance Password
      zh-cn: 執行個體密碼
    ConstraintDescription:
      en: Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
      zh-cn: 長度8~30,必須包含三項(大寫字母、小寫字母、數字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符號)。
    MinLength: 8
    MaxLength: 30
    AssociationProperty: ALIYUN::ECS::Instance::Password
  PayType:
    Type: String
    Label:
      en: ECS Instance Charge Type
      zh-cn: 付費類型
    Default: PostPaid
    AllowedValues:
      - PostPaid
      - PrePaid
    AssociationProperty: ChargeType
    AssociationPropertyMetadata:
      LocaleKey: InstanceChargeType
  PayPeriodUnit:
    Type: String
    Description:
      en: The long cycle of purchasing resources. <br>Week is a Week and Month is a Month. <br> The default value is month.<br><b><font color='red'> When ECS instance types are PrePaid valid.</b></font>
      zh-cn: 購買資源時間長度的周期。<br>Week為周,Month為月<br>預設值為月<br><b><font color='red'>當ECS執行個體類型為PrePaid有效。</b></font>
    Label:
      en: Pay Period Unit
      zh-cn: 購買資源時間長度周期
    Default: Month
    AllowedValues:
      - Month
      - Year
    AssociationProperty: PayPeriodUnit
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Not:
            Fn::Equals:
              - ${PayType}
              - PostPaid
  PayPeriod:
    Type: Number
    Label:
      en: Period
      zh-cn: 購買資源時間長度
    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: LoadBalancer Specifications
      zh-cn: Server Load Balancer執行個體規格
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
  Bandwidth:
    Description:
      zh-cn: 取值範圍:[0, 1000],單位:Mbps。
      en: 'Value range: [0, 1000], Unit: Mbps.'
    Default: 10
    MaxValue: 1000
    Label:
      zh-cn: 負載平衡的Elastic IP Address頻寬
      en: CLB Elastic IP Bandwidth
    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
        - 100
  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: 對外暴露的公網IP地址。
      en: Public IP Addresses.
    Value:
      Fn::Sub:
        - http://${ServerAddress}
        - ServerAddress:
            Fn::GetAtt:
              - EipSlbAddress
              - EipAddress

添加模板參數分組與動態擷取參數配置

在以上模板中您完成了對多種資源及其依賴關係的定義,此時模板中EcsInstanceGroup資源的InstanceTypeSystemDiskCategory屬性值與Slb資源的LoadBalancerSpec屬性值為引用參數。當您在不同地區建立資源棧時,需要多次調整模板變更資源屬性值以達到部署資源棧的目的。

您可以對模板添加參數Parameters,從而提高模板的靈活性和可複用性。

添加模板參數分組

您可以在模板中使用中繼資料(Metadata)對Parameters中定義的參數進行分組,並定義參數分組標籤。

當您完成對模板資源和參數的定義後,可以按照不同資源和資源對應的參數進行分組。

以當前模板為例,您可以將資源按如下結果劃分。

資源參數分類

資源名稱

參數名稱

基礎網路設定

VpcVSwitchEcsSecurityGroup

ZoneIdVpcCidrBlockVSwitchCidrBlock

負載平衡配置

SlbEipSlbAddress

LoadBalancerSpecBandwidth

Elastic Compute Service配置

EcsInstanceGroup

PayTypePayPeriodUnitPayPeriodECSInstanceTypeECSDiskSizeECSDiskCategoryEcsInstancePassword

動態擷取參數配置

ECSInstanceType參數為例,當你需要在ROS控制台上對參數設定篩選條件並動態選擇參數配置時,可以根據參數對應的資源類型(ALIYUN::ECS::InstanceGroup)在AssociationProperty和AssociationPropertyMetadata文檔中查詢到該參數支援的AssociationProperty取值(ALIYUN::ECS::Instance::InstanceType),然後對篩選到的AssociationProperty查詢過濾條件為ZoneIdInstanceChargeType的AssociationPropertyMetadata取值。更多資訊,請參見AssociationProperty和AssociationPropertyMetadata

完整模板樣本

ROSTemplateFormatVersion: '2015-09-01'
Description:
  en: Create new VPC and vSWitch,, create one CLB, two ECS instances, and bind all ECS instances to the CLB.
  zh-cn: 建立已有VPC、vSWitch,建立1個CLB、2個ECS執行個體,並將所有ECS執行個體綁定到CLB上。
Parameters:
  ZoneId:
    Type: String
    AssociationProperty: ALIYUN::ECS::Instance::ZoneId
    Label:
      en: vSwitch Availability Zone
      zh-cn: 交換器可用性區域
  VpcCidrBlock:
    Default: 192.168.0.0/16
    Label:
      zh-cn: 專用網路網段
      en: VPC CIDR Block
    Type: String
    Description:
      zh-cn: 建立專用網路IP位址區段範圍,推薦使用以下的IP位址區段<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>
      en: New proprietary network IP address segment range, recommended use of the following IP address segments<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>
  VSwitchCidrBlock:
    Default: 192.168.0.0/24
    Type: String
    Description:
      zh-cn: 必須是所屬專用網路的子網段,並且沒有被其他交換器佔用。
      en: Must be a sub-network segment of the proprietary network and is not occupied by other vSwitches.
    Label:
      zh-cn: 交換器網段
      en: vSwitch CIDR Block
  EcsInstanceType:
    Type: String
    Label:
      en: Instance Type
      zh-cn: 執行個體類型
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceChargeType: ${InstanceChargeType}
  SystemDiskCategory:
    Type: String
    Description:
      en: '<font color=''blue''><b>Optional values:</b></font><br>[cloud_efficiency: <font color=''green''>Efficient Cloud Disk</font>]<br>[cloud_ssd: <font color=''green''>SSD Cloud Disk</font>]<br>[cloud_essd: <font color=''green''>ESSD Cloud Disk</font>]<br>[cloud: <font color=''green''>Cloud Disk</font>]<br>[ephemeral_ssd: <font color=''green''>Local SSD Cloud Disk</font>]'
      zh-cn: '<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''>ESSD雲端硬碟</font>]<br>[cloud: <font color=''green''>普通雲端硬碟</font>]<br>[ephemeral_ssd: <font color=''green''>本地SSD盤</font>]'
    AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceType: ${EcsInstanceType}
    Label:
      en: System Disk Type
      zh-cn: 系統硬碟類型
  SystemDiskSize:
    Default: 40
    Type: Number
    Description:
      zh-cn: 系統硬碟大小, 取值範圍:[40, 500], 單位:GB。
      en: 'System disk size, range of values: 40-500, units: GB.'
    Label:
      zh-cn: 系統硬碟空間
      en: System Disk Space
  DataDiskCategory:
    AssociationProperty: ALIYUN::ECS::Disk::DataDiskCategory
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
      InstanceType: ${EcsInstanceType}
    Type: String
    Description:
      zh-cn: '<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''>ESSD雲端硬碟</font>]<br>[cloud: <font color=''green''>普通雲端硬碟</font>]'
      en: '<font color=''blue''><b>Optional values:</b></font><br>[cloud_efficiency: <font color=''green''>Efficient Cloud Disk</font>]<br>[cloud_ssd: <font color=''green''>SSD Cloud Disk</font>]<br>[cloud_essd: <font color=''green''>ESSD Cloud Disk</font>]<br>[cloud: <font color=''green''>Cloud Disk</font>]'
    Label:
      zh-cn: 資料盤盤類型
      en: Data Disk Type
  DataDiskSize:
    Description:
      zh-cn: ECS執行個體資料盤大小,單位為GiB。取值範圍:20~32768。
      en: 'ECS Instance disk size, range of values: 20-32768, units: GB.'
    Default: 100
    MaxValue: 32768
    MinValue: 20
    Label:
      zh-cn: 資料盤空間
      en: Data Disk Space
    Type: Number
  InstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: Server login password, Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
      zh-cn: 伺服器登入密碼,長度8~30,必須包含三項(大寫字母、小寫字母、數字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符號)。
    AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
    Label:
      en: Instance Password
      zh-cn: 執行個體密碼
    ConstraintDescription:
      en: Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
      zh-cn: 長度8~30,必須包含三項(大寫字母、小寫字母、數字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符號)。
    MinLength: 8
    MaxLength: 30
    AssociationProperty: ALIYUN::ECS::Instance::Password
  PayType:
    Type: String
    Label:
      en: ECS Instance Charge Type
      zh-cn: 付費類型
    Default: PostPaid
    AllowedValues:
      - PostPaid
      - PrePaid
    AssociationProperty: ChargeType
    AssociationPropertyMetadata:
      LocaleKey: InstanceChargeType
  PayPeriodUnit:
    Type: String
    Description:
      en: The long cycle of purchasing resources. <br>Week is a Week and Month is a Month. <br> The default value is month.<br><b><font color='red'> When ECS instance types are PrePaid valid.</b></font>
      zh-cn: 購買資源時間長度的周期。<br>Week為周,Month為月<br>預設值為月<br><b><font color='red'>當ECS執行個體類型為PrePaid有效。</b></font>
    Label:
      en: Pay Period Unit
      zh-cn: 購買資源時間長度周期
    Default: Month
    AllowedValues:
      - Month
      - Year
    AssociationProperty: PayPeriodUnit
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Not:
            Fn::Equals:
              - ${PayType}
              - PostPaid
  PayPeriod:
    Type: Number
    Label:
      en: Period
      zh-cn: 購買資源時間長度
    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: LoadBalancer Specifications
      zh-cn: Server Load Balancer執行個體規格
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
  Bandwidth:
    Description:
      zh-cn: 取值範圍:[0, 1000],單位:Mbps。
      en: 'Value range: [0, 1000], Unit: Mbps.'
    Default: 10
    MaxValue: 1000
    Label:
      zh-cn: 負載平衡的Elastic IP Address頻寬
      en: CLB Elastic IP Bandwidth
    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: 對外暴露的公網IP地址
      en: Public IP Addresses
    Value:
      Fn::Sub:
        - http://${ServerAddress}
        - ServerAddress:
            Fn::GetAtt:
              - EipSlbAddress
              - EipAddress
Metadata:
  ALIYUN::ROS::Interface:
    ParameterGroups:
      - Parameters:
          - ZoneId
          - VpcCidrBlock
          - VSwitchCidrBlock
        Label:
          default:
            zh-cn: 基礎網路設定
            en: Basic Network Configuration
      - Parameters:
          - PayType
          - PayPeriodUnit
          - PayPeriod
        Label:
          default:
            en: PayType Configuration
            zh-cn: 付費類型
      - Parameters:
          - EcsInstanceType
          - SystemDiskCategory
          - SystemDiskSize
          - DataDiskCategory
          - DataDiskSize
          - InstancePassword
        Label:
          default:
            en: Instance Configure
            zh-cn: ECS執行個體配置
      - Parameters:
          - LoadBalancerSpec
          - Bandwidth
        Label:
          default:
            en: CLB Configure
            zh-cn: 負載平衡配置