本文介绍如何使用Terraform提供的alicloud_alikafka_consumer_group
资源来创建和管理阿里云Kafka消费者组(Consumer Group)。
前提条件
安装Terraform。支持Terraform 0.13及以上版本。
配置阿里云账号信息。
选择一种阿里云认证方式,为Terraform的执行提供认证信息。本文以环境变量认证方式为例:
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-hangzhou"
说明为保障数据安全性,建议您按需为RAM用户授予Kafka资源的操作权限。具体操作,请参见RAM主子账号授权。
背景信息
Terraform的alicloud_alikafka_consumer_group
资源提供了以下参数:
(必填)instance_id:被创建的Group所属的实例ID。
(必填)consumer_id:Group名称。只能包含字母、数字、下划线(_)和短划线(-)。长度限制为3~128字符。
(可选)tags:为consumer_id绑定的标签。
(可选)description:为consumer_id添加描述信息。
更多信息,请参见alicloud_alikafka_consumer_group。
创建Consumer Group
本示例以在华东1(杭州)地域下创建Group为例,Group名称为example-group
。Group创建完成后不支持修改名称。
创建一个用于存放Terraform资源的项目文件夹,命名为terraform。
执行以下命令,进入项目目录。
cd terraform
执行以下命令,创建名为main.tf的配置文件。
resource "alicloud_alikafka_consumer_group" "default" { consumer_id = var.group_name instance_id = var.instance_id description = var.group_description } variable "instance_id" { description = "ID of the ALIKAFKA Instance that owns the groups." type = string } variable "group_name" { description = "ID of the consumer group. The length cannot exceed 64 characters." type = string } variable "group_description" { description = "The description of the resource." type = string } output "group_name" { value = var.group_name description = "The consumer group" }
执行以下命令,初始化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.212.0 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.
依次执行以下命令,创建Group。
执行以下命令,执行配置文件。
terraform apply
根据提示依次输入Group名称、实例ID、描述等信息。
预期输出:
... Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + group_name = "test-group" ... alicloud_alikafka_consumer_group.default: Creating... alicloud_alikafka_consumer_group.default: Creation complete after 6s [id=alikafka_post-cn-xxxx:test-group] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: group_name = "test-group"
Group已成功创建。
删除Consumer Group
在目标项目录内执行以下命令,运行配置文件。
terraform destroy
根据提示输入Group名称、实例ID。
预期输出:
alicloud_alikafka_consumer_group.default: Refreshing state... [id=alikafka_post-cn-****:test-group] 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_consumer_group.default will be destroyed - resource "alicloud_alikafka_consumer_group" "default" { - consumer_id = "test-group" -> null - description = "test terraform apply" -> null - id = "alikafka_post-cn-****:test-group" -> null - instance_id = "alikafka_post-cn-****" -> null - tags = {} -> null } Plan: 0 to add, 0 to change, 1 to destroy. Changes to Outputs: - group_name = "test-group" -> null ... alicloud_alikafka_consumer_group.default: Destroying... [id=alikafka_post-cn-****:test-group] alicloud_alikafka_consumer_group.default: Destruction complete after 5s Destroy complete! Resources: 1 destroyed.
Group已成功删除。
查询Consumer Group
创建一个用于存放Terraform资源的项目文件夹,命名为terraform。
执行以下命令,进入项目目录。
cd terraform
执行以下命令,创建名为main.tf的配置文件。
data "alicloud_alikafka_consumer_groups" "consumer_groups_ds" { instance_id = var.instance_id output_file = "consumerGroups.json" } variable "instance_id" { description = "ID of the ALIKAFKA Instance." type = string } output "group_list" { value = "${data.alicloud_alikafka_consumer_groups.consumer_groups_ds.groups}" }
执行以下命令,初始化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.212.0 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.
依次执行以下命令,查询此实例中Group列表。
执行以下命令,运行配置文件。
terraform apply
根据提示输入实例ID 。
预期输出:
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. ... Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: group_list = tolist([ { "consumer_id" = "test" "id" = "alikafka_post-cn-****:test" "instance_id" = "alikafka_post-cn-****" "remark" = "test" "tags" = tomap({}) }, ])
相关文档
您也可以通过控制台和API调用方式管理Group,请参见步骤三:创建资源和CreateConsumerGroup - 创建消费组。
如果您想了解
alicloud_alikafka_consumer_group
提供的更多参数,请参见alicloud_alikafka_consumer_group。