Deploy Basic Instance

Application Scenario

Bare Metal Server (BMS) is a physical server that can be obtained at any time, with high performance and high availability, providing dedicated physical server resources without virtualization overhead, meeting business scenarios with high performance requirements such as high-performance computing, databases, and big data analysis. BMS instances provide complete control over physical servers, support custom operating systems, network configurations, and security policies, and are suitable for application scenarios with strict requirements for performance, security, and compliance. This best practice will introduce how to use Terraform to automatically deploy a basic BMS instance, including the creation of VPC, subnet, security group, and key pair.

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

Data Sources

Resources

Resource/Data Source Dependencies

Operation Steps

1. Script Preparation

Prepare the TF file (e.g., main.tf) in the specified workspace for writing the current best practice script, 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. Refer to the "Preparation Before Deploying Huawei Cloud Resources" document for configuration introduction.

2. Query Availability Zone Information Through Data Source

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

Parameter Description:

  • count: The number of data sources to create, used to control whether to execute the availability zone list query data source, only created when var.availability_zone is empty (i.e., execute the availability zone list query)

3. Create VPC Resource

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a VPC resource:

Parameter Description:

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

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

4. Create VPC Subnet Resource

Add the following script to the TF file (e.g., main.tf) 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 previously created VPC resource (huaweicloud_vpc.test)

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

  • cidr: The subnet CIDR block, assigned by referencing the input variable subnet_cidr, automatically calculated when the value is an empty string

  • gateway_ip: The subnet gateway IP, assigned by referencing the input variable subnet_gateway_ip, automatically calculated when the value is an empty string

5. Create Security Group Resource

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a security group resource:

Parameter Description:

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

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

6. Query BMS Flavor Information Through Data Source

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

Parameter Description:

  • count: The number of data sources to create, used to control whether to execute the BMS flavor query data source, only created when var.instance_flavor_id is empty (i.e., execute the flavor query)

  • cpu_arch: The CPU architecture, set to "aarch64" indicating ARM architecture

  • availability_zone: The availability zone, uses the input variable availability_zone when it is not empty, otherwise assigned based on the return results of the availability zones query data source (data.huaweicloud_availability_zones)

7. Query BMS Image Information Through Data Source

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

Parameter Description:

  • count: The number of data sources to create, used to control whether to execute the image query data source, only created when var.instance_image_id is empty (i.e., execute the image query)

  • os: The operating system type of the image, set to "Huawei Cloud EulerOS" operating system

  • image_type: The image type, set to "Ironic" indicating bare metal server image

8. Create KPS Keypair Resource

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a KPS keypair resource:

Parameter Description:

  • name: The keypair name, assigned by referencing the input variable keypair_name

9. Create BMS Instance Resource

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a BMS instance resource:

Parameter Description:

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

  • user_id: The BMS instance user ID, assigned by referencing the input variable instance_user_id

  • availability_zone: The availability zone, uses the input variable availability_zone when it is not empty, otherwise assigned based on the return results of the availability zones query data source (data.huaweicloud_availability_zones)

  • vpc_id: The VPC ID, referencing the ID of the previously created VPC resource (huaweicloud_vpc.test)

  • flavor_id: The flavor ID, uses the input variable instance_flavor_id when it is not empty, otherwise assigned based on the return results of the BMS flavor query data source (data.huaweicloud_bms_flavors)

  • image_id: The image ID, uses the input variable instance_image_id when it is not empty, otherwise assigned based on the return results of the image query data source (data.huaweicloud_images_images)

  • security_groups: The security group ID list, referencing the ID of the previously created security group resource (huaweicloud_networking_secgroup.test)

  • key_pair: The keypair name, referencing the name of the previously created KPS keypair resource (huaweicloud_kps_keypair.test)

  • enterprise_project_id: The enterprise project ID, assigned by referencing the input variable enterprise_project_id, default value is null

  • tags: The tag key-value pairs, assigned by referencing the input variable instance_tags, default value is an empty object

  • charging_mode: The billing mode, assigned by referencing the input variable charging_mode, default value is "prePaid"

  • period_unit: The billing period unit, assigned by referencing the input variable period_unit, default value is "month"

  • period: The billing period, assigned by referencing the input variable period, default value is 1

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

  • nics.subnet_id: The subnet ID to which the NIC belongs, referencing the ID of the previously created VPC subnet resource (huaweicloud_vpc_subnet.test)

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

In this practice, some resources and data sources use input variables to assign configuration content. 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 during each execution.

Create a terraform.tfvars file in the working directory with the following example content:

Usage:

  1. Save the above content as a terraform.tfvars file in the working directory (this filename allows users to automatically import the content of this tfvars file when executing terraform commands. For other naming, you 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=test-vpc" -var="instance_name=test-instance"

  2. Environment variables: export TF_VAR_vpc_name=test-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.

11. 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 BMS instance

  4. Run terraform show to view the details of the created BMS instance

Note: The creation of the BMS instance takes about 30 minutes, please be patient.

Reference Information

Last updated