文档

使用Terraform管理Kafka Topic

更新时间:

Topic是云消息队列 Kafka 版里对消息进行的一级归类,使用Kafka的第一步就是先为您的应用创建Topic。本文介绍如何通过Terraform提供的alicloud_alikafka_topic资源创建、删除及查询Topic。

前提条件

  • 安装Terraform。支持Terraform 0.13及以上版本。

  • 配置阿里云账号信息。

    选择一种阿里云认证方式,为Terraform的执行提供认证信息。本文以环境变量认证方式为例:

    export ALICLOUD_ACCESS_KEY="************"
    export ALICLOUD_SECRET_KEY="************"
    export ALICLOUD_REGION="cn-hangzhou"
    说明

    为保障数据安全性,建议您按需为RAM用户授予Kafka资源的操作权限。具体操作,请参见RAM主子账号授权

背景信息

Terraform的alicloud_alikafka_topic资源提供了以下参数:

  • (必填)instance_id:被创建的Topic所属的实例ID。

  • (必填)Topic:Topic名称。只能包含字母、数字、下划线(_)和短划线(-)。长度限制为3~64字符,多于64个字符将被自动截取。

  • (必填)Remark:Topic的备注。

  • (可选)local_topic:Topic的存储引擎是否为Local存储。

  • (可选)compact_topic:Topic的日志清理策略是否为compact清理策略。当存储引擎配置为Local存储时,需配置此项。

  • (可选)partition_num:Topic的分区数。分区数限制1~800。

  • (可选)tags:资源标签。

更多信息,请参见alicloud_alikafka_topic

创建Topic

本示例以在华东1(杭州)地域下创建Topic为例,Topic名称为example-topic。Topic创建完成后不支持修改名称。

  1. 创建一个用于存放Terraform资源的项目文件夹,命名为terraform。

  2. 执行以下命令,进入项目目录。

    cd terraform
  3. 执行以下命令,创建名为topic.tf的配置文件。

    resource "alicloud_alikafka_topic" "default" {
      instance_id   = "<Kafka实例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"
    }
  4. 执行以下命令,初始化Terraform运行环境。

    terraform init

    预期输出:

    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.
  5. 依次执行以下命令,创建Topic。

    1. 执行以下命令,执行配置文件。

      terraform apply
    2. 根据提示依次输入分区数、Topic备注等信息。

      • partition_num:输入1-800内的整数,本示例以分区数为12为例。

      • remark:输入Topic备注。

      预期输出:

      ...
      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已成功创建。

更新Topic

本示例以修改Topic的分区数和备注为例。

  1. 执行以下命令,执行配置文件。

    terraform apply
  2. 根据提示依次输入分区数、Topic备注等信息。

    • partition_num:输入1-800内的整数。本示例将分区数从12修改为24。

    • remark:输入Topic备注。

    预期输出:

    ...
    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"

    分区数和Topic备注已成功更新。

删除Topic

  1. 在目标项目目录内执行以下命令,运行配置文件。

    terraform destroy
  2. 根据提示依次输入分区数、Topic备注等信息。

    • partition_num:输入此Topic的分区数。

    • remark:输入此Topic的备注。

    预期输出:

    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

  1. 创建一个用于存放Terraform资源的项目文件夹,命名为terraform。

  2. 执行以下命令,进入项目目录。

    cd terraform
  3. 执行以下命令,创建名为main.tf的配置文件。

    data "alicloud_alikafka_topics" "topics_ds" {
      instance_id = "<Kafka实例名称>"
      output_file = "topics.txt"
    }
    
    output "first_topic_name" {
      # 输出列表中的第一个Topic的名称。请注意,索引[0]在这里表示列表中的第一项。
      value = "${data.alicloud_alikafka_topics.topics_ds.topics.0.topic}"
    }
  4. 执行以下命令,初始化Terraform运行环境。

    terraform init

    预期输出:

    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.
  5. 执行以下命令,查询实例Topic列表中的第一个Topic。

    terraform apply

    预期输出:

    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"

    关于此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": "服务中",
    		"tags": {},
    		"topic": "testtopic"
    	}
    ]

相关文档

  • 本页导读 (1)
文档反馈