Deploy PrePaid Instance

Application Scenario

Elastic Cloud Server (ECS) is a fundamental computing component composed of CPU, memory, operating system, and cloud disks. After an Elastic Cloud Server is successfully created, you can use it in the cloud just like using your own local PC or physical server. Huawei Cloud provides various types of Elastic Cloud Servers to meet different usage scenarios. PrePaid instances are a prepaid billing mode. Compared to PostPaid (pay-as-you-go) instances, PrePaid instances usually have more favorable prices and are suitable for long-term stable workloads. Before creating, you need to confirm the specification type, image type, disk type, and other parameters of the Elastic Cloud Server according to the actual application scenario, and select appropriate network parameters and security group rules. This best practice will introduce how to use Terraform to automatically deploy a PrePaid ECS instance, including VPC, subnet, and security group creation, as well as PrePaid billing mode configuration.

This best practice involves the following main resources and data sources:

Data Sources

Resources

Resource/Data Source Dependencies

Implementation Steps

1. Script Preparation

Prepare the TF file (such as main.tf) for writing the current best practice script in the specified workspace, ensuring that it (or other TF files in the same directory) contains the provider version declaration and Huawei Cloud authentication information required for deploying resources. For configuration introduction, refer to the introduction in Preparation Before Deploying Huawei Cloud Resources.

2. Query Availability Zones Required for ECS Instance Resource Creation Through Data Source

Add the following script to the TF file (such as main.tf) to instruct Terraform to perform a data source query, the query results are used to create ECS instances:

Parameter Description:

  • count: The number of data source queries, used to control whether to query the availability zone list, only when var.availability_zone is empty, query the availability zone list

3. Query Instance Flavors Required for ECS Instance Resource Creation Through Data Source

Add the following script to the TF file to instruct Terraform to query ECS flavors that meet the conditions:

Parameter Description:

  • count: The number of data source queries, used to control whether to query instance flavor information, only when var.instance_flavor_id is empty, query instance flavor information

  • availability_zone: The availability zone where the flavor is located, if availability zone is specified, use that value, otherwise use the first availability zone from the availability zone list query data source

  • performance_type: The performance type of the flavor, assigned by referencing the input variable instance_performance_type, default is "normal" indicating standard type

  • cpu_core_count: CPU core count, assigned by referencing the input variable instance_cpu_core_count, default is 2 cores

  • memory_size: Memory size (GB), assigned by referencing the input variable instance_memory_size, default is 4GB

4. Query Images Required for ECS Instance Resource Creation Through Data Source

Add the following script to the TF file to instruct Terraform to query images that meet the conditions:

Parameter Description:

  • count: The number of data source queries, used to control whether to query image information, only when var.instance_image_id is empty, query image information

  • flavor_id: The flavor ID supported by the image, if instance flavor ID is specified, use that value, otherwise assign based on the return result of the compute flavor list query data source

  • visibility: The visibility of the image, assigned by referencing the input variable instance_image_visibility, default is "public" indicating public image

  • os: The operating system type of the image, assigned by referencing the input variable instance_image_os, default is "Ubuntu" operating system

5. Create VPC Resource

Add the following script to the TF file to instruct Terraform to create a VPC resource:

Parameter Description:

  • name: The name of the VPC, assigned by referencing the input variable vpc_name

  • cidr: The CIDR block of the VPC, assigned by referencing the input variable vpc_cidr, default is "192.168.0.0/16" block

6. Create VPC Subnet Resource

Add the following script to the TF file to instruct Terraform to create a VPC subnet resource:

Parameter Description:

  • vpc_id: The ID of the VPC to which the subnet belongs, referencing the ID of the VPC resource created earlier

  • name: The name of the subnet, assigned by referencing the input variable subnet_name

  • cidr: The CIDR block of the subnet, if subnet CIDR is specified, use that value, otherwise use the cidrsubnet function to divide a subnet segment from the VPC's CIDR block

  • gateway_ip: The gateway IP of the subnet, if gateway IP is specified, use that value, otherwise use the cidrhost function to get the first IP address from the subnet segment as the gateway IP

7. Create Security Group Resource

Add the following script to the TF file to instruct Terraform to create a security group resource:

Parameter Description:

  • count: The number of resource creations, used to control whether to create a security group resource, only when the security group ID list is empty, create a security group resource

  • name: The name of the security group, assigned by referencing the input variable security_group_name

  • delete_default_rules: Whether to delete default rules, set to true to delete default rules

8. Create ECS Instance Resource

Add the following script to the TF file to instruct Terraform to create a PrePaid ECS instance resource:

