All Products
Search
Document Center

Resource Orchestration Service:Use a scaling group to manage multiple ECS instances

Last Updated:May 08, 2023

This topic helps you walk through the process of creating a Resource Orchestration Service (ROS) template. In this topic, a template that declares the resources used to manage multiple Elastic Compute Service (ECS) instances by using a scaling group is created.

Prerequisites

You are familiar with the syntax and the structure of ROS templates. For more information, see Getting started with templates.

Sample scenario

In this scenario, a scaling group is created in a virtual private cloud (VPC) and a scaling rule is configured for the group. An ECS instance is created and added to the scaling group. This way, the system scales the ECS instance in the scaling group based on the scaling rule.2023-05-08_10-52-53..png

Usage notes

You can view the resource types of the preceding resources to obtain the details of each resource property. For more information, see View resource types.

A resource type declares the properties of a resource, such as the type, whether required, and whether editable. If a property is required, you must declare the property in the Properties part of the Resources section of a template. Otherwise, the property is optional. If a property is editable, you can modify the property in a new template after the property is specified for a cloud resource in a stack template. Then, you can use the new template to update the stack and the resource. Otherwise, you cannot modify the property.

Create a template

You can refer to the documentation of resource types to find the resource types that best suit your needs. For more information, see List of resource types by service.

In the sample scenario, you must create resources by using the following resource types: ALIYUN::ECS::VPC (creates a VPC), ALIYUN::ECS::Instance (creates an ECS instance), ALIYUN::ESS::ScalingGroup (creates a scaling group), ALIYUN::ECS::VSwitch (create a vSwitch for the ECS instance), and ALIYUN::ECS::SecurityGroup (creates a security group for the ECS instance). After you create the resources, you must specify ALIYUN::ESS::ScalingConfiguration (adds a scaling configuration to the scaling group) and ALIYUN::ESS::ScalingGroupEnable (enables the scaling group).

Define resources and associate the resources in the template

Define basic network resources

Define the following basic network resources in the template: Vpc, VSwitch, and EcsSecurityGroup.

  • Use the Ref function together with the ALIYUN::StackName pseudo parameter to obtain the stack name. Then, set a property to Ref: ALIYUN::StackName to associate the property value with the stack name. In the following code snippet, the value of VpcName in Vpc and the value of VSwitchName in VSwitch are associated with the stack name. For more information, see Ref and ALIYUN::StackName.

  • Use the Fn::Select and Fn::GetAZs functions together with the ALIYUN::Region pseudo parameter to obtain the first zone ID of the region where the stack resides. In the following code snippet, the value of ZoneId in VSwitch is obtained. For more information, see Functions and ALIYUN::Region.

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:
        Fn::Select:
          - '0'
          - Fn::GetAZs:
              Ref: ALIYUN::Region
      CidrBlock: 192.168.0.0/24
  EcsSecurityGroup:
    Type: ALIYUN::ECS::SecurityGroup
    Properties:
      SecurityGroupName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      SecurityGroupEgress:
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: intranet

Define ECS resources

Define the EcsInstanceGroup resource in the template.

Use the Ref function to reference the logical names of resources in the template. In the following code snippet, Vpc in VpcId, EcsSecurityGroup in SecurityGroupId, and VSwitch in VSwitchId are referenced. For more information, see Ref.

Resources:
	EcsInstance:
    Type: ALIYUN::ECS::Instance
    Properties:
      VpcId:
        Ref: Vpc
      SecurityGroupId:
        Ref: EcsSecurityGroup
      VSwitchId:
        Ref: VSwitch
      ImageId: centos_7
      AllocatePublicIP: false
      InstanceType: ecs.c5.large
      SystemDiskSize: 40
      SystemDiskCategory: cloud_essd
      Password:
        Ref: EcsInstancePassword

Define scaling group resources

