本文为您介绍如何通过资源编排服务(ROS)创建多台ECS实例。

您可以在模板中使用Metadata对Parameters中定义的参数进行归类。当资源类型较多时,使用Metadata可以使控制台的代码看起来更加整洁、直观。创建多台ECS实例的注意事项,请参见ALIYUN::ECS::InstanceGroup。关于如何通过ROS控制台创建资源栈,请参见创建资源栈

JSON格式模板
{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Description": "创建多台ECS实例",
  "Metadata": {
    "ALIYUN::ROS::Interface": {
      "ParameterGroups": [
        {
          "Parameters": [
            "ImageId",
            "InstanceType",
            "ZoneId",
            "LoginPassword",
            "ECSAmount",
            "DiskSize",
            "DiskCategory",
            "SystemDiskSize",
            "SystemDiskCategory",
            "InstanceNamePrefix",
            "HostNamePrefix"
          ],
          "Label": {
            "default": "ECS"
          }
        }
      ],
      "TemplateTags": [
        "ECS"
      ]
    }
  },
  "Parameters": {
    "ZoneId": {
      "AssociationProperty": "ALIYUN::ECS::Instance:ZoneId",
      "Type": "String",
      "Description": "可用区是指在同一地域内,电力和网络互相独立的物理区域。在同一专有网络内可用区与可用区之间内网互通,可用区之间能做到故障隔离。",
      "Label": "可用区"
    },
    "SystemDiskSize": {
      "Type": "Number",
      "Description": "系统盘大小,40 - 500, 单位:GB",
      "Label": "系统盘",
      "Default": 100
    },
    "PublicIP": {
      "Type": "Boolean",
      "Description": "是否分配公网IP",
      "Label": "分配公网IP",
      "Default": true
    },
    "ECSAmount": {
      "Type": "String",
      "Label": "ECS实例数量",
      "Default": "3"
    },
    "ImageId": {
      "Type": "String",
      "Description": "镜像ID, 表示要启动一个ECS实例的镜像资源, <a href='#/product/cn-hangzhou/list/imageList' target='_blank'>查看镜像资源</a>",
      "Label": "ECS镜像ID",
      "Default": "centos_7"
    },
    "DiskSize": {
      "Type": "String",
      "Description": "数据盘大小, 单位:GB",
      "Label": "数据盘",
      "Default": "900"
    },
    "InstanceNamePrefix": {
      "Type": "String",
      "Description": "2-128个字符,以大小写英文或中文开头,可包含数字、下划线(_)、点(.)或连字符(-)。",
      "Label": "实例名称",
      "MinLength": 2,
      "MaxLength": 128,
      "Default": "Instance"
    },
    "HostNamePrefix": {
      "Type": "String",
      "Description": "2-64个字符,允许使用点(.)分隔成多段,每段允许使用大小写字母、数字或连字符(-),但不能连续使用点(.)或连字符(-),不能以点(.)或连字符(-)开头或结尾。",
      "Label": "主机名",
      "MinLength": 2,
      "MaxLength": 64,
      "Default": "Host"
    },
    "DiskCategory": {
      "Type": "String",
      "Description": "数据盘类型",
      "AllowedValues": [
        "cloud",
        "cloud_efficiency",
        "cloud_ssd",
        "ephemeral_ssd"
      ],
      "Label": "数据盘类型",
      "Default": "cloud_efficiency"
    },
    "InstanceType": {
      "Type": "String",
      "Description": "ECS实例类型, <a href='#/product/cn-hangzhou/list/typeList' target='_blank'>查看实例类型</a>",
      "AllowedValues": [
        "ecs.g5.large",
        "ecs.c5.large",
        "ecs.g5.xlarge",
        "ecs.c5.xlarge"
      ],
      "Label": "ECS实例类型",
      "Default": "ecs.c5.large"
    },
    "SystemDiskCategory": {
      "Type": "String",
      "Description": "系统盘类型",
      "AllowedValues": [
        "cloud",
        "cloud_efficiency",
        "cloud_ssd",
        "ephemeral_ssd"
      ],
      "Label": "系统盘类型",
      "Default": "cloud_ssd"
    },
    "LoginPassword": {
      "NoEcho": true,
      "Type": "String",
      "Description": "ECS登录密码",
      "AllowedPattern": "[a-zA-Z0-9-\\(\\)\\`\\~\\!@\\#\\$%\\^&\\*-+=\\|\\{\\}\\[\\]\\:\\;\\‘\\,\\.\\?\\/]*",
      "Label": "ECS登录密码",
      "Confirm": true,
      "MinLength": 8,
      "MaxLength": 30
    }
  },
  "Resources": {
    "VSwitch": {
      "Type": "ALIYUN::ECS::VSwitch",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "ZoneId": {
          "Ref": "ZoneId"
        },
        "CidrBlock": "192.168.0.0/24",
        "VSwitchName": "MyVsw"
      }
    },
    "SG": {
      "Type": "ALIYUN::ECS::SecurityGroup",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "SecurityGroupName": "MySG",
        "SecurityGroupIngress": [
          {
            "PortRange": "-1/-1",
            "Priority": 1,
            "SourceCidrIp": "0.0.0.0/0",
            "IpProtocol": "all",
            "NicType": "internet"
          }
        ],
        "SecurityGroupEgress": [
          {
            "PortRange": "-1/-1",
            "Priority": 1,
            "IpProtocol": "all",
            "DestCidrIp": "0.0.0.0/0",
            "NicType": "intranet"
          }
        ]
      }
    },
    "ECS": {
      "Type": "ALIYUN::ECS::InstanceGroup",
      "Properties": {
        "IoOptimized": "optimized",
        "PrivateIpAddress": "192.168.0.1",
        "DiskMappings": [
          {
            "Category": {
              "Ref": "DiskCategory"
            },
            "Size": {
              "Ref": "DiskSize"
            }
          }
        ],
        "ZoneId": {
          "Ref": "ZoneId"
        },
        "SystemDiskSize": {
          "Ref": "SystemDiskSize"
        },
        "SecurityGroupId": {
          "Ref": "SG"
        },
        "VSwitchId": {
          "Ref": "VSwitch"
        },
        "MaxAmount": {
          "Ref": "ECSAmount"
        },
        "SystemDiskCategory": {
          "Ref": "SystemDiskCategory"
        },
        "InstanceName": {
          "Fn::Join": [
            "",
            [
              {
                "Ref": "InstanceNamePrefix"
              },
              "[1,3]"
            ]
          ]
        },
        "VpcId": {
          "Ref": "VPC"
        },
        "MinAmount": {
          "Ref": "ECSAmount"
        },
        "ImageId": {
          "Ref": "ImageId"
        },
        "AllocatePublicIP": {
          "Ref": "PublicIP"
        },
        "InstanceType": {
          "Ref": "InstanceType"
        },
        "HostName": {
          "Fn::Join": [
            "",
            [
              {
                "Ref": "HostNamePrefix"
              },
              "[1,3]"
            ]
          ]
        },
        "Password": {
          "Ref": "LoginPassword"
        }
      }
    },
    "VPC": {
      "Type": "ALIYUN::ECS::VPC",
      "Properties": {
        "CidrBlock": "192.168.0.0/16",
        "VpcName": "MyVPC"
      }
    }
  },
  "Outputs": {
    "ECS实例ID": {
      "Value": {
        "Fn::GetAtt": [
          "ECS",
          "InstanceIds"
        ]
      }
    },
    "公网IP": {
      "Value": {
        "Fn::GetAtt": [
          "ECS",
          "PublicIps"
        ]
      }
    }
  }
}
YAML格式模板
ROSTemplateFormatVersion: '2015-09-01'
Description: 创建多台ECS实例
Metadata:
  'ALIYUN::ROS::Interface':
    ParameterGroups:
      - Parameters:
          - ImageId
          - InstanceType
          - ZoneId
          - LoginPassword
          - ECSAmount
          - DiskSize
          - DiskCategory
          - SystemDiskSize
          - SystemDiskCategory
          - InstanceNamePrefix
          - HostNamePrefix
        Label:
          default: ECS
    TemplateTags:
      - ECS
