文档

ROS引用Count参数后,如何使用ALIYUN::Index?

更新时间:

本文以使用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
  • 本页导读 (1)
文档反馈