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

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

创建一台ECS实例

本例使用{"Fn::Select": ["0", {"Fn::GetAZs": {"Ref": "ALIYUN::Region"}}]} 获取当前地域的第一个可用区;使用Parameters以提高模板的灵活性,即可以在创建资源栈时自定义资源类型所需参数,而不是在模板中进行硬编码;参数"AssociationProperty": "ALIYUN::ECS::Instance:ZoneId"用于列出当前地域的所有可用区;使用Ref关联参数或其他资源类型,获取参数的值或其他资源的ID;使用Fn::GetAtt获取资源类型的返回值,通过Outputs输出,您可以进入控制台资源栈概况页面在概览一栏查看输出值。

JSON格式模板

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Description": "创建一台ECS实例",
  "Parameters": {
    "ZoneId": {
      "AssociationProperty": "ALIYUN::ECS::Instance:ZoneId",
      "Type": "String",
      "Description": "可用区是指在同一地域内,电力和网络互相独立的物理区域。在同一专有网络内可用区与可用区之间内网互通,可用区之间能做到故障隔离。",
      "Label": "可用区"
    },
    "PublicIP": {
      "Type": "Boolean",
      "Description": "是否分配公网IP",
      "Label": "分配公网IP",
      "Default": true
    },
    "ImageId": {
      "Type": "String",
      "Description": "镜像ID, 表示要启动一个ECS实例的镜像资源, <a href='#/product/cn-beijing/list/imageList' target='_blank'>查看镜像资源</a>",
      "AllowedValues": [
        "centos_7",
        "ubuntu",
        "win"
      ],
      "Label": "ECS镜像ID",
      "Default": "centos_7"
    },
    "InstanceType": {
      "Type": "String",
      "Description": "ECS实例类型, <a href='#/product/cn-beijing/list/typeList' target='_blank'>查看实例类型</a>",
      "AllowedValues": [
        "ecs.c5.large",
        "ecs.g5.large",
        "ecs.c5.xlarge",
        "ecs.g5.xlarge"
      ],
      "Label": "ECS实例类型",
      "Default": "ecs.c5.large"
    },
    "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"
      }
    },
    "SG": {
      "Type": "ALIYUN::ECS::SecurityGroup",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "SecurityGroupName": "ECSSG",
        "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::Instance",
      "Properties": {
        "IoOptimized": "optimized",
        "PrivateIpAddress": "192.168.0.1",
        "VpcId": {
          "Ref": "VPC"
        },
        "ZoneId": {
          "Ref": "ZoneId"
        },
        "SecurityGroupId": {
          "Ref": "SG"
        },
        "VSwitchId": {
          "Ref": "VSwitch"
        },
        "ImageId": {
          "Ref": "ImageId"
        },
        "AllocatePublicIP": {
          "Ref": "PublicIP"
        },
        "InstanceType": {
          "Ref": "InstanceType"
        },
        "SystemDiskCategory": "cloud_ssd",
        "Password": {
          "Ref": "LoginPassword"
        }
      }
    },
    "VPC": {
      "Type": "ALIYUN::ECS::VPC",
      "Properties": {
        "CidrBlock": "192.168.0.0/16",
        "VpcName": "MyVPC"
      }
    }
  },
  "Outputs": {
    "ECS实例ID": {
      "Value": {
        "Fn::GetAtt": [
          "ECS",
          "InstanceId"
        ]
      }
    },
    "公网IP": {
      "Value": {
        "Fn::GetAtt": [
          "ECS",
          "PublicIp"
        ]
      }
    }
  }
}

YAML格式模板

ROSTemplateFormatVersion: '2015-09-01'
Description: 创建一台ECS实例
Parameters:
  ZoneId:
    AssociationProperty: 'ALIYUN::ECS::Instance:ZoneId'
    Type: String
    Description: 可用区是指在同一地域内,电力和网络互相独立的物理区域。在同一专有网络内可用区与可用区之间内网互通,可用区之间能做到故障隔离。
    Label: 可用区
  PublicIP:
    Type: Boolean
    Description: 是否分配公网IP
    Label: 分配公网IP
    Default: true
  ImageId:
    Type: String
    Description: "镜像ID, 表示要启动一个ECS实例的镜像资源, <a href='#/product/cn-beijing/list/imageList' target='_blank'>查看镜像资源</a>"
    AllowedValues:
      - centos_7
      - ubuntu
      - win
    Label: ECS镜像ID
    Default: centos_7
  InstanceType:
    Type: String
    Description: "ECS实例类型, <a href='#/product/cn-beijing/list/typeList' target='_blank'>查看实例类型</a>"
    AllowedValues:
      - ecs.c5.large
      - ecs.g5.large
      - ecs.c5.xlarge
      - ecs.g5.xlarge
    Label: ECS实例类型
    Default: ecs.c5.large
  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
  SG:
    Type: 'ALIYUN::ECS::SecurityGroup'
    Properties:
      VpcId:
        Ref: VPC
      SecurityGroupName: ECSSG
      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::Instance'
    Properties:
      IoOptimized: optimized
      PrivateIpAddress: 192.168.0.1
      VpcId:
        Ref: VPC
      ZoneId:
        Ref: ZoneId
      SecurityGroupId:
        Ref: SG
      VSwitchId:
        Ref: VSwitch
      ImageId:
        Ref: ImageId
      AllocatePublicIP:
        Ref: PublicIP
      InstanceType:
        Ref: InstanceType
      SystemDiskCategory: cloud_ssd
      Password:
        Ref: LoginPassword
  VPC:
    Type: 'ALIYUN::ECS::VPC'
    Properties:
      CidrBlock: 192.168.0.0/16
      VpcName: MyVPC
Outputs:
  ECS实例ID:
    Value:
      'Fn::GetAtt':
        - ECS
        - InstanceId
  公网IP:
    Value:
      'Fn::GetAtt':
        - ECS
        - PublicIp
说明 您可以使用本文提供的模板,在任意地域创建资源栈。如果在创建模板时报错,请选择其它可用区或实例类型。