使用Terraform管理Kafka Topic
Topic是云消息隊(duì)列 Kafka 版里對(duì)消息進(jìn)行的一級(jí)歸類,使用Kafka的第一步就是先為您的應(yīng)用創(chuàng)建Topic。本文介紹如何通過Terraform提供的alicloud_alikafka_topic
資源創(chuàng)建、刪除及查詢Topic。
前提條件
安裝Terraform。支持Terraform 0.13及以上版本。
配置阿里云賬號(hào)信息。
選擇一種阿里云認(rèn)證方式,為Terraform的執(zhí)行提供認(rèn)證信息。本文以環(huán)境變量認(rèn)證方式為例:
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-hangzhou"
說明為保障數(shù)據(jù)安全性,建議您按需為RAM用戶授予Kafka資源的操作權(quán)限。具體操作,請參見RAM主子賬號(hào)授權(quán)。
背景信息
Terraform的alicloud_alikafka_topic
資源提供了以下參數(shù):
(必填)instance_id:被創(chuàng)建的Topic所屬的實(shí)例ID。
(必填)Topic:Topic名稱。只能包含字母、數(shù)字、下劃線(_)和短劃線(-)。長度限制為3~64字符,多于64個(gè)字符將被自動(dòng)截取。
(必填)Remark:Topic的備注。
(可選)local_topic:Topic的存儲(chǔ)引擎是否為Local存儲(chǔ)。
(可選)compact_topic:Topic的日志清理策略是否為compact清理策略。當(dāng)存儲(chǔ)引擎配置為Local存儲(chǔ)時(shí),需配置此項(xiàng)。
(可選)partition_num:Topic的分區(qū)數(shù)。分區(qū)數(shù)限制1~800。
(可選)tags:資源標(biāo)簽。
更多信息,請參見alicloud_alikafka_topic。
創(chuàng)建Topic
本示例以在華東1(杭州)地域下創(chuàng)建Topic為例,Topic名稱為example-topic
。Topic創(chuàng)建完成后不支持修改名稱。
創(chuàng)建一個(gè)用于存放Terraform資源的項(xiàng)目文件夾,命名為terraform。
執(zhí)行以下命令,進(jìn)入項(xiàng)目目錄。
cd terraform
執(zhí)行以下命令,創(chuàng)建名為topic.tf的配置文件。
resource "alicloud_alikafka_topic" "default" { instance_id = "<Kafka實(shí)例ID>" topic = "example-topic" local_topic = "false" compact_topic = "false" partition_num = var.partition_num remark = var.remark } variable "partition_num" { description = "Partition Description" type = number } variable "remark" { description = "Topic Description" type = string } output "partition_num" { value = var.partition_num description = "Partition Description" } output "remark" { value = var.remark description = "Topic Description" }
執(zhí)行以下命令,初始化Terraform運(yùn)行環(huán)境。
terraform init
預(yù)期輸出:
Initializing provider plugins... - Using previously-installed hashicorp/alicloud v1.211.2 Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
依次執(zhí)行以下命令,創(chuàng)建Topic。
執(zhí)行以下命令,執(zhí)行配置文件。
terraform apply
根據(jù)提示依次輸入分區(qū)數(shù)、Topic備注等信息。
partition_num:輸入1-800內(nèi)的整數(shù),本示例以分區(qū)數(shù)為12為例。
remark:輸入Topic備注。
預(yù)期輸出:
... Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + partition_num = 12 + remark = "kafka_topic_remark" ... alicloud_alikafka_topic.default: Creating... alicloud_alikafka_topic.default: Still creating... [10s elapsed] alicloud_alikafka_topic.default: Creation complete after 10s [id=alikafka_post-cn-5yd3eegy****:example-topic] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: partition_num = 12 remark = "kafka_topic_remark"
Topic已成功創(chuàng)建。
更新Topic
本示例以修改Topic的分區(qū)數(shù)和備注為例。
執(zhí)行以下命令,執(zhí)行配置文件。
terraform apply
根據(jù)提示依次輸入分區(qū)數(shù)、Topic備注等信息。
partition_num:輸入1-800內(nèi)的整數(shù)。本示例將分區(qū)數(shù)從12修改為24。
remark:輸入Topic備注。
預(yù)期輸出:
... alicloud_alikafka_topic.default: Refreshing state... [id=alikafka_post-cn-5yd3eegy****:example-topic] ... Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: ~ update in-place Terraform will perform the following actions: # alicloud_alikafka_topic.default will be updated in-place ~ resource "alicloud_alikafka_topic" "default" { id = "alikafka_post-cn-5yd3eegy****:example-topic" ~ partition_num = 12 -> 24 ~ remark = "kafka_topic_remark" -> "updated_kafka_topic_remark" tags = {} # (4 unchanged attributes hidden) } Plan: 0 to add, 1 to change, 0 to destroy. ... alicloud_alikafka_topic.default: Modifying... [id=alikafka_post-cn-5yd3eegy****:example-topic] alicloud_alikafka_topic.default: Modifications complete after 2s [id=alikafka_post-cn-5yd3eegy****:example-topic] Apply complete! Resources: 0 added, 1 changed, 0 destroyed. Outputs: partition_num = 24 remark = "updated_kafka_topic_remark"
分區(qū)數(shù)和Topic備注已成功更新。
刪除Topic
在目標(biāo)項(xiàng)目目錄內(nèi)執(zhí)行以下命令,運(yùn)行配置文件。
terraform destroy
根據(jù)提示依次輸入分區(qū)數(shù)、Topic備注等信息。
partition_num:輸入此Topic的分區(qū)數(shù)。
remark:輸入此Topic的備注。
預(yù)期輸出:
alicloud_alikafka_topic.default: Refreshing state... [id=alikafka_post-cn-5yd3eegy****:example-topic] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # alicloud_alikafka_topic.default will be destroyed - resource "alicloud_alikafka_topic" "default" { - compact_topic = false -> null - id = "alikafka_post-cn-5yd3eegy****:example-topic" -> null - instance_id = "alikafka_post-cn-5yd3eegy****" -> null - local_topic = false -> null - partition_num = 24 -> null - remark = "updated_kafka_topic_remark" -> null - tags = {} -> null - topic = "example-topic" -> null } Plan: 0 to add, 0 to change, 1 to destroy. Changes to Outputs: - partition_num = 24 -> null - remark = "updated_kafka_topic_remark" -> null ... alicloud_alikafka_topic.default: Destroying... [id=alikafka_post-cn-5yd3eegy****:example-topic] alicloud_alikafka_topic.default: Still destroying... [id=alikafka_post-cn-5yd3eegy****:example-topic, 10s elapsed] alicloud_alikafka_topic.default: Destruction complete after 17s Destroy complete! Resources: 1 destroyed.
Topic已成功刪除。
查詢Topic
創(chuàng)建一個(gè)用于存放Terraform資源的項(xiàng)目文件夾,命名為terraform。
執(zhí)行以下命令,進(jìn)入項(xiàng)目目錄。
cd terraform
執(zhí)行以下命令,創(chuàng)建名為main.tf的配置文件。
data "alicloud_alikafka_topics" "topics_ds" { instance_id = "<Kafka實(shí)例名稱>" output_file = "topics.txt" } output "first_topic_name" { # 輸出列表中的第一個(gè)Topic的名稱。請注意,索引[0]在這里表示列表中的第一項(xiàng)。 value = "${data.alicloud_alikafka_topics.topics_ds.topics.0.topic}" }
執(zhí)行以下命令,初始化Terraform運(yùn)行環(huán)境。
terraform init
預(yù)期輸出:
Initializing the backend... Initializing provider plugins... - Reusing previous version of hashicorp/alicloud from the dependency lock file - Using previously-installed hashicorp/alicloud v1.211.2 ... Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
執(zhí)行以下命令,查詢實(shí)例Topic列表中的第一個(gè)Topic。
terraform apply
預(yù)期輸出:
No changes. Your infrastructure matches the configuration. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed. Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: first_topic_name = "testtopic"
關(guān)于此Topic的更多信息,可在
topics.txt
文件中獲取。[ { "compact_topic": false, "create_time": 1700191087000, "id": "alikafka_post-cn-uax3go6z****:testtopic", "instance_id": "alikafka_post-cn-uax3go6z****", "local_topic": false, "partition_num": 36, "remark": "beizhu", "status": 0, "status_name": "服務(wù)中", "tags": {}, "topic": "testtopic" } ]
相關(guān)文檔
您也可以通過控制臺(tái)和API調(diào)用方式管理Topic,請參見步驟三:創(chuàng)建資源和CreateTopic - 創(chuàng)建Topic。
如果您想了解
alicloud_alikafka_topic
提供的更多參數(shù),請參見alicloud_alikafka_topic。