全部產品
Search
文件中心

Resource Orchestration Service:如何對ALIYUN::ECS::InstanceGroup建立的資源輸出資料行表進行遍曆拼接?

更新時間:Jun 19, 2024

本文以ALIYUN::ECS::InstanceGroup資源為例,向您介紹如何對資源的輸出資料行表進行遍曆拼接。

ALIYUN::ECS::InstanceGroup資源用於建立多個ECS執行個體,建立成功後返回的參數都是以列表的形式展開。更多資訊,請參見ALIYUN::ECS::InstanceGroup

例如,ALIYUN::ECS::InstanceGroup資源的返回參數HostNames為['name1', 'name2'],PrivateIps為['192.168.1.1', 192.168.1.2'],經過遍曆拼接後的結果為['name1:192.168.1.1', 'name2:192.168.1.2']。

您可以在模板的Output部分先使用內建函式Fn::GetAtt擷取對應資源的屬性值,再使用內建函式Fn::MergeMapToList將多個Map合并為一個以Map為元素的列表,最後再使用內建函式Fn::Jq迴圈拼接成需要的格式。

ROSTemplateFormatVersion: '2015-09-01'
Outputs:
  testMapping:
    Value:
      Fn::Jq:
        - All
        - .[] | "\(.a):\(.b)"
        - Fn::MergeMapToList:
            - a:
                Fn::GetAtt:
                  - InstanceGroup # 模板中建立的資源部分已省略,下同。
                  - HostNames
            - b:
                Fn::GetAtt:
                  - InstanceGroup
                  - PrivateIps

完整模板樣本

ROSTemplateFormatVersion: '2015-09-01'
Parameters:
  VpcId:
    Description:
      en: The VPC id to create ecs instance.
    Default: Null
    Required: false
    Label:
      zh-cn: 專用網路ID
      en: VpcId
    AssociationProperty: ALIYUN::ECS::VPC::VPCId
    Type: String
  ImageId:
    AssociationPropertyMetadata:
      DefaultFilter:
        OSType: linux
        ImageName: aliyun_3_**
        ImageOwnerAlias: system
      InstanceType: ${InstanceType}
      SupportedImageOwnerAlias:
        - system
        - self
        - others
    Description:
      zh-cn: 用於啟動ECS執行個體的鏡像ID。包括公用鏡像、自訂鏡像和雲市場鏡像。
      en: Image ID to create ecs instance.
    Default: Null
    Required: false
    Label:
      zh-cn: 用於啟動ECS執行個體的鏡像ID
      en: ImageId
    AssociationProperty: ALIYUN::ECS::Image::ImageId
    Type: String
  VSwitchId:
    AssociationPropertyMetadata:
      VpcId: ${VpcId}
      ZoneId: ${ZoneId}
    Description:
      en: The vSwitch Id to create ecs instance.
    Default: Null
    Required: false
    Label:
      zh-cn: 交換器ID
      en: VSwitchId
    AssociationProperty: ALIYUN::VPC::VSwitch::VSwitchId
    Type: String
  Password:
    Description:
      en: Password of created ecs instance. Must contain at least 3 types of special character, lower character, upper character, number.
    Default: Null
    Required: false
    Label:
      zh-cn: ECS執行個體登入密碼
      en: Password
    AssociationProperty: ALIYUN::ECS::Instance::Password
    Type: String
  InstanceType:
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
    Description:
      en: Ecs instance supported instance type, make sure it should be correct.
    Default: ecs.g7.xlarge
    Required: true
    Label:
      zh-cn: ECS執行個體規格
      en: InstanceType
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    Type: String
  MaxAmount:
    Description:
      en: Max number of instances to create, should be bigger than 'MinAmount' and smaller than 1000.
    Default: 2
    Required: true
    MaxValue: 1000
    Label:
      zh-cn: 執行個體數量
    MinValue: 0
    Type: Number
  SystemDiskCategory:
    AssociationPropertyMetadata:
      InstanceType: ${InstanceType}
      ZoneId: ${ZoneId}
    Description:
      en: Category of system disk. Default is cloud_efficiency. support cloud|cloud_efficiency|cloud_ssd|cloud_essd|ephemeral_ssd|cloud_auto|cloud_essd_entry.Old instances will not be changed.
    Default: cloud_essd
    Required: false
    Label:
      zh-cn: 系統硬碟類型
      en: SystemDiskCategory
    AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
    AllowedValues:
      - cloud_essd
      - cloud_efficiency
      - cloud_ssd
      - cloud
      - cloud_auto
      - cloud_essd_entry
    Type: String
  ZoneId:
    Description:
      en: |-
        The ID of the zone to which the instance belongs. For more information, 
        call the DescribeZones operation to query the most recent zone list. 
        Default value is empty, which means random selection.
    Default: Null
    Required: false
    Label:
      zh-cn: 可用性區域ID
      en: ZoneId
    AssociationProperty: ZoneId
    Type: String
  SystemDiskSize:
    Description:
      en: 'Disk size of the system disk, range from 20 to 500 GB. If you specify with your own image, make sure the system disk size bigger than image size. '
    Default: 40
    Required: false
    Label:
      zh-cn: 系統硬碟大小
      en: SystemDiskSize
    MinValue: 20
    Type: Number
  SystemDiskPerformanceLevel:
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Equals:
            - ${SystemDiskCategory}
            - cloud_essd
    Description:
      zh-cn: 建立ESSD雲端硬碟作為系統硬碟使用時,設定雲端硬碟的效能等級。
      en: 'The performance level of the enhanced SSD used as the system disk.Default value: PL1. Valid values:PL0: A single enhanced SSD delivers up to 10,000 random read/write IOPS.PL1: A single enhanced SSD delivers up to 50,000 random read/write IOPS.PL2: A single enhanced SSD delivers up to 100,000 random read/write IOPS.PL3: A single enhanced SSD delivers up to 1,000,000 random read/write IOPS.'
    Default: PL0
    Required: false
    AllowedValues:
      - PL0
      - PL1
      - PL2
      - PL3
    Label:
      zh-cn: ESSD系統硬碟效能等級
    Type: String
  SecurityGroupId:
    Type: String
    AssociationProperty: ALIYUN::ECS::SecurityGroup::SecurityGroupId
    AssociationPropertyMetadata:
      VpcId: ${VpcId}
Resources:
  InstanceGroup:
    Type: ALIYUN::ECS::InstanceGroup
    Properties:
      VpcId:
        Ref: VpcId
      ImageId:
        Ref: ImageId
      VSwitchId:
        Ref: VSwitchId
      Password:
        Ref: Password
      InstanceType:
        Ref: InstanceType
      MaxAmount:
        Ref: MaxAmount
      SecurityGroupId:
        Ref: SecurityGroupId
      SystemDiskCategory:
        Ref: SystemDiskCategory
      ZoneId:
        Ref: ZoneId
      SystemDiskSize:
        Ref: SystemDiskSize
      SystemDiskPerformanceLevel:
        Ref: SystemDiskPerformanceLevel
Metadata:
  ALIYUN::ROS::Interface:
    ParameterGroups:
      - Parameters:
          - ZoneId
          - VpcId
          - VSwitchId
          - SecurityGroupId
        Label:
          default:
            zh-cn: 網路設定
            en: Network Configuration
      - Parameters:
          - InstanceType
          - ImageId
          - MaxAmount
        Label:
          default:
            zh-cn: 執行個體與鏡像
            en: Instance and Image
      - Parameters:
          - SystemDiskCategory
          - SystemDiskPerformanceLevel
          - SystemDiskSize
        Label:
          default:
            zh-cn: 系統硬碟設定
            en: System Disk Configuration
      - Parameters:
          - Password
        DefaultExpand: false
        Label:
          default:
            zh-cn: 管理設定
            en: Manage Configuration
Outputs:
  testMapping:
    Value:
      Fn::Jq:
        - All
        - .[] | "\(.a):\(.b)"
        - Fn::MergeMapToList:
            - a:
                Fn::GetAtt:
                  - InstanceGroup
                  - HostNames
            - b:
                Fn::GetAtt:
                  - InstanceGroup
                  - PrivateIps