使用Count功能批量創(chuàng)建資源
資源編排服務(wù)ROS(Resource Orchestration Service)支持Count功能,用于批量創(chuàng)建資源。本文將以批量創(chuàng)建ECS實例和EIP,并為ECS實例綁定EIP為例,為您介紹如何使用Count功能。
背景信息
ALIYUN::VPC::EIP類型用于申請彈性公網(wǎng)IP(EIP)。如果需要申請多個彈性公網(wǎng)IP,則需要在模板中編寫多個ALIYUN::VPC::EIP資源,這樣模板會變得冗長。此時您可以使用Count功能批量創(chuàng)建資源。Count功能詳情,請參見Count。此外,使用Count批量創(chuàng)建多組資源時可以通過為資源指定DependsOn并設(shè)置參數(shù)(ParallelCount)進(jìn)行資源創(chuàng)建的并發(fā)控制。
步驟一:編寫模板
您可以使用Count功能編寫模板,創(chuàng)建以下資源:
1個VPC(專有網(wǎng)絡(luò))
1個vSwitch(交換機(jī))
1個SecurityGroup(安全組)
4個ECS(按量付費的ECS)
4個EIP(彈性公網(wǎng)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
創(chuàng)建4個EIP。
Count取值為4。使用Count功能時,ParallelCount取值為2,此時會分兩批創(chuàng)建4個EIP資源。通過ROS的預(yù)處理,會生成名為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
創(chuàng)建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
創(chuàng)建4個EipBind資源,通過ALIYUN::Index偽參數(shù)逐一綁定ECS實例和EIP。
Count取值為4。使用Count功能時,ParallelCount取值為2,此時會分兩批創(chuàng)建4個EipBind資源。通過ROS的預(yù)處理,會生成名為EipBind[0]和EipBind[1](第一批)、EipBind[2]和EipBind[3](第二批)的4個資源。ALIYUN::Index在Count中使用,在預(yù)處理時會被替換為相應(yīng)的數(shù)值。本示例中將逐一綁定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
步驟二:創(chuàng)建資源棧
登錄資源編排控制臺。
在左側(cè)導(dǎo)航欄單擊資源棧。
在頂部菜單欄的地域下拉列表,選擇資源棧的所在地域,例如:華東1(杭州)。
在資源棧列表頁面,單擊創(chuàng)建資源棧,然后在下拉列表中選擇使用ROS。
在選擇模板頁面的指定模板區(qū)域,單擊選擇已有模板。
在模板錄入方式區(qū)域單擊輸入模板,輸入步驟一:編寫模板中編寫的模板內(nèi)容,然后單擊下一步。
在配置參數(shù)頁面,根據(jù)控制臺提示,配置資源棧名稱和模板參數(shù)。
在配置資源棧區(qū)塊,根據(jù)控制臺提示,配置資源棧策略、失敗時回滾、超時設(shè)置、刪除保護(hù)、RAM角色、標(biāo)簽和資源組,然后單擊下一步。
資源的創(chuàng)建或更新未在超時設(shè)置的時間內(nèi)完成,系統(tǒng)自動判斷該操作失敗,再根據(jù)失敗時回滾設(shè)置,判斷是否回滾到創(chuàng)建或更新資源之前的狀態(tài)。
在合規(guī)預(yù)檢頁面,完成合規(guī)檢測,然后單擊下一步。
說明目前合規(guī)預(yù)檢功能僅支持為部分資源提供合規(guī)預(yù)檢功能。更多信息,請參見資源編排支持合規(guī)預(yù)檢。
在檢測規(guī)則區(qū)域,添加檢測規(guī)則。
您可以根據(jù)ROS模板中的云資源選擇需要檢測的規(guī)則。
單擊開始檢測。
您可以根據(jù)不合規(guī)或不適用資源的修正方案修改模板內(nèi)容,從而保證資源的合規(guī)性。
在檢查并確認(rèn)頁面,單擊創(chuàng)建。
步驟三(可選):查詢資源和模板
資源棧創(chuàng)建成功后,您可以按需在資源棧詳情頁查詢經(jīng)過ROS預(yù)處理的資源和模板信息。
在資源棧詳情頁,單擊資源頁簽,查詢資源信息。
在資源棧詳情頁,單擊模板頁簽,查詢模板信息。
本文示例中,預(yù)處理后的模板如下:
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