Deploy Single-Node Redis Instance

Application Scenario

Distributed Cache Service (DCS) is a high-performance, highly available in-memory database service provided by Huawei Cloud, supporting mainstream cache engines such as Redis and Memcached. DCS service provides multiple instance specifications and deployment modes, including single-node, master-standby, and cluster, meeting cache requirements for different scales and scenarios.

Single-node Redis instance is the basic deployment mode in DCS service, suitable for development and testing environments, small-scale applications, or scenarios with low high availability requirements. Single-node instances have the characteristics of low cost, simple deployment, and easy maintenance, making them an ideal choice for learning and using Redis. This best practice will introduce how to use Terraform to automatically deploy DCS single-node Redis instances, including VPC creation, instance configuration, and basic network setup.

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. 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

3. Create VPC Subnet

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: 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 using input variable, if empty then calculated using cidrsubnet function

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

4. 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 DCS instances:

Parameter Description:

  • count: Data source creation count, used to control whether to execute the availability zones list query data source, only creates data source when var.availability_zone is empty

5. Query DCS 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 DCS instances:

Parameter Description:

  • count: Data source creation count, used to control whether to execute the DCS flavors list query data source, only creates data source when var.instance_flavor_id is empty

  • cache_mode: Cache mode, set to "single" for single-node mode

  • capacity: Capacity, assigned by referencing the input variable instance_capacity

  • engine_version: Engine version, assigned by referencing the input variable instance_engine_version

6. Create DCS Single-Node Redis Instance

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

Parameter Description:

  • name: Instance name, assigned by referencing the input variable instance_name

  • engine: Engine type, set to "Redis"

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

  • engine_version: Engine version, assigned by referencing the input variable instance_engine_version

  • capacity: Capacity, assigned by referencing the input variable instance_capacity

  • flavor: Flavor, prioritizes using input variable, if empty then uses the first result from DCS flavors list query data source

  • availability_zones: Availability zones list, prioritizes using input variable, if empty then uses the first result from availability zones list query data source

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

  • subnet_id: Subnet ID, assigned by referencing the VPC subnet resource (huaweicloud_vpc_subnet.test) ID

  • password: Password, assigned by referencing the input variable instance_password

  • charging_mode: Charging mode, assigned by referencing the input variable charging_mode

  • period_unit: Charging period unit, assigned by referencing the input variable period_unit

  • period: Charging period, assigned by referencing the input variable period

  • auto_renew: Auto-renewal, assigned by referencing the input variable auto_renew

  • lifecycle.ignore_changes: Lifecycle ignore changes, ignores changes to flavor and availability_zones

7. 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="instance_name=my-redis"

  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.

8. 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 DCS single-node Redis instance

  4. Run terraform show to view the details of the created DCS single-node Redis instance

Reference Information

Last updated