Deploy RocketMQ Basic Instance
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. Through modern application patterns such as asynchronous message processing, event-driven architecture, and system decoupling, it meets message communication requirements for different business scenarios. This best practice will introduce how to use Terraform to automatically deploy a basic RocketMQ instance, including VPC, subnet, security group, and EIP 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
var.availability_zonesis 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
charging_mode: Charging mode, prioritizes using the charging mode specified in input variables, defaults to "postPaid" 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 Elastic Public IP Resource (Optional)
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create an elastic public IP resource:
Parameter Description:
count: Resource creation count, used to control whether to create EIP resources, only creates when instance public IP is enabled and no existing public IP is specified, creation count is
var.instance_eips_countpublicip: Public IP configuration block
type: Public IP type, prioritizes using the public IP type specified in input variables, defaults to "5_bgp" if not specified
bandwidth: Bandwidth configuration block
name: Bandwidth name
size: Bandwidth size, prioritizes using the bandwidth size specified in input variables, defaults to 5Mbps if not specified
share_type: Bandwidth type, prioritizes using the bandwidth type specified in input variables, defaults to "PER" if not specified
charge_mode: Charging mode, prioritizes using the charging mode specified in input variables, defaults to "traffic" if not specified
8. 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
enable_publicip: Whether to enable public IP, prioritizes using the enable public IP setting specified in input variables, defaults to false if not specified
publicip_id: Public IP ID to associate when instance public IP is enabled, prioritizes using the public IP specified in input variables, references the EIP resource ID created by resources if not specified
configs: Instance configuration block
charging_mode: Charging mode
period_unit: Charging period unit
period: Charging period
auto_renew: Whether to auto-renew
9. 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.
10. 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 instanceRun
terraform showto view the details of the created RocketMQ instance
Reference Information
Last updated