Resource Orchestration Service (ROS) provides the Count feature to create multiple resources in batches. This example uses Count to create multiple ECS instances and elastic IP addresses (EIPs) and bind them to their corresponding ECS instances.
Background
Provisioning an elastic IP address (EIP) requires the ALIYUN::VPC::EIP resource type. Declaring multiple ALIYUN::VPC::EIP resources makes the template verbose. The Count feature lets you create multiple resources efficiently. For more information, see Count. You can also specify the DependsOn attribute and set the ParallelCount parameter to control resource creation concurrency.
Step 1: Write the template
This template uses the Count feature to create the following resources:
-
One Virtual Private Cloud (VPC)
-
One vSwitch
-
One security group
-
Four pay-as-you-go ECS instances
-
Four elastic IP addresses (EIPs)
The following sections provide the sample template and explain its components.
Step 2: Create a stack
-
Log on to the ROS console.
-
In the navigation pane on the left, click Stacks.
-
In the top navigation bar, select the region where you want to create the stack, such as China (Hangzhou).
-
On the Stacks page, click Create Stack and select Use ROS from the drop-down list.
-
On the Select Template page, in the Specify Template section, click Select an Existing Template.
-
In the Template Import Method section, click Enter Template Content, paste the template content from Step 1: Write the template, and then click Next.
-
On the Configure Parameters page, set the Stack Name and configure the parameters in the Template Parameters section.
-
In the Configure Stack Settings section, configure Stack Policy, Rollback on Failure, Timeout Period, Deletion Protection, RAM Role, Tags, and Resource Group. Then, click Next.
If a resource is not created or updated within the specified Timeout Period, the operation fails. The system then uses the Rollback on Failure setting to determine whether to roll back the resources.
-
On the Compliance Precheck page, complete the compliance check, and then click Next.
NoteThe compliance precheck feature is available only for specific resources. For more information, see Compliance precheck.
-
In the Detection Rules section, add detection rules.
Select detection rules based on the cloud resources defined in the ROS template.
-
Click Start Check.
If a resource is found to be Non-compliant, you can click Remediation Plan and modify the cloud resource configuration or ROS template content based on the Remediation Plan to ensure resource compliance.
-
-
On the Check and Confirm page, click Create.
Step 3 (Optional): View resources and template
After the stack is created, you can view the resources and preprocessed template on the stack details page.
-
On the stack details page, click the Resources tab to view resource information.
-
On the stack details page, click the Templates tab to view template information.
The following code provides an example of the preprocessed template:
ROSTemplateFormatVersion: '2015-09-01' Resources: EipBind[1]: Type: ALIYUN::VPC::EIPAssociation Properties: InstanceId: Fn::Select: - 1 - Fn::GetAtt: - Servers - InstanceIds AllocationId: Ref: Eip[1] Eip[1]: Type: ALIYUN::VPC::EIP Properties: Bandwidth: 5 SecurityGroup: Type: ALIYUN::ECS::SecurityGroup Properties: VpcId: Ref: Vpc SecurityGroupName: test-resource-count Servers: Type: ALIYUN::ECS::InstanceGroup Properties: SystemDiskCategory: Ref: SystemDiskCategory VpcId: Ref: Vpc SecurityGroupId: Ref: SecurityGroup ImageId: centos_7 AllocatePublicIP: false VSwitchId: Ref: VSwitch Password: Ref: Password InstanceType: Ref: InstanceType MaxAmount: Ref: Count Eip[2]: Type: ALIYUN::VPC::EIP Properties: Bandwidth: 5 DependsOn: Eip[0] Eip[0]: Type: ALIYUN::VPC::EIP Properties: Bandwidth: 5 Vpc: Type: ALIYUN::ECS::VPC Properties: VpcName: test-resource-count CidrBlock: 10.0.0.0/8 Eip[3]: Type: ALIYUN::VPC::EIP Properties: Bandwidth: 5 DependsOn: Eip[1] VSwitch: Type: ALIYUN::ECS::VSwitch Properties: VpcId: Ref: Vpc CidrBlock: 10.0.10.0/24 ZoneId: Ref: ZoneId EipBind[3]: Type: ALIYUN::VPC::EIPAssociation Properties: InstanceId: Fn::Select: - 3 - Fn::GetAtt: - Servers - InstanceIds AllocationId: Ref: Eip[3] DependsOn: EipBind[1] EipBind[0]: Type: ALIYUN::VPC::EIPAssociation Properties: InstanceId: Fn::Select: - 0 - Fn::GetAtt: - Servers - InstanceIds AllocationId: Ref: Eip[0] EipBind[2]: Type: ALIYUN::VPC::EIPAssociation Properties: InstanceId: Fn::Select: - 2 - Fn::GetAtt: - Servers - InstanceIds AllocationId: Ref: Eip[2] DependsOn: EipBind[0] Parameters: Count: Default: 4 Type: Number SystemDiskCategory: Type: String ZoneId: Type: String Password: Default: Abc1**** NoEcho: true Type: String InstanceType: Default: ecs.c6.large Type: String ParallelCount: Default: 2 Type: Number Outputs: AllocationIds: Value: - Ref: Eip[0] - Ref: Eip[1] - Ref: Eip[2] - Ref: Eip[3] InstanceIds: Value: Fn::GetAtt: - Servers - InstanceIds EipAddresses: Value: - Fn::GetAtt: - Eip[0] - EipAddress - Fn::GetAtt: - Eip[1] - EipAddress - Fn::GetAtt: - Eip[2] - EipAddress - Fn::GetAtt: - Eip[3] - EipAddress