All Products
Search
Document Center

Resource Orchestration Service:How to traverse and concatenate return values of resources created by ALIYUN::ECS::InstanceGroup?

Last Updated:Mar 28, 2024

This topic provides an example on how to traverse and concatenate return values of resources that are created by using ALIYUN::ECS::InstanceGroup.

ALIYUN::ECS::InstanceGroup is used to create multiple Elastic Compute Service (ECS) instances. After the instances are created, the return values are arranged in a list. For more information, see ALIYUN::ECS::InstanceGroup.

For example, ['name1', 'name2'] is returned for the HostNames property and ['192.168.1.1', 192.168.1.2'] is returned for the PrivateIps property of ALIYUN::ECS::InstanceGroup. After Resource Orchestration Service (ROS) traverses and concatenates the return values, the final result is ['name1:192.168.1.1', 'name2:192.168.1.2'].

In the Outputs section of your template, you can use the Fn::GetAtt built-in function to query the property values of resources, use the Fn::MergeMapToList built-in function to merge multiple maps into a map list, and then use the Fn::Jq built-in function to traverse and concatenate the final result in a desired format. Sample functions:

ROSTemplateFormatVersion: '2015-09-01'
Outputs:
  testMapping:
    Value:
      Fn::Jq:
        - All
        - .[] | "\(.a):\(.b)"
        - Fn::MergeMapToList:
            - a:
                Fn::GetAtt:
                  - InstanceGroup # The configurations defined for InstanceGroup in the template to create resources are omitted. This also applies to the next InstanceGroup. 
                  - HostNames
            - b:
                Fn::GetAtt:
                  - InstanceGroup
                  - PrivateIps

Sample template:

ROSTemplateFormatVersion: '2015-09-01'
Parameters:
  VpcId:
    Description:
      en: The VPC id to create ecs instance.
    Default: Null
    Required: false
    Label:
       
      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:
        
      en: Image ID to create ecs instance.
    Default: Null
    Required: false
    Label:
       
      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:
       
      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:
       
      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:
       
      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:
       
    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:
       
      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:
       
      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:
       
      en: SystemDiskSize
    MinValue: 20
    Type: Number
  SystemDiskPerformanceLevel:
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Equals:
            - ${SystemDiskCategory}
            - cloud_essd
    Description:
       
      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:
       
    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:
             
            en: Network Configuration
      - Parameters:
          - InstanceType
          - ImageId
          - MaxAmount
        Label:
          default:
             
            en: Instance and Image
      - Parameters:
          - SystemDiskCategory
          - SystemDiskPerformanceLevel
          - SystemDiskSize
        Label:
          default:
             
            en: System Disk Configuration
      - Parameters:
          - Password
        DefaultExpand: false
        Label:
          default:
             
            en: Manage Configuration
Outputs:
  testMapping:
    Value:
      Fn::Jq:
        - All
        - .[] | "\(.a):\(.b)"
        - Fn::MergeMapToList:
            - a:
                Fn::GetAtt:
                  - InstanceGroup
                  - HostNames
            - b:
                Fn::GetAtt:
                  - InstanceGroup
                  - PrivateIps