Parameters:
  ZoneId:
    AssociationProperty: 'ALIYUN::ECS::Instance:ZoneId'
    Type: String
    Description: 可用区是指在同一地域内,电力和网络互相独立的物理区域。在同一专有网络内可用区与可用区之间内网互通,可用区之间能做到故障隔离。
    Label: 可用区
  SystemDiskSize:
    Type: Number
    Description: '系统盘大小,40 - 500, 单位:GB'
    Label: 系统盘
    Default: 100
  PublicIP:
    Type: Boolean
    Description: 是否分配公网IP
    Label: 分配公网IP
    Default: true
  ECSAmount:
    Type: String
    Label: ECS实例数量
    Default: '3'
  ImageId:
    Type: String
    Description: "镜像ID, 表示要启动一个ECS实例的镜像资源, <a href='#/product/cn-hangzhou/list/imageList' target='_blank'>查看镜像资源</a>"
    Label: ECS镜像ID
    Default: centos_7
  DiskSize:
    Type: String
    Description: '数据盘大小, 单位:GB'
    Label: 数据盘
    Default: '900'
  InstanceNamePrefix:
    Type: String
    Description: 2-128个字符,以大小写英文或中文开头,可包含数字、下划线(_)、点(.)或连字符(-)。
    Label: 实例名称
    MinLength: 2
    MaxLength: 128
    Default: Instance
  HostNamePrefix:
    Type: String
    Description: 2-64个字符,允许使用点(.)分隔成多段,每段允许使用大小写字母、数字或连字符(-),但不能连续使用点(.)或连字符(-),不能以点(.)或连字符(-)开头或结尾。
    Label: 主机名
    MinLength: 2
    MaxLength: 64
    Default: Host
  DiskCategory:
    Type: String
    Description: 数据盘类型
    AllowedValues:
      - cloud
      - cloud_efficiency
      - cloud_ssd
      - ephemeral_ssd
    Label: 数据盘类型
    Default: cloud_efficiency
  InstanceType:
    Type: String
    Description: "ECS实例类型, <a href='#/product/cn-hangzhou/list/typeList' target='_blank'>查看实例类型</a>"
    AllowedValues:
      - ecs.g5.large
      - ecs.c5.large
      - ecs.g5.xlarge
      - ecs.c5.xlarge
    Label: ECS实例类型
    Default: ecs.c5.large
  SystemDiskCategory:
    Type: String
    Description: 系统盘类型
    AllowedValues:
      - cloud
      - cloud_efficiency
      - cloud_ssd
      - ephemeral_ssd
    Label: 系统盘类型
    Default: cloud_ssd
  LoginPassword:
    NoEcho: true
    Type: String
    Description: ECS登录密码
    AllowedPattern: "[a-zA-Z0-9-\\(\\)\\`\\~\\!@\\#\\$%\\^&\\*-+=\\|\\{\\}\\[\\]\\:\\;\\‘\\,\\.\\?\\/]*"
    Label: ECS登录密码
    Confirm: true
    MinLength: 8
    MaxLength: 30