Define the following scaling group resources in the template: EssInstanceScalingGroup, EssInstanceScalingGroupEnable, and EssInstanceScalingConfiguration.

  • Use the Fn::GetAtt function to return the property value of a resource. In the following code snippet, the value of InstanceIds in EssInstanceScalingGroupEnable is returned. For more information, see Fn::GetAtt.

  • Use the Fn::Sub function to substitute the variable in an input string with the value that you specify. In the following code snippet, the value of ScalingConfigurationName in EssInstanceScalingConfiguration is substituted. For more information, see Fn::Sub.

Resources:
	EssInstanceScalingGroup:
    Type: ALIYUN::ESS::ScalingGroup
    Properties:
      ScalingGroupName:
        Ref: ALIYUN::StackName
      RemovalPolicys:
        - NewestInstance
      MinSize: 3
      MaxSize: 50
      VSwitchId:
        Ref: VSwitch
      DefaultCooldown: 300
  EssInstanceScalingConfiguration:
    Type: ALIYUN::ESS::ScalingConfiguration
    Properties:
      SecurityGroupId:
        Ref: EcsSecurityGroup
      ScalingGroupId:
        Ref: EssInstanceScalingGroup
      ScalingConfigurationName:
        Fn::Sub: sc-${ALIYUN::StackName}
      InstanceType: ecs.c5.large
      SystemDiskCategory: cloud_essd
      SystemDiskSize: 200
      ImageId: centos_7_9_x64_20G_alibase_20220727.vhd
      InstanceName:
        Fn::Join:
          - '-'
          - - Ref: ALIYUN::StackName
            - '[1,4]'
  EssInstanceScalingGroupEnable:
    Type: ALIYUN::ESS::ScalingGroupEnable
    Properties:
      ScalingRuleArisExecuteVersion: '1'
      ScalingConfigurationId:
        Ref: EssInstanceScalingConfiguration
      InstanceIds:
        Fn::GetAtt:
          - EcsInstanceGroup
          - InstanceIds
      ScalingGroupId:
        Ref: EssInstanceScalingGroup

Complete sample template

ROSTemplateFormatVersion: '2015-09-01'
Description: { }
Parameters:
  EcsInstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: Server login password, Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
       
    AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
    Label:
      en: Instance Password
       
    ConstraintDescription:
      en: Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
       
    MinLength: 8
    MaxLength: 30
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:
        Fn::Select:
          - '0'
          - Fn::GetAZs:
              Ref: ALIYUN::Region
      CidrBlock: 192.168.0.0/24
  EcsSecurityGroup:
    Type: ALIYUN::ECS::SecurityGroup
    Properties:
      SecurityGroupName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      SecurityGroupEgress:
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: intranet
  EcsInstance:
    Type: ALIYUN::ECS::Instance
    Properties:
      VpcId:
        Ref: Vpc
      SecurityGroupId:
        Ref: EcsSecurityGroup
      VSwitchId:
        Ref: VSwitch
      ImageId: centos_7
      AllocatePublicIP: false
      InstanceType: ecs.c5.large
      SystemDiskSize: 40
      SystemDiskCategory: cloud_essd
      Password:
        Ref: EcsInstancePassword
  EssInstanceScalingGroup:
    Type: ALIYUN::ESS::ScalingGroup
    Properties:
      ScalingGroupName:
        Ref: ALIYUN::StackName
      RemovalPolicys:
        - NewestInstance
      MinSize: 3
      MaxSize: 50
      VSwitchId:
        Ref: VSwitchId
      DefaultCooldown: 300
  EssInstanceScalingConfiguration:
    Type: ALIYUN::ESS::ScalingConfiguration
    Properties:
      SecurityGroupId:
        Ref: EcsSecurityGroup
      ScalingGroupId:
        Ref: EssInstanceScalingGroup
      ScalingConfigurationName:
        Fn::Sub: sc-${ALIYUN::StackName}
      InstanceType: ecs.c5.large
      SystemDiskCategory: cloud_essd
      SystemDiskSize: 200
      ImageId: centos_7_9_x64_20G_alibase_20220727.vhd
      InstanceName:
        Fn::Join:
          - '-'
          - - Ref: ALIYUN::StackName
            - '[1,4]'
  EssInstanceScalingGroupEnable:
    Type: ALIYUN::ESS::ScalingGroupEnable
    Properties:
      ScalingRuleArisExecuteVersion: '1'
      ScalingConfigurationId:
        Ref: EssInstanceScalingConfiguration
      InstanceIds:
        Fn::GetAtt:
          - EcsInstanceGroup
          - InstanceIds

