Updated on 2023-08-11 GMT+08:00

Writing a Template to Create an ECS

This section describes how to create an Elastic Cloud Server (ECS), including a Virtual Private Cloud (VPC) and subnet by writing a template. An ECS is a computing server equipped with CPUs, memory, images, and Elastic Volume Service (EVS) disks. ECSs can be created on demand and supports auto scaling. A VPC provides logically isolated, configurable, and manageable virtual networks for your ECSs. One or more subnets are automatically created when you create a VPC.

At the end of this walkthrough, you will see the newly created ECS on the Cloud Server Console, as shown in Figure 1.
Figure 1 Created ECS

In addition to writing templates from scratch, you can also select public templates to create templates. For more information about templates, see Templates (Cloud-Based Automation Scripts).

In this section, you will complete the following steps:

  1. Step 1: Write a Template: Use the YAML language to write a template for creating an ECS, VPC, and subnet.
  2. Step 2: Create an ECS: Use the template to create an ECS, VPC, and subnet.
  3. Step 3: Delete Unnecessary Resources: Delete unnecessary stack to avoid unwanted charges.

Step 1: Write a Template

  1. Write a simple template to create a VPC.

    tosca_definitions_version: huaweicloud_tosca_version_1_0   #Template version information
    node_templates:                 #Element object definition
      myvpc:                        #VPC
        type: HuaweiCloud.VPC.VPC   
        properties:                 
          name: my-vpc              #Name of the VPC
          cidr: '192.168.0.0/16'    #VPC CIDR

    This template includes:

    1. tosca_definitions_version: specifies the version of a template. Currently, only huaweicloud_tosca_version_1_0 is supported by AOS.
    2. node_templates: defines the set of objects to be orchestrated in a template. In AOS, objects are used interchangeably with elements. An object can be an application or cloud service resource. In the preceding template, node_templates defines the myvpc VPC.
    3. type: specifies the type of an orchestration object. The value comes from the element type list and can be set to Cloud.*** (*** indicates the element name in the Resource Indexes). In the preceding template, the myvpc VPC type is HuaweiCloud.VPC.VPC.
    4. properties: defines element properties, which vary with element types. In the preceding template, the myvpc VPC has the names and cidr properties, which indicate the name and network segment of the VPC, respectively. For more information, see VPC.VPC.

  2. Define a subnet in the VPC. A VPC is a large network segment and is usually divided into several subnets. Define a subnet in the created VPC based on the preceding template.

    tosca_definitions_version: huaweicloud_tosca_version_1_0   #Template version information
    node_templates:                 #Element object definition
      myvpc:                        #VPC
        type: HuaweiCloud.VPC.VPC   
        properties:                 
          name: my-vpc              #Name of the VPC
          cidr: '192.168.0.0/16'    #VPC CIDR
      mysubnet:                     #Subnet
        type: HuaweiCloud.VPC.Subnet
        properties:
          name: my-subnet           #Name of the subnet
          cidr: '192.168.1.0/24'    #Subnet CIDR
          gateway: 192.168.1.1      #Gateway of the subnet
          vpcId:                    #ID of the VPC to which the subnet belongs
            get_reference: myvpc
          dhcpEnable: true          #Determines whether to enable the DHCP function for the subnet in the VPC.
        requirements:               #Dependency between the subnet and VPC.
          - vpcId:
              node: myvpc

    The requirements parameter specifies the element that has a dependency relationship with the current element. For example, define myvpc as a dependent node in the requirements of the subnet because a subnet depends on a VPC.

  3. Define an ECS in the template.

    tosca_definitions_version: huaweicloud_tosca_version_1_0   #Template version information
    node_templates:                 #Element object definition
      myvpc:                        #VPC
        type: HuaweiCloud.VPC.VPC   
        properties:                 
          name: my-vpc              #Name of the VPC
          cidr: '192.168.0.0/16'    #VPC CIDR
      mysubnet:                     #Subnet
        type: HuaweiCloud.VPC.Subnet
        properties:
          name: my-subnet           #Name of the subnet
          cidr: '192.168.1.0/24'    #Subnet CIDR
          gateway: 192.168.1.1      #Gateway of the subnet
          vpcId:                    #ID of the VPC to which the subnet belongs
            get_reference: myvpc
          dhcpEnable: true          #Determines whether to enable the DHCP function for the subnet in the VPC.
        requirements:               #Dependency between the subnet and VPC.
          - vpcId:
              node: myvpc
      myecs:                        #ECS
        type: HuaweiCloud.ECS.CloudServer    
        properties:       
          name: my-ecs              #Name of the ECS
          instances: 1              #Number of created ECSs
          imageId: 7be2e72e-0679-4a1b-8faf-0c1865708b20    #Image ID used by the ECS. In this template, the image ID is the ID of the system disk based on 64-bit CentOS 7.4.
          flavor: c2.large         #Specifications of the ECS
          vpcId:                    #ID of the VPC to which the ECS belongs. Either a new or an existing VPC ID can be used.
            get_reference: myvpc    #Obtains the dynamic attribute value of the associated element.
          availabilityZone: cn-south-1a      #AZ to which the ECS belongs. This template uses an AZ in the CN South-Guangzhou region.
          nics:                              #NIC of the ECS
            - subnetId: 
                get_reference: mysubnet      
          rootVolume:              #System disk configuration of the ECS
            volumeType: SATA       #Common I/O disk type
            size: 40               #System disk size (unit: GB)
        requirements:              #Dependency among the ECS, VPC, and subnet.
          - vpcId:         
              node: myvpc      
          - nics.subnetId:          
              node: mysubnet

  4. Save the template as a local file myecs.yaml.
  5. Log in to the AOS console.

    In this template, the image ID is the ID of the CentOS 6.3 64bit image, and the AZ is cn-south-1a in the CN South-Guangzhou region. Therefore, select the CN South-Guangzhou region after logging in to the console to avoid stack creation failure.

  6. In the navigation pane, choose My Templates, and then click Create Template.
  7. On the Upload Local Template tab page, specify the following parameters, upload a local YAML file, and then click Create. The template details page is then displayed, showing the template information.

    • Template: Enter a template name. Each template name must be globally unique. For example, set this parameter to myecs.
    • Version: Set this parameter to 1.0.
    • Select File: Upload the myecs.yaml file.