Resources:
  VSwitch:
    Type: 'ALIYUN::ECS::VSwitch'
    Properties:
      VpcId:
        Ref: VPC
      ZoneId:
        Ref: ZoneId
      CidrBlock: 192.168.0.0/24
      VSwitchName: MyVsw
  SG:
    Type: 'ALIYUN::ECS::SecurityGroup'
    Properties:
      VpcId:
        Ref: VPC
      SecurityGroupName: MySG
      SecurityGroupIngress:
        - PortRange: '-1/-1'
          Priority: 1
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: all
          NicType: internet
      SecurityGroupEgress:
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: intranet
  ECS:
    Type: 'ALIYUN::ECS::InstanceGroup'
    Properties:
      IoOptimized: optimized
      PrivateIpAddress: 192.168.0.1
      DiskMappings:
        - Category:
            Ref: DiskCategory
          Size:
            Ref: DiskSize
      ZoneId:
        Ref: ZoneId
      SystemDiskSize:
        Ref: SystemDiskSize
      SecurityGroupId:
        Ref: SG
      VSwitchId:
        Ref: VSwitch
      MaxAmount:
        Ref: ECSAmount
      SystemDiskCategory:
        Ref: SystemDiskCategory
      InstanceName:
        'Fn::Join':
          - ''
          - - Ref: InstanceNamePrefix
            - '[1,3]'
      VpcId:
        Ref: VPC
      MinAmount:
        Ref: ECSAmount
      ImageId:
        Ref: ImageId
      AllocatePublicIP:
        Ref: PublicIP
      InstanceType:
        Ref: InstanceType
      HostName:
        'Fn::Join':
          - ''
          - - Ref: HostNamePrefix
            - '[1,3]'
      Password:
        Ref: LoginPassword
  VPC:
    Type: 'ALIYUN::ECS::VPC'
    Properties:
      CidrBlock: 192.168.0.0/16
      VpcName: MyVPC
Outputs:
  ECS实例ID:
    Value:
      'Fn::GetAtt':
        - ECS
        - InstanceIds
  公网IP:
    Value:
      'Fn::GetAtt':
        - ECS
        - PublicIps
说明 您可以使用本文提供的模板,在任意地域创建资源栈。如果在创建模板时报错,请选择其它可用区或实例类型。