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.
Related Resources/Data Sources
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_zoneis 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_idis empty, query instance flavor informationavailability_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 typecpu_core_count: CPU core count, assigned by referencing the input variable
instance_cpu_core_count, default is 2 coresmemory_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_idis empty, query image informationflavor_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 imageos: 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_namecidr: 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_namecidr: 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_namedelete_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_nameimage_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_passworddescription: The description of the ECS instance, assigned by referencing the input variable
instance_descriptionsystem_disk_type: System disk type, assigned by referencing the input variable
instance_system_disk_typesystem_disk_size: System disk size (GB), assigned by referencing the input variable
instance_system_disk_sizesystem_disk_iops: System disk IOPS, assigned by referencing the input variable
instance_system_disk_iopssystem_disk_throughput: System disk throughput, assigned by referencing the input variable
instance_system_disk_throughputsystem_disk_dss_pool_id: System disk DSS pool ID, assigned by referencing the input variable
instance_system_disk_dss_pool_idmetadata: The metadata key/value pairs of the ECS instance, assigned by referencing the input variable
instance_metadatatags: The tag key/value pairs associated with the ECS instance, assigned by referencing the input variable
instance_tagsenterprise_project_id: The enterprise project ID to which the ECS instance belongs, assigned by referencing the input variable
enterprise_project_ideip_id: The EIP ID associated with the ECS instance, assigned by referencing the input variable
instance_eip_ideip_type: The EIP type to create and associate with the ECS instance, assigned by referencing the input variable
instance_eip_typebandwidth: Bandwidth configuration block (dynamic block), dynamically created based on the input variable
instance_bandwidthshare_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 billingperiod: Billing period, assigned by referencing the input variable
period, default is 1 indicating one billing periodauto_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:
Save the above content as a
terraform.tfvarsfile in the working directory (this filename allows users to automatically import the content in thistfvarsfile when executing terraform commands, other names need to add.autobefore tfvars, such asvariables.auto.tfvars)Modify parameter values according to actual needs
When executing
terraform planorterraform 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:
Command line parameters:
terraform apply -var="vpc_name=my-vpc" -var="instance_name=my-instance"Environment variables:
export TF_VAR_vpc_name=my-vpcCustom 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:
Run
terraform initto initialize the environmentRun
terraform planto view the resource creation planAfter confirming that the resource plan is correct, run
terraform applyto start creating the PrePaid ECS instanceRun
terraform showto view the created PrePaid ECS instance details
Reference Information
Last updated