Step 2: Create an ECS

  1. Log in to the AOS console.
  2. In the navigation pane, choose My Templates. The myecs template is displayed in the template list.
  3. Click Create Stack in the Operation column of the myecs template.
  4. Set the stack information.

    • Stack Name: Enter a unique stack name, for example, aos-ecs.
    • Description: The description can be left blank.

  5. Click Next and check the stack information. If the stack information is correct, click Create Stack.

    The stack details page is displayed, showing that the stack is being created. The stack includes a VPC, a subnet, and an ECS. It will take about 6 minutes to create the stack.

  6. Wait until the stack status becomes Normal. The VPC, subnet, and ECS are created and displayed in the stack element list.

    Figure 2 Created stack

  7. View the created cloud services.

    1. Log in to the management console.
    2. Choose Service List > Computing > Elastic Cloud Server. You will see the newly created ECS on the ECS list.
      Figure 3 Created ECS
    3. Choose Service List > Network > Virtual Private Cloud. You will see the newly created VPC on the VPC list.
      Figure 4 Created VPC
    4. Click the VPC name to show more details about the VPC. On the VPC details page, you will see that the subnet has been created in the VPC.
      Figure 5 Created subnet

Step 3: Delete Unnecessary Resources

Delete unnecessary stack resources to avoid unwanted charges.

  1. Log in to the AOS console.
  2. In the navigation pane, click My Stacks.
  3. Select the stack that will no longer be used, and click Delete to delete the stack.