Add parameter groups to a template and display the optional values of a parameter based on a filter condition

The preceding sample template declares multiple resources and their associations. The values of InstanceType and SystemDiskCategory in EcsInstance are literal values. Each time you create a stack in a different region, you must modify the template structure and change the property values.

In this case, you can add the Parameters section to improve the flexibility and reusability of the template.

Add parameter groups to a template

You can add the Metadata section to a template to group the parameters that are defined in the Parameters section and define a label for each group.

After you define resources and parameters in a template, you can group parameters by resource or by resource parameter. The following table lists parameter groups that are added based on the complete sample template. In this example, the parameters are grouped by resource name or resource parameter name.

Parameter group

Resource name

Resource parameter name

Basic network configurations

Vpc, VSwitch, and EcsSecurityGroup

VSwitchZoneId, VpcCidrBlock, and VSwitchCidrBlock

ECS configurations

EcsInstance

ECSInstanceType, ECSInstanceGroupCount, ECSImageId, ECSDiskCategory, and EcsInstancePassword

Scaling group configurations

EssInstanceScalingGroup, EssInstanceScalingGroupEnable, and EssInstanceScalingConfiguration

ESSGroupMaxSize

Display the optional values of a parameter based on a filter condition

If you want to specify a filter condition for a parameter and associate the parameter value with the filter condition chosen in the ROS console, you can specify AssociationProperty and AssociationPropertyMetadata in your template. In this example, the optional values of the ECSInstanceType parameter are displayed based on the value of ZoneId. To specify AssociationProperty and AssociationPropertyMetadata in ECSInstanceType, use ALIYUN::ECS::Instance to query the value of AssociationProperty that is supported by ECSInstanceType in the official documentation of ROS. The following value is queried: ALIYUN::ECS::Instance::InstanceType. Then, query the value of AssociationPropertyMetadata when AssociationProperty is set to ALIYUN::ECS::Instance::InstanceType and ZoneId is used as a filter condition. For more information about the official documentation, see AssociationProperty and AssociationPropertyMetadata.

Complete sample template

ROSTemplateFormatVersion: '2015-09-01'
Description:
  en: scaling-ecs.
   
