资源编排服务ROS(Resource Orchestration Service)支持Count功能,用于批量创建资源。本文将以批量创建ECS实例和EIP,并为ECS实例绑定EIP为例,为您介绍如何使用Count功能。
背景信息
ALIYUN::VPC::EIP类型用于申请弹性公网IP(EIP)。如果需要申请多个弹性公网IP,则需要在模板中编写多个ALIYUN::VPC::EIP资源,这样模板会变得冗长。此时您可以使用Count功能批量创建资源。Count功能详情,请参见Count。此外,使用Count批量创建多组资源时可以通过为资源指定DependsOn并设置参数(ParallelCount)进行资源创建的并发控制。
步骤一:编写模板
您可以使用Count功能编写模板,创建以下资源:
1个VPC(专有网络)
1个vSwitch(交换机)
1个SecurityGroup(安全组)
4个ECS(按量付费的ECS)
4个EIP(弹性公网IP)
模板示例代码和模板说明如下:
步骤二:创建资源栈
登录资源编排控制台。
在左侧导航栏单击资源栈。
在顶部菜单栏的地域下拉列表,选择资源栈的所在地域,例如:华东1(杭州)。
在资源栈列表页面,单击创建资源栈,然后在下拉列表中选择使用ROS。
在选择模板页面的指定模板区域,单击选择已有模板。
在模板录入方式区域单击输入模板,输入步骤一:编写模板中编写的模板内容,然后单击下一步。
在配置参数页面,根据控制台提示,配置资源栈名称和模板参数。
在配置资源栈区块,根据控制台提示,配置资源栈策略、失败时回滚、超时设置、删除保护、RAM角色、标签和资源组,然后单击下一步。
资源的创建或更新未在超时设置的时间内完成,系统自动判断该操作失败,再根据失败时回滚设置,判断是否回滚到创建或更新资源之前的状态。
在合规预检页面,完成合规检测,然后单击下一步。
目前合规预检功能仅支持为部分资源提供合规预检功能。更多信息,请参见资源编排支持合规预检。
在检测规则区域,添加检测规则。
您可以根据ROS模板中的云资源选择需要检测的规则。
单击开始检测。
您可以根据不合规或不适用资源的修正方案修改模板内容,从而保证资源的合规性。
在检查并确认页面,单击创建。
步骤三(可选):查询资源和模板
资源栈创建成功后,您可以按需在资源栈详情页查询经过ROS预处理的资源和模板信息。
在资源栈详情页,单击资源页签,查询资源信息。
在资源栈详情页,单击模板页签,查询模板信息。
本文示例中,预处理后的模板如下:
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
- 本页导读 (1)
- 背景信息
- 步骤一:编写模板
- 步骤二:创建资源栈
- 步骤三(可选):查询资源和模板