Learn how to edit a Resource Orchestration Service (ROS) template to manage multiple Elastic Compute Service (ECS) instances by using a scaling group, progressing from a simple configuration to a more complex one.
Prerequisites
Familiarize yourself with the template syntax and structure. For more information, see Quick Start.
Sample scenario
Create a scaling group with scaling rules in an Alibaba Cloud virtual private cloud (VPC), add ECS instances to the group, and then scale the instances out or in based on the configured rules.
Usage notes
View the property details for each resource type. For more information, see View resource types.
Each resource type defines the type, requirements, and update policy for its properties. Required properties must be declared in the `Properties` section of the `Resources` block. Optional properties do not need to be declared. If a property allows updates, you can modify it in a new template and then update the stack to apply the changes. Otherwise, the property cannot be updated.
Edit the template
You can find the required resource types in the resource type index document. For more information, see Resource type index.
This scenario requires you to create a VPC (ALIYUN::ECS::VPC), an ECS instance (ALIYUN::ECS::Instance), and a scaling group (ALIYUN::ESS::ScalingGroup). You also need a vSwitch (ALIYUN::ECS::VSwitch) and a security group (ALIYUN::ECS::SecurityGroup) for the ECS instance, a scaling configuration (ALIYUN::ESS::ScalingConfiguration) for the scaling group, and a resource to enable the scaling group (ALIYUN::ESS::ScalingGroupEnable).
Define template resources and their dependencies
Define basic network resources
Define the basic network resources `Vpc`, `vSwitch`, and `EcsSecurityGroup` in the template.
-
Use the `Ref` intrinsic function and the `ALIYUN::StackName` pseudo parameter to retrieve the stack name and use it as the value for resource properties, such as `VpcName` in `Vpc` and `VSwitchName` in `vSwitch`. For more information, see Ref and ALIYUN::StackName.
-
Use the `Fn::Select` and `Fn::GetAZs` intrinsic functions with the `ALIYUN::Region` pseudo parameter to retrieve the ID of the first zone in the region where the stack is created, such as for the `ZoneId` property of `vSwitch`. 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 ECS resource `EcsInstanceGroup` in the template.
Use the `Ref` intrinsic function to reference the logical names of other resources in the template, such as `VpcId` of the `Vpc` resource, `SecurityGroupId` of the `EcsSecurityGroup` resource, and `VSwitchId` of the `vSwitch` resource. For more information, see Ref.
Resources:
EcsInstanceGroup:
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 scaling group resources `EssInstanceScalingGroup`, `EssInstanceScalingGroupEnable`, and `EssInstanceScalingConfiguration` in the template.
-
Use the `Fn::GetAtt` intrinsic function to retrieve the value of a resource attribute, such as `InstanceIds` in `EssInstanceScalingGroupEnable`. For more information, see Fn::GetAtt.
-
Use the `Fn::Sub` intrinsic function to substitute variables in an input string with specified values. For example, you can use this function for the `ScalingConfigurationName` property in `EssInstanceScalingConfiguration`. 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: The logon password for the server. It must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters. Special characters include ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/.
AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
Label: Instance Password
ConstraintDescription: The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters. Special characters include ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/.
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: 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:
- EcsInstance
- InstanceIds
Add template parameter groups and dynamically get parameter configurations
The preceding template defines resources and their dependencies, but properties such as `InstanceType` and `SystemDiskCategory` for `EcsInstance` use static values. You would need to modify the template every time you create stacks in a different region.
To make the template more flexible and reusable, add a `Parameters` section.
Add template parameter groups
Use the `Metadata` section in the template to group parameters and define group labels.
After you define the template resources and parameters, group the parameters by their corresponding resources as follows:
|
Resource parameter categorization |
Resource name |
Parameter name |
|
Basic network configuration |
|
|
|
ECS configuration |
|
|
|
Scaling group resource configuration |
|
|
Dynamically get parameter settings
Take `EcsInstanceType` as an example. To set filter conditions and dynamically select parameter values in the console, find the supported `AssociationProperty` value for the parameter. The value `ALIYUN::ECS::Instance::InstanceType` is documented under the `AssociationProperty` and `AssociationPropertyMetadata` section for the `ALIYUN::ECS::Instance` resource type, with `ZoneId` as the filter condition. For more information, see AssociationProperty and AssociationPropertyMetadata.
Complete sample template
ROSTemplateFormatVersion: '2015-09-01'
Description:
en: An ECS deployment that supports auto scaling.
Parameters:
VSwitchZoneId:
Type: String
AssociationProperty: ALIYUN::ECS::Instance::ZoneId
Description:
en: The zone ID of the vSwitch.
Label:
en: VSwitch Zone ID
VpcCidrBlock:
Default: 192.168.0.0/16
Label:
en: VPC CIDR Block
Type: String
Description:
en: The CIDR block for the new VPC. We recommend that you use one of the following CIDR blocks:<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: The CIDR block must be a subnet of the VPC and must not be in use by another vSwitch.
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>Valid values:</font>[cloud_efficiency: <font color=''green''>ultra disk</font>]<br>[cloud_ssd: <font color=''green''>standard SSD</font>]<br>[cloud_essd: <font color=''green''>enterprise SSD</font>]<br>[cloud: <font color=''green''>basic disk</font>]<br>[ephemeral_ssd: <font color=''green''>local SSD</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: The ID of the image used to create the ECS instance. <font><a href='https://www.alibabacloud.com/help/doc-detail/112977.html' target='_blank'><b>View image resources</font color='blue'></a>
Type: String
EcsInstancePassword:
NoEcho: true
Type: String
Description:
en: The logon password for the server. The password must be 8 to 30 characters in length and contain characters from at least three of the following categories: uppercase letters, lowercase letters, digits, and special characters: ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/.
AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
Label:
en: Instance Password
ConstraintDescription:
en: The password must be 8 to 30 characters in length and contain characters from at least three of the following categories: uppercase letters, lowercase letters, digits, and special characters: ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/.
MinLength: 8
MaxLength: 30
AssociationProperty: ALIYUN::ECS::Instance::Password
ECSInstanceGroupCount:
Type: Number
Description:
en: The number of ECS instances.
Label:
en: Instance Count
Default: 3
ESSGroupMaxSize:
Type: Number
Label:
en: The maximum number of ECS instances in the scaling group.
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: ECS Instance Configuration
- Parameters:
- ESSGroupMaxSize
Label:
default:
en: Scaling Group Configuration