Deploy RocketMQ Message Send
Application Scenario
Huawei Cloud Distributed Message Service RocketMQ is a highly available, highly reliable, and high-performance distributed message middleware service, widely used in distributed systems in industries such as e-commerce, finance, and IoT. Message sending is one of the core functions of RocketMQ, used to send business data as messages to specified topics for consumer processing.
Through RocketMQ message sending functionality, enterprises can implement modern application patterns such as asynchronous message processing, event-driven architecture, and system decoupling, meeting message communication requirements for different business scenarios. This best practice will introduce how to use Terraform to automatically deploy RocketMQ message sending functionality, including RocketMQ instance, topic, and message sending creation.
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 RocketMQ 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 RocketMQ 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 availability_zones is empty (i.e., executes availability zones list query)
3. Query RocketMQ 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 RocketMQ instances:
Parameter Description:
count: Data source creation count, used to control whether to execute the flavors list query data source, only creates data source when
var.instance_flavor_idis empty (i.e., executes flavors list query)type: RocketMQ instance flavor type, prioritizes using the flavor type specified in input variables, defaults to "cluster.small" if not specified
availability_zones: Availability zones list, prioritizes using the availability zones specified in input variables, uses the first
var.availability_zones_countavailability zones from data source query results if not specifiedavailability_zones_count: Number of availability zones, prioritizes using the availability zones count specified in input variables, defaults to 1 if not specified
4. Create VPC Resource
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
cidr: VPC CIDR block, prioritizes using the CIDR block specified in input variables, defaults to "192.168.0.0/16" if not specified
5. Create VPC Subnet Resource
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: Subnet's VPC ID, references the ID of the VPC resource created earlier
name: Subnet name
cidr: Subnet CIDR block, prioritizes using the CIDR block specified in input variables, automatically calculated if not specified
gateway_ip: Subnet gateway IP, prioritizes using the gateway IP specified in input variables, automatically calculated if not specified
6. Create Security Group Resource
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a security group resource:
Parameter Description:
name: Security group name
delete_default_rules: Whether to delete default rules, set to true to delete default rules
7. Create RocketMQ Instance
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a RocketMQ instance resource:
Parameter Description:
name: RocketMQ instance name
flavor_id: RocketMQ instance flavor ID, prioritizes using the flavor specified in input variables, uses data source query results if not specified
engine_version: RocketMQ engine version, prioritizes using the version specified in input variables, uses data source query results if not specified
storage_spec_code: Storage spec code, prioritizes using the spec specified in input variables, uses data source query results if not specified
storage_space: Storage space size, prioritizes using the storage space size specified in input variables, defaults to 800GB if not specified
availability_zones: Availability zones list, prioritizes using the availability zones specified in input variables, uses data source query results if not specified
vpc_id: VPC ID, references the ID of the VPC resource created earlier
subnet_id: Subnet ID, references the ID of the subnet resource created earlier
security_group_id: Security group ID, references the ID of the security group resource created earlier
broker_num: Broker count, prioritizes using the broker count specified in input variables, defaults to 1 if not specified
description: Instance description
tags: Instance tags
enterprise_project_id: Enterprise project ID
enable_acl: Whether to enable ACL, prioritizes using the enable ACL setting specified in input variables, defaults to false if not specified
tls_mode: TLS mode, prioritizes using the TLS mode specified in input variables, defaults to "SSL" if not specified
configs: Instance configuration block
8. Query RocketMQ Broker 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 topics:
Parameter Description:
count: Data source creation count, used to control whether to execute the Broker query data source, only creates data source when topic broker list is not specified and RocketMQ instance version is 4.8.0
instance_id: RocketMQ instance ID, references the ID of the RocketMQ instance resource created earlier
9. Create RocketMQ Topic
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a RocketMQ topic resource:
Parameter Description:
instance_id: RocketMQ instance ID, references the ID of the RocketMQ instance resource created earlier
name: Topic name
message_type: Message type, prioritizes using the message type specified in input variables, defaults to "NORMAL" if not specified, only valid when RocketMQ instance version is 5.x
queue_num: Queue count, prioritizes using the queue count specified in input variables, defaults to 3 if not specified, only valid when RocketMQ instance version is 4.8.0
permission: Topic permission, prioritizes using the topic permission specified in input variables, defaults to "all" if not specified, only valid when RocketMQ instance version is 4.8.0
brokers: Broker configuration block, prioritizes using the broker list specified in input variables, uses Broker query data source results if not specified
10. Create RocketMQ Message Send
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a RocketMQ message send resource:
Parameter Description:
instance_id: RocketMQ instance ID, references the ID of the RocketMQ instance resource created earlier
topic: Topic name, references the name of the topic resource created earlier
body: Message body content
property_list: Message property list, used to set key-value pair properties for messages
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:
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="subnet_name=my-subnet"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.
12. 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 RocketMQ message send functionalityRun
terraform showto view the details of the created RocketMQ message send functionality
Reference Information
Last updated