全部產品
Search
文件中心

Resource Orchestration Service:ROS引用Count參數後,如何使用ALIYUN::Index?

更新時間:Feb 27, 2024

本文以使用Count大量建立EIP和SLB執行個體為例向您介紹如何在引用Count後正確使用ALIYUN::Index。

當您在模板中為資源指定Count後,ROS會對模板進行預先處理,把對應資源展開為多個資源,操作資源棧時使用處理後的模板。更多資訊,請參見資源(Resources)

模板中定義的資源名為Eip,Count值為2,在ROS處理後的模板中不再有Eip資源,只有Eip[0]和Eip[1]兩個資源。同理處理後的模板中只包含Slb[0]和Slb[1]資源、EipBind[0]和EipBind[1]資源,而在EipBind[0]中的ALIYUN::Index會自動轉換為0,其中Fn::GetAtt:Slb,引用的資源為列表[Slb[0],Slb[1]],會轉換成如下內容。InstanceId使用Fn::Select就能引用到Slb[0],同理AllocationId能引用到Eip[0],自此就把Slb[0]和Eip[0]綁定到一起。

InstanceId:
  Fn::Select:
    - 0
    - [Slb[0],Slb[1]] 

完整樣本如下:

ROSTemplateFormatVersion: '2015-09-01'
Parameters:
  Count:
    Type: Number
    Default: 2
  ZoneId:
    Type: String
    AssociationProperty: ALIYUN::ECS::Instance:ZoneId
Resources:
  Vpc:
    Type: ALIYUN::ECS::VPC
    Properties:
      CidrBlock: 10.0.0.0/8
      VpcName: test-resource-count
  VSwitch:
    Type: ALIYUN::ECS::VSwitch
    Properties:
      CidrBlock: 10.0.10.0/24
      ZoneId:
        Ref: ZoneId
      VpcId:
        Ref: Vpc
  Eip:
    Type: ALIYUN::VPC::EIP
    Count:
      Ref: Count
    Properties:
      Bandwidth: 5
  Slb:
    Type: ALIYUN::SLB::LoadBalancer
    Count:
      Ref: Count
    Properties:
      LoadBalancerSpec: slb.s1.small
      VpcId:
        Ref: Vpc
      VSwitchId:
        Ref: VSwitch
      LoadBalancerName: test
      MasterZoneId:
        Ref: ZoneId
      AddressType: intranet
  EipBind:
    Type: ALIYUN::VPC::EIPAssociation
    Count:
      Ref: Count
    Properties:
      InstanceId:
        Fn::Select:
          - Ref: ALIYUN::Index
          - Fn::GetAtt:
              - Slb
              - LoadBalancerId
      AllocationId:
        Fn::Select:
          - Ref: ALIYUN::Index
          - Ref: Eip
Outputs:
  LoadBalancerId:
    Value:
      Fn::GetAtt:
        - Slb
        - LoadBalancerId
  EipAddresses:
    Value:
      Fn::GetAtt:
        - Eip
        - EipAddress