使用Count功能批量创建资源
资源编排服务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)
模板示例代码和模板说明如下:
模板示例代码
ROSTemplateFormatVersion: '2015-09-01' Parameters: Count: Type: Number Default: 4 ZoneId: Type: String InstanceType: Type: String Default: ecs.c6.large Password: Type: String Default: Abc1**** NoEcho: true ParallelCount: Type: Number Default: 2 SystemDiskCategory: Type: String Resources: Vpc: Type: ALIYUN::ECS::VPC Properties: CidrBlock: 10.0.0.0/8 VpcName: test-resource-count VSwitch: Type: ALIYUN::ECS::VSwitch Properties: CidrBlock: 10.0.10.0/24 ZoneId: Ref: ZoneId VpcId: Ref: Vpc SecurityGroup: Type: ALIYUN::ECS::SecurityGroup Properties: SecurityGroupName: test-resource-count VpcId: Ref: Vpc Eip: Type: ALIYUN::VPC::EIP Count: Ref: Count DependsOn: Fn::Select: - Fn::Calculate: - 1-{0}//{1} - 0 - - Fn::Min: - Ref: ALIYUN::Index - Ref: ParallelCount - Ref: ParallelCount - - Fn::Replace: - index: Fn::Calculate: - '{0}-{1}' - 0 - - Ref: ALIYUN::Index - Ref: ParallelCount - Eip[index] Properties: Bandwidth: 5 Servers: Type: ALIYUN::ECS::InstanceGroup Properties: ImageId: centos_7 InstanceType: Ref: InstanceType VpcId: Ref: Vpc VSwitchId: Ref: VSwitch SecurityGroupId: Ref: SecurityGroup Password: Ref: Password AllocatePublicIP: false MaxAmount: Ref: Count SystemDiskCategory: Ref: SystemDiskCategory EipBind: Type: ALIYUN::VPC::EIPAssociation Count: Ref: Count DependsOn: Fn::Select: - Fn::Calculate: - 1-{0}//{1} - 0 - - Fn::Min: - Ref: ALIYUN::Index - Ref: ParallelCount - Ref: ParallelCount - - Fn::Replace: - index: Fn::Calculate: - '{0}-{1}' - 0 - - Ref: ALIYUN::Index - Ref: ParallelCount - EipBind[index] Properties: InstanceId: Fn::Select: - Ref: ALIYUN::Index - Fn::GetAtt: - Servers - InstanceIds AllocationId: Fn::Select: - Ref: ALIYUN::Index - Ref: Eip Outputs: InstanceIds: Value: Fn::GetAtt: - Servers - InstanceIds AllocationIds: Value: Ref: Eip EipAddresses: Value: Fn::GetAtt: - Eip - EipAddress
模板说明
- 创建4个EIP。
Count取值为4。使用Count功能时,ParallelCount取值为2,此时会分两批创建4个EIP资源。通过ROS的预处理,会生成名为Eip[0]和Eip[1](第一批)、Eip[2]和Eip[3](第二批)的4个EIP资源。
Eip: Type: ALIYUN::VPC::EIP Count: Ref: Count DependsOn: Fn::Select: - Fn::Calculate: - 1-{0}//{1} - 0 - - Fn::Min: - Ref: ALIYUN::Index - Ref: ParallelCount - Ref: ParallelCount - - Fn::Replace: - index: Fn::Calculate: - '{0}-{1}' - 0 - - Ref: ALIYUN::Index - Ref: ParallelCount - Eip[index] Properties: Bandwidth: 5
- 创建4个ECS实例。
Count取值为4。将ALIYUN::ECS::InstanceGroup的MaxAmount指定为Count,取值为4,生成4个ECS资源。
Servers: Type: ALIYUN::ECS::InstanceGroup Properties: ImageId: centos_7 InstanceType: Ref: InstanceType VpcId: Ref: Vpc VSwitchId: Ref: VSwitch SecurityGroupId: Ref: SecurityGroup Password: Ref: Password AllocatePublicIP: false MaxAmount: Ref: Count SystemDiskCategory: Ref: SystemDiskCategory
- 创建4个EipBind资源,通过ALIYUN::Index伪参数逐一绑定ECS实例和EIP。
Count取值为4。使用Count功能时,ParallelCount取值为2,此时会分两批创建4个EipBind资源。通过ROS的预处理,会生成名为EipBind[0]和EipBind[1](第一批)、EipBind[2]和EipBind[3](第二批)的4个资源。ALIYUN::Index在Count中使用,在预处理时会被替换为相应的数值。本示例中将逐一绑定ECS实例与EIP,即第1个实例绑定Eip[0]、第2个实例绑定Eip[1]、第3个实例绑定Eip[2]、第4个实例绑定Eip[3]。
EipBind: Type: ALIYUN::VPC::EIPAssociation Count: Ref: Count DependsOn: Fn::Select: - Fn::Calculate: - 1-{0}//{1} - 0 - - Fn::Min: - Ref: ALIYUN::Index - Ref: ParallelCount - Ref: ParallelCount - - Fn::Replace: - index: Fn::Calculate: - '{0}-{1}' - 0 - - Ref: ALIYUN::Index - Ref: ParallelCount - EipBind[index] Properties: InstanceId: Fn::Select: - Ref: ALIYUN::Index - Fn::GetAtt: - Servers - InstanceIds AllocationId: Fn::Select: - Ref: ALIYUN::Index - Ref: Eip
- 创建4个EIP。
步骤二:创建资源栈
- 登录资源编排控制台。
- 在左侧导航栏单击资源栈。
- 在页面左上角的地域下拉列表,选择资源栈的所在地域。
- 在资源栈列表页面,单击创建资源栈,然后在下拉列表中选择使用新资源(标准)。
- 在选择模板页面的指定模板区域,单击选择已有模板。
- 在模板录入方式区域单击输入模板,输入步骤一:编写模板中编写的模板内容,然后单击下一步。
- 在配置模板参数页面,根据控制台提示,配置资源栈名称和参数录入,然后单击下一步。
在合规预检页面,完成合规检测,然后单击下一步。
说明目前合规预检功能仅支持为部分资源提供合规预检功能。更多信息,请参见资源编排支持合规预检。
在检测规则区域,添加检测规则。
您可以根据ROS模板中的云资源选择需要检测的规则。
单击开始检测。
您可以根据不合规或不适用资源的修正方案修改模板内容,从而保证资源的合规性。
- 在配置资源栈页面,根据控制台提示,配置资源栈策略、失败时回滚、超时设置、删除保护、RAM角色、标签和资源组,然后单击下一步。资源的创建或更新未在超时设置的时间内完成,系统自动判断该操作失败,再根据失败时回滚设置,判断是否回滚到创建或更新资源之前的状态。
- 在检查并确认页面,单击创建。
步骤三(可选):查询资源和模板
资源栈创建成功后,您可以按需在资源栈详情页查询经过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