# 部署RocketMQ基础实例

## 应用场景

华为云分布式消息服务RocketMQ版是一种高可用、高可靠、高性能的分布式消息中间件服务，广泛应用于电商、金融、IoT等行业的分布式系统中， 通过异步消息处理、事件驱动架构、系统解耦等现代化应用模式，满足不同业务场景下的消息通信需求。 本最佳实践将介绍如何使用Terraform自动化部署一个基础的RocketMQ实例，包括VPC、子网、安全组和EIP的创建。

## 相关资源/数据源

本最佳实践涉及以下主要资源和数据源：

### 数据源

* [RocketMQ可用区列表查询数据源（data.huaweicloud\_dms\_rocketmq\_availability\_zones）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/data-sources/dms_rocketmq_availability_zones)
* [RocketMQ规格列表查询数据源（data.huaweicloud\_dms\_rocketmq\_flavors）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/data-sources/dms_rocketmq_flavors)

### 资源

* [VPC资源（huaweicloud\_vpc）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/resources/vpc)
* [VPC子网资源（huaweicloud\_vpc\_subnet）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/resources/vpc_subnet)
* [安全组资源（huaweicloud\_networking\_secgroup）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/resources/networking_secgroup)
* [弹性公网IP资源（huaweicloud\_vpc\_eip）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/resources/vpc_eip)
* [RocketMQ实例资源（huaweicloud\_dms\_rocketmq\_instance）](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs/resources/dms_rocketmq_instance)

### 资源/数据源依赖关系

```
data.huaweicloud_dms_rocketmq_availability_zones
    └── data.huaweicloud_dms_rocketmq_flavors
        └── huaweicloud_dms_rocketmq_instance

huaweicloud_vpc
    └── huaweicloud_vpc_subnet
        └── huaweicloud_dms_rocketmq_instance

huaweicloud_networking_secgroup
    └── huaweicloud_dms_rocketmq_instance

huaweicloud_vpc_eip
    └── huaweicloud_dms_rocketmq_instance
```

## 操作步骤

### 1. 脚本准备

