Deploy RocketMQ Migration Task

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. Migration tasks are one of the important functions of RocketMQ, used to migrate existing message middleware configurations and data to Huawei Cloud RocketMQ instances, achieving smooth business migration.

Through RocketMQ migration tasks, enterprises can achieve seamless migration from other message middleware (such as Apache RocketMQ, RabbitMQ, etc.) to Huawei Cloud RocketMQ, including topic configurations, consumer group configurations, virtual hosts, queues, exchanges, and binding relationships, ensuring business continuity and data integrity. This best practice will introduce how to use Terraform to automatically deploy RocketMQ migration tasks, including RocketMQ instance and migration task creation.

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_id is 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_count availability zones from data source query results if not specified

  • availability_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. Create RocketMQ Migration Task

Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a RocketMQ migration task resource:

Create Migration Task from RocketMQ to RocketMQ

Create Migration Task from RabbitMQ to RocketMQ

Parameter Description:

  • instance_id: RocketMQ instance ID, references the ID of the RocketMQ instance resource created earlier

  • name: Migration task name

  • overwrite: Whether to overwrite existing configurations with the same name, prioritizes using the overwrite setting specified in input variables, defaults to false if not specified

  • type: Migration task type, supports "rocketmq" and "rabbitToRocket" types

  • topic_configs: Topic configuration block, used to configure RocketMQ topic-related parameters (only used when type="rocketmq")

  • subscription_groups: Subscription group configuration block, used to configure consumer group-related parameters (only used when type="rocketmq")

  • vhosts: Virtual host configuration block, used to configure RabbitMQ virtual hosts (only used when type="rabbitToRocket")

  • queues: Queue configuration block, used to configure RabbitMQ queues (only used when type="rabbitToRocket")

  • exchanges: Exchange configuration block, used to configure RabbitMQ exchanges (only used when type="rabbitToRocket")

  • bindings: Binding relationship configuration block, used to configure RabbitMQ binding relationships (only used when type="rabbitToRocket")

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:

  1. Save the above content as terraform.tfvars file in the working directory (this file name allows users to automatically import the content of this tfvars file when executing terraform commands; for other names, .auto needs to be added before tfvars, such as variables.auto.tfvars)

  2. Modify parameter values as needed

  3. When executing terraform plan or terraform 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:

  1. Command line parameters: terraform apply -var="vpc_name=my-vpc" -var="subnet_name=my-subnet"

  2. Environment variables: export TF_VAR_vpc_name=my-vpc

  3. Custom 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:

  1. Run terraform init to initialize the environment

  2. Run terraform plan to view the resource creation plan

  3. After confirming the resource plan is correct, run terraform apply to start creating the RocketMQ migration task

  4. Run terraform show to view the details of the created RocketMQ migration task

Reference Information

Last updated