Updated on 2023-12-21 GMT+08:00

Creating an ECS

Application Scenario

An Elastic Cloud Server (ECS) is a basic computing unit that consists of vCPUs, memory, OS, and Elastic Volume Service (EVS) disks. After creating an ECS, you can use it like using your local computer or physical server. HUAWEI CLOUD provides a variety of ECS types for different scenario requirements. When creating an ECS, select specifications, image type, and disk type and configure network parameters and security group rules based on your scenario requirements.

Related Resources

huaweicloud_compute_instance

Procedure

  1. Use data source to query the AZ, ECS specifications, image, and network parameters.

    Create the main.tf file, enter the following information, and save the file:
    data "huaweicloud_availability_zones" "myaz" {}
    
    data "huaweicloud_compute_flavors" "myflavor" {
      availability_zone = data.huaweicloud_availability_zones.myaz.names[0]
      performance_type  = "normal"
      cpu_core_count    = 2
      memory_size       = 4
    }
    
    data "huaweicloud_images_image" "myimage" {
      name        = "Ubuntu 18.04 server 64bit"
      most_recent = true
    }
    
    data "huaweicloud_vpc_subnet" "mynet" {
      name = "subnet-default"
    }
    
    data "huaweicloud_networking_secgroup" "mysecgroup" {
      name = "default"
    }

  2. Create an ECS that supports login with a random password.

    1. Add the following information to the main.tf file:
      resource "random_password" "password" {
        length           = 16
        special          = true
        override_special = "!@#$%*"
      }
      
      resource "huaweicloud_compute_instance" "myinstance" {
        name               = "basic"
        admin_pass         = random_password.password.result
        image_id           = data.huaweicloud_images_image.myimage.id
        flavor_id          = data.huaweicloud_compute_flavors.myflavor.ids[0]
        availability_zone  = data.huaweicloud_availability_zones.myaz.names[0]
        security_group_ids = [data.huaweicloud_networking_secgroup.mysecgroup.id]
      
        network {
          uuid = data.huaweicloud_vpc_subnet.mynet.id
        }
      }
    2. Run terraform init to initialize the environment.
    3. Run terraform plan to view resources.
    4. After you confirm that the resource information is correct, run terraform apply to start ECS creation.
    5. Run terraform show to view the created ECS.