Deploy Kafka Instance Configuration
Application Scenario
Huawei Cloud Distributed Message Service Kafka is a highly available, highly reliable, and high-performance distributed message middleware service, widely used in big data, log collection, stream processing and other scenarios. By configuring Kafka instances, you can create and manage Kafka clusters, including instance specifications, storage configuration, network configuration, security configuration, etc., achieving reliable message transmission and processing. Automating Kafka instance configuration through Terraform can ensure standardized and consistent instance configuration, improving operational efficiency. This best practice will introduce how to use Terraform to automatically configure Kafka instances.
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. Query Data Sources
Add the following script to the TF file (e.g., main.tf) to query availability zone and Kafka flavor information:
Parameter Description:
type: Flavor type, assigned by referencing the input variable instance_flavor_type, default value is "cluster" (cluster mode)
availability_zones: Availability zone list, assigned by referencing the input variable availability_zones or availability zones data source
storage_spec_code: Storage specification code, assigned by referencing the input variable instance_storage_spec_code, default value is "dms.physical.storage.ultra.v2"
3. Create Basic Network Resources
Add the following script to the TF file (e.g., main.tf) to create VPC, subnet and security group:
4. Create Kafka Instance Resource
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a Kafka instance resource:
Parameter Description:
name: Kafka instance name, assigned by referencing the input variable instance_name
availability_zones: Availability zone list, assigned by referencing the input variable availability_zones or availability zones data source
engine_version: Engine version, assigned by referencing the input variable instance_engine_version, default value is "2.7"
flavor_id: Flavor ID, assigned by referencing the input variable instance_flavor_id or Kafka flavors data source
storage_spec_code: Storage specification code, assigned by referencing the input variable instance_storage_spec_code, default value is "dms.physical.storage.ultra.v2"
storage_space: Storage space, assigned by referencing the input variable instance_storage_space, default value is 600 (GB)
broker_num: Number of brokers, assigned by referencing the input variable instance_broker_num, default value is 3
vpc_id: VPC ID, assigned by referencing the VPC resource
network_id: Network subnet ID, assigned by referencing the subnet resource
security_group_id: Security group ID, assigned by referencing the security group resource
ssl_enable: Whether to enable SSL, assigned by referencing the input variable instance_ssl_enable, default value is false
access_user: Access user name, assigned by referencing the input variable instance_access_user_name, optional parameter, default value is empty string
password: Access password, assigned by referencing the input variable instance_access_user_password, optional parameter, default value is empty string
description: Instance description, assigned by referencing the input variable instance_description, optional parameter, default value is empty string
charging_mode: Charging mode, assigned by referencing the input variable charging_mode, default value is "postPaid" (on-demand)
period_unit: Billing period unit, assigned by referencing the input variable period_unit, optional parameter, default value is null
period: Billing period, assigned by referencing the input variable period, optional parameter, default value is null
auto_renew: Whether to enable auto renew, assigned by referencing the input variable auto_renew, default value is "false"
5. Preset Input Parameters Required for Resource Deployment (Optional)
In this practice, some resources 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:
Save the above content as a
terraform.tfvarsfile in the working directory (this filename allows users to automatically import the content of thistfvarsfile when executing terraform commands. For other naming, you need to add.autobefore tfvars, such asvariables.auto.tfvars)Modify parameter values according to actual needs, especially
instance_access_user_passwordneeds to be set to a password that meets password complexity requirementsWhen 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="instance_name=my_kafka" -var="vpc_name=my_vpc"Environment variables:
export TF_VAR_instance_name=my_kafkaandexport 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. Since instance_access_user_password contains sensitive information, it is recommended to use environment variables or encrypted variable files for setting.
6. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create a Kafka instance:
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 Kafka instance and related resourcesRun
terraform showto view the details of the created Kafka instance
Note: After the Kafka instance is created, security can be enhanced by configuring SSL and access users. The instance's availability zones and flavor ID cannot be modified after creation, so they need to be configured correctly during creation. Through lifecycle.ignore_changes, Terraform can be prevented from modifying these immutable parameters in subsequent updates.
Reference Information
Last updated