批量创建云主机
更新时间 2026-03-26 13:53:51
最近更新时间: 2026-03-26 13:53:51
操作说明
本方案基于 天翼云 ROS(资源编排服务)控制台,实现云主机批量快速创建与销毁,有效提升资源交付效率,降低人工运维成本。
方案内置两类标准化编排模板,分别支持同配置批量创建与多规格差异化创建两种场景,用户可根据业务规模与实例需求灵活选用,快速完成批量资源部署。
适用场景
云主机批量快速搭建
多环境标准化批量部署
临时业务资源快速创建与释放
操作步骤
登录控制中心。
在控制台首页搜索“资源编排ROS”,或在左侧产品导航栏选择“管理工具 > 资源编排ROS”,进入资源编排控制台。
在左侧导航栏选择 模板管理。
在模板管理页面,单击创建模板,可参考创建模板完成模板配置。
模板默认基于华东1资源池, 可以根据需要可以进行调整。
模板1:创建多台相同配置云主机,模板使用count语法实现
terraform {
required_providers {
ctyun = {
source = "ctyun-it/ctyun"
version = "2.1.0"
}
}
}
provider "ctyun" {
az_name = var.az_name
}
variable "az_name" {
type = string
default = "cn-huadong1-jsnj1A-public-ctcloud"
description = "可用区名称"
}
variable "instance_name" {
type = string
default = "test-ecs"
description = "云主机名称"
}
variable "instance_count" {
type = number
default = 4
description = "云主机数量"
}
variable "image_id" {
type = string
default = "f9415853-b07d-4dd8-afb7-f48e10de151e"
description = "镜像ID"
}
variable "flavor_name" {
type = string
default = "c7.xlarge.2"
description = "规格名称"
}
variable "system_disk_type" {
type = string
default = "SAS"
description = "系统盘类型"
}
variable "system_disk_size" {
type = number
default = 40
description = "系统盘大小"
}
variable "password" {
type = string
sensitive = true
description = "密码"
}
variable "bandwidth" {
type = number
default = 10
description = "公网带宽"
}
# 创建vpc
resource "ctyun_vpc" "vpc_test" {
name = "vpc-for-ecs"
cidr = "192.168.0.0/16"
description = "terraform测试使用"
enable_ipv6 = true
}
# 在vpc下创建子网
resource "ctyun_subnet" "subnet_test" {
vpc_id = ctyun_vpc.vpc_test.id
name = "subnet-for-ecs"
cidr = "192.168.1.0/24"
description = "terraform测试使用"
dns = [
"114.114.114.114",
"8.8.8.8",
]
enable_ipv6 = true
}
# 开通N台按需云主机
resource "ctyun_ecs" "ecs_test" {
count = var.instance_count
instance_name = "${var.instance_name}-${count.index + 1}"
display_name = "${var.instance_name}-${count.index + 1}"
flavor_name = var.flavor_name
image_id = var.image_id
system_disk_type = var.system_disk_type
system_disk_size = var.system_disk_size
password = var.password
bandwidth = var.bandwidth
cycle_type = "on_demand"
vpc_id = ctyun_vpc.vpc_test.id
subnet_id = ctyun_subnet.subnet_test.id
}模板2:创建多台配置不同的云主机,模板使用for_each语法实现
terraform {
required_providers {
ctyun = {
source = "ctyun-it/ctyun"
version = "2.1.0"
}
}
}
provider "ctyun" {
az_name = var.az_name
}
variable "az_name" {
type = string
default = "cn-huadong1-jsnj1A-public-ctcloud"
description = "可用区名称"
}
variable "password" {
type = string
sensitive = true
description = "密码"
}
# 创建vpc
resource "ctyun_vpc" "vpc_test" {
name = "vpc-for-ecs2"
cidr = "192.168.0.0/16"
description = "terraform测试使用"
enable_ipv6 = true
}
# 在vpc下创建子网
resource "ctyun_subnet" "subnet_test" {
vpc_id = ctyun_vpc.vpc_test.id
name = "subnet-for-ecs2"
cidr = "192.168.1.0/24"
description = "terraform测试使用"
dns = [
"114.114.114.114",
"8.8.8.8",
]
enable_ipv6 = true
}
# 创建两台不同配置的云主机
locals {
ecs = [
{
key = "test1"
display_name = "tf-ecs-1"
instance_name = "tf-ecs-1"
flavor_name = "s7.large.2"
image_id = "f9415853-b07d-4dd8-afb7-f48e10de151e"
system_disk_type = "SATA"
system_disk_size = 500
subnet_id = ctyun_subnet.subnet_test.id
vpc_id = ctyun_vpc.vpc_test.id
password = var.password
bandwidth = null
cycle_type = "month"
cycle_count = 3
},
{
key = "test2"
display_name = "tf-ecs-2"
instance_name = "tf-ecs-2"
flavor_name = "s7.large.4"
system_disk_type = "SAS"
system_disk_size = 120
image_id = "f9415853-b07d-4dd8-afb7-f48e10de151e"
subnet_id = ctyun_subnet.subnet_test.id
vpc_id = ctyun_vpc.vpc_test.id
password = var.password
bandwidth = 10
cycle_type = "on_demand"
cycle_count = null
},
]
}
resource "ctyun_ecs" "ecs_test" {
for_each = { for s in local.ecs : s.key => s }
display_name = each.value.display_name
instance_name = each.value.instance_name
flavor_name = each.value.flavor_name
image_id = each.value.image_id
system_disk_type = each.value.system_disk_type
system_disk_size = each.value.system_disk_size
vpc_id = each.value.vpc_id
subnet_id = each.value.subnet_id
cycle_type = each.value.cycle_type
cycle_count = each.value.cycle_count
bandwidth = each.value.bandwidth
password = each.value.password
is_destroy_instance = true # 包周期退订时进行销毁
}