Deploy Kafka Public Access Instance Network
Application Scenario
Huawei Cloud Distributed Message Service Kafka supports public network access to instances, suitable for scenarios that require accessing Kafka services in a public network environment, such as cross-region access, development and testing environments, etc. By configuring public network access, you can bind Elastic IP (EIP) to Kafka instances and configure corresponding security group rules and port protocols to achieve secure public network access. This best practice will introduce how to use Terraform to automatically deploy Kafka instance network configuration that supports public network access, including VPC, subnet, security group, EIP, and Kafka instance public access configuration.
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 Security Group Rules
Add the following script to the TF file (e.g., main.tf) to create security group rules that allow public network access to Kafka instance ports:
Parameter Description:
security_group_id: Security group ID, assigned by referencing the security group resource
direction: Rule direction, set to "ingress" (inbound)
ethertype: IP protocol type, set to "IPv4"
protocol: Protocol type, set to "tcp"
ports: Port range, assigned by referencing the input variable security_group_rule_ports, default value is "9094,9095" (Kafka public network access ports)
remote_ip_prefix: Remote IP address segment, assigned by referencing the input variable security_group_rule_remote_ip_prefix, used to limit the IP range allowed to access
5. Create Elastic IP
Add the following script to the TF file (e.g., main.tf) to create Elastic IP (EIP) for binding to Kafka instances:
Parameter Description:
count: Creation count, assigned by referencing the input variable instance_broker_num, ensuring one EIP is created for each broker
publicip.type: Public IP type, assigned by referencing the input variable eip_type, default value is "5_bgp" (full dynamic BGP)
bandwidth.name: Bandwidth name, assigned by referencing the input variable bandwidth_name
bandwidth.size: Bandwidth size, assigned by referencing the input variable bandwidth_size, default value is 5 (Mbit/s)
bandwidth.share_type: Bandwidth sharing type, assigned by referencing the input variable bandwidth_share_type, default value is "PER" (dedicated)
bandwidth.charge_mode: Bandwidth billing mode, assigned by referencing the input variable bandwidth_charge_mode, default value is "traffic" (pay-per-use)
6. 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 that supports public network access:
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
description: Instance description, assigned by referencing the input variable instance_description, optional parameter, default value is empty string
public_ip_ids: Public IP ID list, assigned by referencing the EIP resource list, used to bind public IPs to Kafka instances
access_user: Access user name, assigned by referencing the input variable instance_access_user_name, optional parameter, default value is null
password: Access password, assigned by referencing the input variable instance_access_user_password, optional parameter, default value is null
enabled_mechanisms: Enabled authentication mechanisms, assigned by referencing the input variable instance_enabled_mechanisms, optional parameter, default value is null, supports "SCRAM-SHA-512", etc.
port_protocol.private_plain_enable: Whether to enable private network plaintext access, set to true
port_protocol.public_plain_enable: Whether to enable public network plaintext access, assigned by referencing the input variable instance_public_plain_enable, default value is true
port_protocol.public_sasl_ssl_enable: Whether to enable public network SASL SSL access, assigned by referencing the input variable instance_public_sasl_ssl_enable, default value is false
port_protocol.public_sasl_plaintext_enable: Whether to enable public network SASL plaintext access, assigned by referencing the input variable instance_public_sasl_plaintext_enable, default value is false
7. 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:
security_group_rule_remote_ip_prefixneeds to be set to the client IP address segment allowed to access (e.g., "0.0.0.0/0" means allowing all IPs to access, but it is recommended to set it to a specific IP segment to improve security)instance_access_user_passwordneeds to be set to a password that meets password complexity requirements
When 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. In addition, for security reasons, it is recommended to set security_group_rule_remote_ip_prefix to a specific client IP address segment instead of "0.0.0.0/0".
8. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create a Kafka instance that supports public network access:
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, an EIP will be bound to each broker, and you can access the Kafka service through the public IP. It is recommended to enable SASL SSL access to improve security. 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