Parameters:
  VSwitchZoneId:
    Type: String
    AssociationProperty: ALIYUN::ECS::Instance::ZoneId
    Description:
      en: Availability ID for existing switches.
       
    Label:
      en: VSwitch Zone ID
       
  VpcCidrBlock:
    Default: 192.168.0.0/16
    Label:
       
      en: VPC CIDR Block
    Type: String
    Description:
       
      en: New proprietary network IP address segment range, recommended use of the following IP address segments<br><font color='green'>[10.0.XX.XX/8]</font><br><font color='green'>[172.16.XX.XX/12]</font><br><font color='green'>[192.168.XX.XX/16]</font>.
  VSwitchCidrBlock:
    Default: 192.168.0.0/24
    Type: String
    Description:
       
      en: Must be a sub-network segment of the proprietary network and is not occupied by other VSwitches.
    Label:
       
      en: VSwitch CIDR Block
  ECSInstanceType:
    Type: String
    Label:
      en: Instance Type
       
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    AssociationPropertyMetadata:
      ZoneId: ${VSwitchZoneId}
      InstanceChargeType: ${InstanceChargeType}
  ECSDiskCategory:
    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>]'.
       
    AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
    AssociationPropertyMetadata:
      ZoneId: ${VSwitchZoneId}
      InstanceType: ${ECSInstanceType}
    Label:
      en: System Disk Type
       
  ECSImageId:
    AssociationProperty: ALIYUN::ECS::Image::ImageId
    Label:
       
      en: Image ID
    Description:
       
      en: Image ID, represents the image resource to startup one ECS instance, <font><a href='https://www.alibabacloud.com/help/doc-detail/112977.html' target='_blank'><b>View image resources</b></font color='blue'></a>.
    Type: String
  EcsInstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: Server login password, Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
       
    AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
    Label:
      en: Instance Password
       
    ConstraintDescription:
      en: Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
       
    MinLength: 8
    MaxLength: 30
    AssociationProperty: ALIYUN::ECS::Instance::Password
  ECSInstanceGroupCount:
    Type: Number
    Description:
      en: ECS Instance Count
       
    Label:
      en: Instance Count
       
    Default: 3
  ESSGroupMaxSize:
    Type: Number
    Label:
      en: The maximum number of ECS instances in ESS.
       
    Default: 50
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: VSwitchZoneId
      CidrBlock:
        Ref: VSwitchCidrBlock
  EcsSecurityGroup:
    Type: ALIYUN::ECS::SecurityGroup
    Properties:
      SecurityGroupName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      SecurityGroupIngress:
        - PortRange: 8080/8080
          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
  EcsInstanceGroup:
    Type: ALIYUN::ECS::InstanceGroup
    Properties:
      InstanceName:
        Fn::Join:
          - '-'
          - - Ref: ALIYUN::StackName
            - '[1,4]'
      VpcId:
        Ref: Vpc
      VSwitchId:
        Ref: VSwitch
      SecurityGroupId:
        Ref: EcsSecurityGroup
      SystemDiskCategory:
        Ref: ECSDiskCategory
      SystemDiskSize: 200
      MaxAmount:
        Ref: ECSInstanceGroupCount
      ImageId:
        Ref: ECSImageId
      InstanceType:
        Ref: ECSInstanceType
      Password:
        Ref: EcsInstancePassword
      AllocatePublicIP: false
  EssInstanceScalingGroupEnable:
    Type: ALIYUN::ESS::ScalingGroupEnable
    Properties:
      ScalingRuleArisExecuteVersion: '1'
      ScalingConfigurationId:
        Ref: EssInstanceScalingConfiguration
      InstanceIds:
        Fn::GetAtt:
          - EcsInstanceGroup
          - InstanceIds
      ScalingGroupId:
        Ref: EssInstanceScalingGroup
  EssInstanceScalingConfiguration:
    Type: ALIYUN::ESS::ScalingConfiguration
    Properties:
      SecurityGroupId:
        Ref: EcsSecurityGroup
      ScalingGroupId:
        Ref: EssInstanceScalingGroup
      ScalingConfigurationName:
        Fn::Sub: sc-${ALIYUN::StackName}
      InstanceType:
        Ref: ECSInstanceType
      SystemDiskCategory:
        Ref: ECSDiskCategory
      SystemDiskSize: 200
      ImageId:
        Ref: ECSImageId
      InstanceName:
        Fn::Join:
          - '-'
          - - Ref: ALIYUN::StackName
            - '[1,4]'
  EssInstanceScalingGroup:
    Type: ALIYUN::ESS::ScalingGroup
    Properties:
      ScalingGroupName:
        Ref: ALIYUN::StackName
      RemovalPolicys:
        - NewestInstance
      MinSize:
        Ref: ECSInstanceGroupCount
      MaxSize: 50
      VSwitchId:
        Ref: VSwitch
      DefaultCooldown: 300
Metadata:
  ALIYUN::ROS::Interface:
    ParameterGroups:
      - Parameters:
          - VSwitchZoneId
          - VpcCidrBlock
          - VSwitchCidrBlock
        Label:
          default:
             
            en: Basic Network Configuration
      - Parameters:
          - ECSInstanceType
          - ECSDiskCategory
          - ECSImageId
          - EcsInstancePassword
          - ECSInstanceGroupCount
        Label:
          default:
            en: Instance
             
      - Parameters:
          - ESSGroupMaxSize
        Label:
          default:
             
            en: ESS Configuration