在指定工作空间中准备好用于编写当前最佳实践脚本的TF文件（如main.tf），确保其中（也可以是其他同级目录下的TF文件）包含部署资源所需的provider版本声明和华为云鉴权信息。 配置介绍参考[部署华为云资源前的准备工作](https://hcbp.gitbook.io/hua-wei-yun-provider/chan-pin-jie-shao/prepare_before_deploy)一文中的介绍。

### 2. 通过数据源查询RocketMQ可用区信息

在TF文件（如main.tf）中添加以下脚本以告知Terraform进行一次数据源查询，其查询结果用于创建RocketMQ实例：

```hcl
variable "availability_zones" {
  description = "The availability zones to which the RocketMQ instance belongs"
  type        = list(string)
  default     = []
  nullable    = false
}

# 获取指定region（region参数缺省时默认继承当前provider块中所指定的region）下所有的RocketMQ可用区信息，用于创建RocketMQ实例
data "huaweicloud_dms_rocketmq_availability_zones" "test" {
  count = length(var.availability_zones) == 0 ? 1 : 0
}
```

**参数说明**：

* **count**：数据源的创建数，用于控制是否执行可用区列表查询数据源，仅当 `var.availability_zones` 为空时创建数据源（即执行可用区列表查询）

### 3. 通过数据源查询RocketMQ规格信息

在TF文件（如main.tf）中添加以下脚本以告知Terraform进行一次数据源查询，其查询结果用于创建RocketMQ实例：

```hcl
variable "instance_flavor_id" {
  description = "The flavor ID of the RocketMQ instance"
  type        = string
  default     = ""
  nullable    = false
}

variable "instance_flavor_type" {
  description = "The flavor type of the RocketMQ instance"
  type        = string
  default     = "cluster.small"
}


variable "availability_zones_count" {
  description = "The number of availability zones"
  type        = number
  default     = 1
}

variable "charging_mode" {
  description = "The charging mode of the RocketMQ instance"
  type        = string
  default     = "postPaid"
}

# 获取指定region（region参数缺省时默认继承当前provider块中所指定的region）下所有的RocketMQ规格信息，用于创建RocketMQ实例
data "huaweicloud_dms_rocketmq_flavors" "test" {
  count = var.instance_flavor_id == "" ? 1 : 0

  type               = var.instance_flavor_type
  availability_zones = length(var.availability_zones) != 0 ? var.availability_zones : slice(data.huaweicloud_dms_rocketmq_availability_zones.test[0].availability_zones[*].code, 0, var.availability_zones_count)
  charging_mode      = var.charging_mode
}
```

**参数说明**：

* **count**：数据源的创建数，用于控制是否执行规格列表查询数据源，仅当 `var.instance_flavor_id` 为空时创建数据源（即执行规格列表查询）
* **type**：RocketMQ实例规格类型，优先使用输入变量中指定的规格类型，如未指定则默认为"cluster.small"
* **availability\_zones**：可用区列表，优先使用输入变量中指定的可用区，如未指定则使用数据源查询结果中的前 `var.availability_zones_count` 个可用区
* **availability\_zones\_count**：可用区数量，优先使用输入变量中指定的可用区数量，如未指定则默认为1
* **charging\_mode**：计费模式，优先使用输入变量中指定的计费模式，如未指定则默认为"postPaid"

### 4. 创建VPC资源

在TF文件（如main.tf）中添加以下脚本以告知Terraform创建VPC资源：

```hcl
variable "vpc_name" {
  description = "The VPC name"
  type        = string
}

variable "vpc_cidr" {
  description = "The CIDR block of the VPC"
  type        = string
  default     = "192.168.0.0/16"
}

# 在指定region（region参数缺省时默认继承当前provider块中所指定的region）下创建VPC资源
resource "huaweicloud_vpc" "test" {
  name = var.vpc_name
  cidr = var.vpc_cidr
}
```

**参数说明**：

* **name**：VPC的名称
* **cidr**：VPC的CIDR块，优先使用输入变量中指定的CIDR块，如未指定则默认为"192.168.0.0/16"

### 5. 创建VPC子网资源

在TF文件（如main.tf）中添加以下脚本以告知Terraform创建VPC子网资源：

```hcl
variable "subnet_name" {
  description = "The subnet name"
  type        = string
}

variable "subnet_cidr" {
  description = "The CIDR block of the subnet"
  type        = string
  default     = ""
  nullable    = false
}

variable "subnet_gateway_ip" {
  description = "The gateway IP of the subnet"
  type        = string
  default     = ""
  nullable    = false
}

# 在指定region（region参数缺省时默认继承当前provider块中所指定的region）下创建VPC子网资源
resource "huaweicloud_vpc_subnet" "test" {
  vpc_id     = huaweicloud_vpc.test.id
  name       = var.subnet_name
  cidr       = var.subnet_cidr == "" ? cidrsubnet(huaweicloud_vpc.test.cidr, 8, 0) : var.subnet_cidr
  gateway_ip = var.subnet_gateway_ip == "" ? cidrhost(cidrsubnet(huaweicloud_vpc.test.cidr, 8, 0), 1) : var.subnet_gateway_ip
}
```

**参数说明**：

* **vpc\_id**：子网所属VPC的ID，引用前面创建的VPC资源的ID
* **name**：子网的名称
* **cidr**：子网的CIDR块，优先使用输入变量中指定的CIDR块，如未指定则自动计算
* **gateway\_ip**：子网的网关IP，优先使用输入变量中指定的网关IP，如未指定则自动计算

### 6. 创建安全组资源

在TF文件（如main.tf）中添加以下脚本以告知Terraform创建安全组资源：

```hcl
variable "security_group_name" {
  description = "The security group name"
  type        = string
}

# 在指定region（region参数缺省时默认继承当前provider块中所指定的region）下创建安全组资源
resource "huaweicloud_networking_secgroup" "test" {
  name                 = var.security_group_name
  delete_default_rules = true
}
```

**参数说明**：

* **name**：安全组的名称
* **delete\_default\_rules**：是否删除默认规则，设置为true表示删除默认规则

### 7. 创建弹性公网IP资源（可选）

在TF文件（如main.tf）中添加以下脚本以告知Terraform创建弹性公网IP资源：

```hcl
variable "instance_enable_publicip" {
  description = "Whether to enable public IP for the RocketMQ instance"
  type        = bool
  default     = false
}

variable "instance_publicip_id" {
  description = "The existing public IP ID to bind to the RocketMQ instance"
  type        = string
  default     = ""
  nullable    = false
}

variable "instance_eips_count" {
  description = "The number of EIPs to create for the RocketMQ instance"
  type        = number
  default     = 1
}

variable "eip_type" {
  description = "The type of the EIP"
  type        = string
  default     = "5_bgp"
}

variable "bandwidth_name" {
  description = "The name of the bandwidth"
  type        = string
  default     = "tf_test_bandwidth"
  nullable    = false

  validation {
    condition     = var.instance_eips_count == 0 || var.bandwidth_name != ""
    error_message = "When 'instance_eips_count' is greater than 0, 'bandwidth_name' must be set"
  }
}

variable "bandwidth_size" {
  description = "The size of the bandwidth in Mbps"
  type        = number
  default     = 5
}

variable "bandwidth_share_type" {
  description = "The share type of the bandwidth"
  type        = string
  default     = "PER"
}

variable "bandwidth_charge_mode" {
  description = "The charge mode of the bandwidth"
  type        = string
  default     = "traffic"
}

# 在指定region（region参数缺省时默认继承当前provider块中所指定的region）下创建弹性公网IP资源
resource "huaweicloud_vpc_eip" "test" {
  count = var.instance_enable_publicip ? var.instance_publicip_id != "" ? 0 : var.instance_eips_count : 0

  publicip {
    type = var.eip_type
  }

  bandwidth {
    name        = format("%s-%d", var.bandwidth_name, count.index)
    size        = var.bandwidth_size
    share_type  = var.bandwidth_share_type
    charge_mode = var.bandwidth_charge_mode
  }
}
```

**参数说明**：

* **count**：资源的创建数，用于控制是否创建EIP资源，仅当开启实例公网且未指定现有公网IP时创建，创建数量为 `var.instance_eips_count`
* **publicip**：公网IP配置块
  * **type**：公网IP类型，优先使用输入变量中指定的公网IP类型，如未指定则默认为"5\_bgp"
* **bandwidth**：带宽配置块
  * **name**：带宽名称
  * **size**：带宽大小，优先使用输入变量中指定的带宽大小，如未指定则默认为5Mbps
  * **share\_type**：带宽类型，优先使用输入变量中指定的带宽类型，如未指定则默认为"PER"
  * **charge\_mode**：计费模式，优先使用输入变量中指定的计费模式，如未指定则默认为"traffic"

### 8. 创建RocketMQ实例

在TF文件（如main.tf）中添加以下脚本以告知Terraform创建RocketMQ实例资源：

```hcl
variable "instance_name" {
  description = "The name of the RocketMQ instance"
  type        = string
}

variable "instance_engine_version" {
  description = "The engine version of the RocketMQ instance"
  type        = string
  default     = ""
  nullable    = false

  validation {
    condition     = var.instance_flavor_id == "" || var.instance_engine_version != ""
    error_message = "When 'instance_flavor_id' is not empty, 'instance_engine_version' is required"
  }
}

variable "instance_storage_spec_code" {
  description = "The storage spec code of the RocketMQ instance"
  type        = string
  default     = ""
  nullable    = false
  
  validation {
    condition     = var.instance_flavor_id == "" || var.instance_storage_spec_code != ""
    error_message = "When 'instance_flavor_id' is not empty, 'instance_storage_spec_code' is required"
  }
}

variable "instance_storage_space" {
  description = "The storage space of the RocketMQ instance in GB"
  type        = number
  default     = 800
}

variable "instance_broker_num" {
  description = "The number of brokers for the RocketMQ instance"
  type        = number
  default     = 1
}

variable "instance_description" {
  description = "The description of the RocketMQ instance"
  type        = string
  default     = ""
}

variable "instance_tags" {
  description = "The tags of the RocketMQ instance"
  type        = map(string)
  default     = {}
}

variable "enterprise_project_id" {
  description = "The enterprise project ID"
  type        = string
  default     = null
}

variable "instance_enable_acl" {
  description = "Whether to enable ACL for the RocketMQ instance"
  type        = bool
  default     = false
}

variable "instance_tls_mode" {
  description = "The TLS mode of the RocketMQ instance"
  type        = string
  default     = "SSL"
}

variable "instance_configs" {
  description = "The configuration parameters of the RocketMQ instance"
  type = list(object({
    name  = string
    value = string
  }))

  nullable    = false
  default = []
}

variable "period_unit" {
  description = "The period unit for the RocketMQ instance"
  type        = string
  default     = null
}

variable "period" {
  description = "The period for the RocketMQ instance"
  type        = number
  default     = null
}

variable "auto_renew" {
  description = "Whether to auto renew the RocketMQ instance"
  type        = string
  default     = null
}

# 在指定region（region参数缺省时默认继承当前provider块中所指定的region）下创建RocketMQ实例资源
resource "huaweicloud_dms_rocketmq_instance" "test" {
  name                  = var.instance_name
  flavor_id             = var.instance_flavor_id != "" ? var.instance_flavor_id : try(data.huaweicloud_dms_rocketmq_flavors.test[0].flavors[0].id, null)
  engine_version        = var.instance_engine_version != "" ? var.instance_engine_version : try(data.huaweicloud_dms_rocketmq_flavors.test[0].versions[0], null)
  storage_spec_code     = var.instance_storage_spec_code != "" ? var.instance_storage_spec_code : try(data.huaweicloud_dms_rocketmq_flavors.test[0].flavors[0].ios[0].storage_spec_code, null)
  storage_space         = var.instance_storage_space
  availability_zones    = length(var.availability_zones) != 0 ? var.availability_zones : slice(data.huaweicloud_dms_rocketmq_availability_zones.test[0].availability_zones[*].code, 0, var.availability_zones_count)
  vpc_id                = huaweicloud_vpc.test.id
  subnet_id             = huaweicloud_vpc_subnet.test.id
  security_group_id     = huaweicloud_networking_secgroup.test.id
  broker_num            = var.instance_broker_num
  description           = var.instance_description
  tags                  = var.instance_tags
  enterprise_project_id = var.enterprise_project_id
  enable_acl            = var.instance_enable_acl
  tls_mode              = var.instance_tls_mode
  enable_publicip       = var.instance_enable_publicip
  publicip_id           = var.instance_enable_publicip ? var.instance_publicip_id != "" ? var.instance_publicip_id : join(",", huaweicloud_vpc_eip.test[*].id) : null

  dynamic "configs" {
    for_each = var.instance_configs

    content {
      name  = configs.value.name
      value = configs.value.value
    }
  }

  charging_mode = var.charging_mode
  period_unit   = var.period_unit
  period        = var.period
  auto_renew    = var.auto_renew
}
```

**参数说明**：

* **name**：RocketMQ实例的名称
* **flavor\_id**：RocketMQ实例规格ID，优先使用输入变量中指定的规格，如未指定则使用规格查询数据源的结果
* **engine\_version**：RocketMQ引擎版本，优先使用输入变量中指定的版本，如未指定则使用规格查询数据源的结果
* **storage\_spec\_code**：存储规格代码，优先使用输入变量中指定的规格，如未指定则使用规格查询数据源的结果
* **storage\_space**：存储空间大小，优先使用输入变量中指定的存储空间大小，如未指定则默认为800GB
* **availability\_zones**：可用区列表，优先使用输入变量中指定的可用区，如未指定则使用可用区查询数据源的结果
* **vpc\_id**：VPC ID，引用前面创建的VPC资源的ID
* **subnet\_id**：子网ID，引用前面创建的子网资源的ID
* **security\_group\_id**：安全组ID，引用前面创建的安全组资源的ID
* **broker\_num**：Broker数量，优先使用输入变量中指定的Broker数量，如未指定则默认为1
* **description**：实例描述
* **tags**：实例标签
* **enterprise\_project\_id**：企业项目ID
* **enable\_acl**：是否启用ACL，优先使用输入变量中指定的是否启用ACL，如未指定则默认为false
* **tls\_mode**：TLS模式，优先使用输入变量中指定的TLS模式，如未指定则默认为"SSL"
* **enable\_publicip**：是否启用公网IP，优先使用输入变量中指定的是否启用公网IP，如未指定则默认为false
* **publicip\_id**：实例开启了公网时要关联的公网IP的ID，优先使用输入变量中指定的公网IP，如未指定则引用资源创建的EIP资源ID
* **configs**：实例配置块
* **charging\_mode**：计费模式
* **period\_unit**：计费周期单位
* **period**：计费周期
* **auto\_renew**：是否自动续费

### 9. 预设资源部署所需的入参（可选）

本实践中，部分资源、数据源使用了输入变量对配置内容进行赋值，这些输入参数在后续部署时需要手工输入。 同时，Terraform提供了通过`tfvars`文件预设这些配置的方法，可以避免每次执行时重复输入。

在工作目录下创建`terraform.tfvars`文件，示例内容如下：

```hcl
# VPC和子网配置
vpc_name            = "tf_test_vpc"
subnet_name         = "tf_test_subnet"
security_group_name = "tf_test_security_group"

# RocketMQ实例基本信息
instance_name        = "tf_test_instance"
instance_enable_acl  = true
instance_broker_num  = 2
instance_description = "Created by terraform script"

instance_tags = {
  owner = "terraform"
}

instance_configs = [
  {
    name  = "fileReservedTime"
    value = "72"
  }
]
```

**使用方法**：

1. 将上述内容保存为工作目录下的`terraform.tfvars`文件（该文件名可使用户在执行terraform命令时自动导入该`tfvars`文件中的内容，其他命名则需要在tfvars前补充`.auto`定义，如`variables.auto.tfvars`）
2. 根据实际需要修改参数值
3. 执行`terraform plan`或`terraform apply`时，Terraform会自动读取该文件中的变量值

除了使用`terraform.tfvars`文件外，还可以通过以下方式设置变量值：

1. 命令行参数：`terraform apply -var="vpc_name=my-vpc" -var="subnet_name=my-subnet"`
2. 环境变量：`export TF_VAR_vpc_name=my-vpc`
3. 自定义命名的变量文件：`terraform apply -var-file="custom.tfvars"`

> 注意：如果同一个变量通过多种方式进行设置，Terraform会按照以下优先级使用变量值：命令行参数 > 变量文件 > 环境变量 > 默认值。

### 10. 初始化并应用Terraform配置

完成以上脚本配置后，执行以下步骤来创建资源：

1. 运行 `terraform init` 初始化环境
2. 运行 `terraform plan` 查看资源创建计划
3. 确认资源计划无误后，运行 `terraform apply` 开始创建RocketMQ实例
4. 运行 `terraform show` 查看已创建的RocketMQ实例

## 参考信息

* [华为云分布式消息服务RocketMQ产品文档](https://support.huaweicloud.com/hrm/index.html)
* [华为云Provider文档](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs)
* [RocketMQ基础实例最佳实践源码参考](https://github.com/huaweicloud/terraform-provider-huaweicloud/tree/master/examples/dms/rocketmq/basic-instance)
