Deploy Server Type Vault

Application Scenario

Cloud Backup and Recovery (CBR) is a data protection service provided by Huawei Cloud, offering simple and easy-to-use backup services for both cloud and on-premises resources. When events such as virus intrusion, accidental deletion, or hardware/software failures occur, data can be restored to any backup point. Server type vault is a type of vault in CBR service, specifically designed for backing up Elastic Cloud Server (ECS) instances.

Server type vault supports complete backup of ECS instances, including system disks and data disks, ensuring that the entire server environment can be quickly restored when failures occur. This best practice will introduce how to use Terraform to automatically deploy a CBR server type vault, including creating ECS instances, configuring backup policies, and creating vaults.

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 Zones Required for ECS Instance Resource Creation via Data Source

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to perform a data source query, the result of which will be used to create the ECS instance:

Parameter Description:

  • No special parameters, gets all availability zone information in the current region

3. Query ECS Instance Flavor Information via Data Source

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to perform a data source query, the result of which will be used to create the ECS instance:

Parameter Description:

  • count: Number of data source instances, used to control whether to execute the ECS flavor list query data source, only creates the data source when var.instance_flavor_id is empty

  • availability_zone: Availability zone, prioritizes input variable, uses the first result from availability zone list query data source if empty

  • performance_type: Performance type, used to filter ECS flavors

  • cpu_core_count: CPU core count, used to filter ECS flavors

  • memory_size: Memory size, used to filter ECS flavors

4. Query ECS Instance Image Information via Data Source

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to perform a data source query, the result of which will be used to create the ECS instance:

Parameter Description:

  • count: Number of data source instances, used to control whether to execute the image list query data source, only creates the data source when var.instance_image_id is empty

  • flavor_id: Flavor ID, prioritizes input variable, uses the first result from ECS flavor list query data source if empty

  • os: Operating system type, used to filter images

  • visibility: Visibility, used to filter images

5. Create VPC

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

Parameter Description:

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

  • cidr: VPC CIDR block, assigned by referencing the input variable vpc_cidr

6. Create VPC Subnet

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

Parameter Description:

  • vpc_id: VPC ID, assigned by referencing the VPC resource (huaweicloud_vpc.test) ID

  • name: Subnet name, assigned by referencing the input variable subnet_name

  • cidr: Subnet CIDR block, prioritizes input variable, calculates using cidrsubnet function if empty

  • gateway_ip: Gateway IP, prioritizes input variable, calculates using cidrhost function if empty

  • availability_zone: Availability zone, prioritizes input variable, uses the first result from availability zone list query data source if empty

7. Create Security Group

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

Parameter Description:

  • name: Security group name, assigned by referencing the input variable secgroup_name

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

8. Create ECS Instance

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

Parameter Description:

  • name: ECS instance name, assigned by referencing the input variable ecs_instance_name

  • availability_zone: Availability zone, prioritizes input variable, uses the first result from availability zone list query data source if empty

  • flavor_id: Flavor ID, prioritizes input variable, uses the first result from ECS flavor list query data source if empty

  • image_id: Image ID, prioritizes input variable, uses the first result from image list query data source if empty

  • security_groups: Security group list, assigned by referencing the security group resource (huaweicloud_networking_secgroup.test) name

  • key_pair: Key pair name, assigned by referencing the input variable key_pair_name

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

  • system_disk_size: System disk size, assigned by referencing the input variable system_disk_size

  • network.uuid: Network ID, assigned by referencing the VPC subnet resource (huaweicloud_vpc_subnet.test) ID

9. Create CBR Backup Policy (Optional)

Add the following script to the TF file to instruct Terraform to create a CBR backup policy resource:

Parameter Description:

  • count: Number of resource instances, used to control whether to create the backup policy resource, only creates when var.enable_policy is true

  • name: Backup policy name, assigned by referencing the input variable vault_name and fixed suffix

  • type: Policy type, set to "backup" for backup policy

  • time_period: Backup retention time (days), set to 20 days

  • time_zone: Time zone, set to "UTC+08:00"

  • enabled: Whether to enable policy, set to true

  • backup_cycle.days: Backup cycle, set to Monday and Tuesday

  • backup_cycle.execution_times: Execution time, set to 06:00

10. Create CBR Vault

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

Parameter Description:

  • name: Vault name, assigned by referencing the input variable vault_name

  • type: Vault type, set to "server" for server type vault

  • protection_type: Protection type, assigned by referencing the input variable protection_type

  • consistent_level: Consistency level, assigned by referencing the input variable consistent_level

  • size: Vault size, assigned by referencing the input variable vault_size

  • auto_bind: Whether to auto-bind, assigned by referencing the input variable auto_bind

  • auto_expand: Whether to auto-expand, assigned by referencing the input variable auto_expand

  • enterprise_project_id: Enterprise project ID, assigned by referencing the input variable enterprise_project_id

  • backup_name_prefix: Backup name prefix, assigned by referencing the input variable backup_name_prefix

  • is_multi_az: Whether to deploy across AZs, assigned by referencing the input variable is_multi_az

  • resources.server_id: Server ID, assigned by referencing the ECS instance resource (huaweicloud_compute_instance.test) ID

  • resources.excludes: Excluded volumes, determined by exclude_volumes variable whether to exclude system disk

  • policy.id: Policy ID, assigned by referencing the backup policy resource (huaweicloud_cbr_policy.test) ID when policy is enabled

  • tags: Tags, assigned by referencing the input variable tags

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

In this practice, some resources and data sources use input variables to assign values to configuration content. These input parameters need to be manually entered during subsequent deployments. 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 terraform.tfvars file in the working directory (this file name allows users to automatically import the content of this tfvars file when executing terraform commands; for other names, .auto needs to be added before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values as needed

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

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

  1. Command line parameters: terraform apply -var="vpc_name=my-vpc" -var="subnet_name=my-subnet"

  2. Environment variables: export TF_VAR_vpc_name=my-vpc

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

Note: If the same variable is set in multiple ways, Terraform will use the variable value according to the following priority: command line parameters > variable files > environment variables > default values.

12. 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 the resource plan is correct, run terraform apply to start creating the CBR server type vault

  4. Run terraform show to view the details of the created CBR server type vault

Reference Information

Last updated