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.
Related Resources/Data Sources
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_zoneis 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_idis emptycache_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:
Save the above content as
terraform.tfvarsfile in the working directory (this file name allows users to automatically import the content of thistfvarsfile when executing terraform commands; for other names,.autoneeds to be added before tfvars, such asvariables.auto.tfvars)Modify parameter values as needed
When executing
terraform planorterraform 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:
Command line parameters:
terraform apply -var="vpc_name=my-vpc" -var="instance_name=my-redis"Environment variables:
export TF_VAR_vpc_name=my-vpcCustom 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:
Run
terraform initto initialize the environmentRun
terraform planto view the resource creation planAfter confirming the resource plan is correct, run
terraform applyto start creating the DCS single-node Redis instanceRun
terraform showto view the details of the created DCS single-node Redis instance
Reference Information
Last updated