Parameter Description:

  • name: The name of the ECS instance, assigned by referencing the input variable instance_name

  • image_id: The image ID used by the ECS instance, if image ID is specified, use that value, otherwise assign based on the return result of the image list query data source

  • flavor_id: The flavor ID used by the ECS instance, if instance flavor ID is specified, use that value, otherwise assign based on the return result of the compute flavor list query data source

  • security_group_ids: The list of security group IDs associated with the ECS instance, if security group ID list is specified, use that value, otherwise use the ID of the created security group resource

  • availability_zone: The availability zone where the ECS instance is located, if availability zone is specified, use that value, otherwise use the first availability zone from the availability zone list query data source

  • admin_pass: The administrator password of the ECS instance, assigned by referencing the input variable instance_admin_password

  • description: The description of the ECS instance, assigned by referencing the input variable instance_description

  • system_disk_type: System disk type, assigned by referencing the input variable instance_system_disk_type

  • system_disk_size: System disk size (GB), assigned by referencing the input variable instance_system_disk_size

  • system_disk_iops: System disk IOPS, assigned by referencing the input variable instance_system_disk_iops

  • system_disk_throughput: System disk throughput, assigned by referencing the input variable instance_system_disk_throughput

  • system_disk_dss_pool_id: System disk DSS pool ID, assigned by referencing the input variable instance_system_disk_dss_pool_id

  • metadata: The metadata key/value pairs of the ECS instance, assigned by referencing the input variable instance_metadata

  • tags: The tag key/value pairs associated with the ECS instance, assigned by referencing the input variable instance_tags

  • enterprise_project_id: The enterprise project ID to which the ECS instance belongs, assigned by referencing the input variable enterprise_project_id

  • eip_id: The EIP ID associated with the ECS instance, assigned by referencing the input variable instance_eip_id

  • eip_type: The EIP type to create and associate with the ECS instance, assigned by referencing the input variable instance_eip_type

  • bandwidth: Bandwidth configuration block (dynamic block), dynamically created based on the input variable instance_bandwidth

    • share_type: Bandwidth sharing type, assigned by referencing the bandwidth configuration in the input variable

    • id: Bandwidth ID, assigned by referencing the bandwidth configuration in the input variable

    • size: Bandwidth size, assigned by referencing the bandwidth configuration in the input variable

    • charge_mode: Bandwidth billing mode, assigned by referencing the bandwidth configuration in the input variable

    • extend_param: Bandwidth extension parameters, assigned by referencing the bandwidth configuration in the input variable

  • network: Network configuration block, specifying the network to which the ECS instance connects

    • uuid: The unique identifier of the network, using the ID of the subnet resource created earlier

  • charging_mode: Billing mode, set to "prePaid" indicating PrePaid (prepaid) mode

  • period_unit: Billing period unit, assigned by referencing the input variable period_unit, default is "month" indicating monthly billing

  • period: Billing period, assigned by referencing the input variable period, default is 1 indicating one billing period

  • auto_renew: Whether to enable auto-renewal, assigned by referencing the input variable auto_renew, default is "false" indicating no auto-renewal

Note: PrePaid instances use a prepaid billing mode and require advance payment. The billing period unit can be "month" or "year", and the billing period can be 1-9 (months) or 1-3 (years). After enabling auto-renewal, the instance will automatically renew before expiration to avoid service interruption due to non-renewal.

9. Preset Input Parameters Required for Resource Deployment (Optional)

In this practice, some resources and data sources use input variables to assign configuration content, and these input parameters need to be manually entered during subsequent deployment. At the same time, Terraform provides a method to preset these configurations through tfvars files, which can avoid repeated input each time it is executed.

Create a terraform.tfvars file in the working directory, with example content as follows:

Usage:

  1. Save the above content as a terraform.tfvars file in the working directory (this filename allows users to automatically import the content in this tfvars file when executing terraform commands, other names need to add .auto before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values according to actual needs

  3. When executing terraform plan or terraform apply, Terraform will automatically read the variable values in this file

In addition to using the terraform.tfvars file, you can also set variable values in the following ways:

  1. Command line parameters: terraform apply -var="vpc_name=my-vpc" -var="instance_name=my-instance"

  2. Environment variables: export TF_VAR_vpc_name=my-vpc

  3. Custom named variable file: terraform apply -var-file="custom.tfvars"

Note: If the same variable is set through multiple methods, Terraform will use variable values according to the following priority: command line parameters > variable file > environment variables > default values.

10. Initialize and Apply Terraform Configuration

After completing the above script configuration, execute the following steps to create resources:

  1. Run terraform init to initialize the environment

  2. Run terraform plan to view the resource creation plan

  3. After confirming that the resource plan is correct, run terraform apply to start creating the PrePaid ECS instance

  4. Run terraform show to view the created PrePaid ECS instance details

Reference Information

Last updated