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

前提条件

创建ECS实例前,首先需要创建虚拟专有网络(VPC)和交换机(VSwitch)并加入安全组(SecurityGroup)。本文为您介绍如何通过创建ROS模板生成上述资源,再通过ROS控制台创建资源栈,详情请参见创建资源栈

创建多台ECS实例的注意事项,请参见ALIYUN::ECS::InstanceGroup

创建多台ECS实例

JSON格式

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Description": "创建多台ECS实例",
  "Metadata": {
    "ALIYUN::ROS::Interface": {
      "ParameterGroups": [
        {
          "Parameters": [
            "ImageId",
            "InstanceType",
            "LoginPassword",
            "ECSAmount",
            "DiskSize",
            "DiskCategory",
            "SystemDiskSize",
            "SystemDiskCategory"
          ],
          "Label": {
            "default": "ECS"
          }
        }
      ],
      "TemplateTags": [
        "ECS"
      ]
    }
  },
  "Parameters": {
    "SystemDiskSize": {
      "Type": "Number",
      "Description": "系统盘大小,40-500,单位:GB",
      "Label": "系统盘",
      "Default": 40
    },
    "PublicIP": {
      "Type": "Boolean",
      "Description": "是否分配公网IP",
      "Label": "分配公网IP",
      "Default": true
    },
    "ECSAmount": {
      "Type": "Number",
      "Label": "ECS实例数量",
      "Description": "取值范围:1-100",
      "Default": 3,
      "MaxValue": 100,
      "MinValue": 1
    },
    "ImageId": {
      "Type": "String",
      "Description": "镜像ID,表示要启动一个ECS实例的镜像资源",
      "Label": "ECS镜像ID",
      "Default": "centos_7"
    },
    "DiskSize": {
      "Type": "String",
      "Description": "数据盘大小,单位:GB",
      "Label": "数据盘",
      "Default": "20"
    },
    "DiskCategory": {
      "Type": "String",
      "Description": "数据盘类型",
      "AllowedValues": [
        "cloud_efficiency",
        "cloud_ssd"
      ],
      "Label": "数据盘类型",
      "Default": "cloud_ssd"
    },
    "InstanceType": {
      "Type": "String",
      "Description": "ECS实例类型",
      "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_efficiency",
        "cloud_ssd"
      ],
      "Label": "系统盘类型",
      "Default": "cloud_ssd"
    },
    "LoginPassword": {
      "NoEcho": true,
      "Type": "String",
      "Description": "ECS登录密码",
      "AllowedPattern": "[a-zA-Z0-9-\\(\\)\\`\\~\\!@\\#\\$%\\^&\\*-+=\\|\\{\\}\\[\\]\\:\\;\\‘\\,\\.\\?\\/]*",
      "Label": "ECS登录密码",
      "MinLength": 8,
      "MaxLength": 30
    }
  },
  "Resources": {
    "VSwitch": {
      "Type": "ALIYUN::ECS::VSwitch",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "ZoneId": {
          "Fn::Select": [
            "1",
            {
              "Fn::GetAZs": {
                "Ref": "ALIYUN::Region"
              }
            }
          ]
        },
        "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",
        "DiskMappings": [
          {
            "Category": {
              "Ref": "DiskCategory"
            },
            "Size": {
              "Ref": "DiskSize"
            }
          }
        ],
        "SystemDiskSize": {
          "Ref": "SystemDiskSize"
        },
        "SecurityGroupId": {
          "Ref": "SG"
        },
        "VSwitchId": {
          "Ref": "VSwitch"
        },
        "MaxAmount": {
          "Ref": "ECSAmount"
        },
        "SystemDiskCategory": {
          "Ref": "SystemDiskCategory"
        },
        "VpcId": {
          "Ref": "VPC"
        },
        "MinAmount": {
          "Ref": "ECSAmount"
        },
        "ImageId": {
          "Ref": "ImageId"
        },
        "AllocatePublicIP": {
          "Ref": "PublicIP"
        },
        "InstanceType": {
          "Ref": "InstanceType"
        },
        "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
      - LoginPassword
      - ECSAmount
      - DiskSize
      - DiskCategory
      - SystemDiskSize
      - SystemDiskCategory
      Label:
        default: ECS
    TemplateTags:
    - ECS
Parameters:
  SystemDiskSize:
    Type: Number
    Description: 系统盘大小,40-500,单位:GB
    Label: 系统盘
    Default: 40
  PublicIP:
    Type: Boolean
    Description: 是否分配公网IP
    Label: 分配公网IP
    Default: true
  ECSAmount:
    Type: Number
    Label: ECS实例数量
    Description: 取值范围:1-100
    Default: 3
    MaxValue: 100
    MinValue: 1
  ImageId:
    Type: String
    Description: 镜像ID,表示要启动一个ECS实例的镜像资源
    Label: ECS镜像ID
    Default: centos_7
  DiskSize:
    Type: String
    Description: 数据盘大小,单位:GB
    Label: 数据盘
    Default: '20'
  DiskCategory:
    Type: String
    Description: 数据盘类型
    AllowedValues:
    - cloud_efficiency
    - cloud_ssd
    Label: 数据盘类型
    Default: cloud_ssd
  InstanceType:
    Type: String
    Description: ECS实例类型
    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_efficiency
    - cloud_ssd
    Label: 系统盘类型
    Default: cloud_ssd
  LoginPassword:
    NoEcho: true
    Type: String
    Description: ECS登录密码
    AllowedPattern: "[a-zA-Z0-9-\\(\\)\\`\\~\\!@\\#\\$%\\^&\\*-+=\\|\\{\\}\\[\\]\\:\\;\\‘\\,\\.\\?\\/]*"
    Label: ECS登录密码
    MinLength: 8
    MaxLength: 30
Resources:
  VSwitch:
    Type: ALIYUN::ECS::VSwitch
    Properties:
      VpcId:
        Ref: VPC
      ZoneId:
        Fn::Select:
        - '1'
        - Fn::GetAZs:
            Ref: ALIYUN::Region
      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
      DiskMappings:
      - Category:
          Ref: DiskCategory
        Size:
          Ref: DiskSize
      SystemDiskSize:
        Ref: SystemDiskSize
      SecurityGroupId:
        Ref: SG
      VSwitchId:
        Ref: VSwitch
      MaxAmount:
        Ref: ECSAmount
      SystemDiskCategory:
        Ref: SystemDiskCategory
      VpcId:
        Ref: VPC
      MinAmount:
        Ref: ECSAmount
      ImageId:
        Ref: ImageId
      AllocatePublicIP:
        Ref: PublicIP
      InstanceType:
        Ref: InstanceType
      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
说明 您可以使用本文提供的模板,在任意地域创建资源栈。如果在创建模板时报错,请选择其它可用区或实例类型。