Manage hosted Apache Kafka with the Confluent Terraform provider
Apache Kafka is an event streaming platform that lets applications publish and consume event messages. Confluent Cloud lets you run Kafka on the cloud provider of your choice without having to manage, monitor, and configure Kafka, or its underlying infrastructure. With the Confluent provider you can provision Kafka clusters using Terraform.
In this tutorial you will use Terraform and the Confluent provider to create a Kafka cluster and topic. You will also create service accounts and grant them fine-grained role-based privileges.
Prerequisites
This tutorial assumes some familiarity with Kafka concepts but does not require any existing infrastructure. If you are not familiar with Kafka clusters, topics, and messages, review Apache's introduction to event streaming.
You can complete this tutorial using the same workflow with either Terraform Community Edition or HCP Terraform. HCP Terraform is a platform that you can use to manage and execute your Terraform projects. It includes features like remote state and execution, structured plan output, workspace resource summaries, and more.
Select the Terraform Community Edition tab to complete this tutorial using Terraform Community Edition.
This tutorial assumes that you are familiar with the Terraform and HCP Terraform workflows. If you are new to Terraform, complete the Get Started collection first. If you are new to HCP Terraform, complete the HCP Terraform Get Started tutorials first.
For this tutorial, you will need:
- Terraform v1.2+ installed locally.
- an HCP Terraform account and organization.
- HCP Terraform locally authenticated.
- a Confluent Cloud account. Register for a new account to receive a $400 credit, which covers the resources used in this tutorial.
Configure Confluent Cloud credentials
To use the Confluent provider to create resources, you must configure it with an API key.
In your Confluent Cloud account, navigate to create API key. Select Granular access, and click Next.
You must associate the new API key with a service account. If you have existing service accounts, click Create a new one. Enter a name for the new service account and click Next.
Confluent Cloud will only display your credentials on creation, so save the key and secret in a secure location. You will use these values to configure the Confluent Terraform Provider later.
Now navigate to Service Accounts, select the service account you created, then click the Access tab. Click Add role assignment, select OrganizationAdmin, and then click Save.
Now, create a global HCP Terraform variable set with CONFLUENT_CLOUD_API_KEY
and CONFLUENT_CLOUD_API_SECRET
environment variables. Use the key
and secret
values you saved previously and mark them as Sensitive.
Tip
For detailed guidance on creating variable sets, review the Create a Credential Variable Set tutorial.
Clone example repository
Clone the example repository for this tutorial, which contains the Terraform configuration for several Confluent Cloud resources.
Change into the repository directory.
Review configuration
In your code editor, open main.tf
to review the example configuration.
Review the confluent
provider block. The provider authenticates with Confluent Cloud using the API key and secret environment variables you set earlier.
Confluent Cloud environments allow you to organize and isolate your resources. You must associate Kafka clusters with an environment. The confluent_environment
resource creates a new tutorial
environment for your cluster.
Now review the confluent_kafka_cluster
resource. It defines a standard
type cluster named inventory
in the AWS us-east-2
region. Confluent Cloud will create this cluster in an environment managed by Confluent, and not in your own AWS account.
The standard
cluster type supports Role-Based Access Controls (RBAC). This allows you to control access to an organization, environment, cluster, and granular Kafka resources like topics and consumer groups, using predefined roles. You can use either RBAC or Access Control Lists (ACLs) to manage permissions. In this tutorial, using RBAC requires fewer permission-related Terraform resources than using ACLs would.
Interacting with a Kafka cluster requires a service account. The configuration defines three confluent_service_account
resources, each with a unique responsibility.
Now review the confluent_role_binding.admin
resource.
This resource grants the predefined CloudClusterAdmin
RBAC role to the admin
service account within the inventory
cluster. Visit Authorization using Role-Based Access Control for a list of predefined roles and for more information about RBAC.
Now review the confluent_role_binding.orders_producer
resource.
This resource grants the orders_producer
service account permission to write messages to the orders
topic within the cluster. The crn_pattern
contains interpolated cluster and topic name values to constrain the permission to a single topic within the cluster.
Similarly, the confluent_role_binding.orders_consumer
resource gives the orders_consumer
service account the ability to read messages from the orders
topic within the cluster. Note that the principal
and role_name
values differ from those in the orders_consumer
resource.
Now review the confluent_api_key.admin
resource. This resource defines an API key for the admin
service account, and associates it with the cluster.
The configuration follows a similar pattern for the orders_producer
and orders_consumer
service account API keys.
Kafka's functionality is built around topics, which are streams of event messages. Theconfluent_kafka_topic
resource creates an orders
topic within the cluster. Terraform authenticates using the admin
API keys created previously.
Create infrastructure
Set the TF_CLOUD_ORGANIZATION
environment variable to your HCP Terraform
organization name. This will configure your HCP Terraform integration.
Initialize your configuration. Terraform will automatically create the learn-terraform-confluent-provider
workspace in your HCP Terraform organization.
Note
The next step requires your global variable set of Confluent Cloud credentials. If you are using a scoped variable set or direct workspace variables, assign them to your new workspace now.
Now, apply the configuration to create your Confluent Cloud environment, Kafka cluster, service accounts, and topic. Respond with yes
to the prompt to confirm the operation.
View resources in Confluent Cloud
In your Confluent Cloud dashboard, review the Service Accounts created by Terraform.
Now visit your Confluent Cloud overview page. Click View environments, then select the tutorial environment, and find the Kafka cluster named inventory.
Click the inventory cluster, then click Topics in the side navigation. Select the orders topic from the list. The topic is empty but you will populate it in the next section.
Leave this browser window open. You will use it later in the tutorial.
Write data to the orders topic
To test your cluster, you will use Confluent's Datagen Source Connector to generate sample data and write it to the orders
topic.
In your text editor, open the empty data-source.tf
file, paste the configuration below, and save the file.
The connector uses the orders_producer
service account to write data. The quickstart
parameter specifies that the source should generate sample messages that mimic order events.
Apply the configuration to create the data source connector. Respond yes
to confirm the operation. It may take up to 10 minutes for Confluent Cloud to provision the connector.
View topic messages
On the orders topic page that you left open in your web browser, click the Messages tab. Confluent now displays the sample messages populated by the source connector.
Recall that the source connector uses the orders_producer
service account, which has permission to write to the orders
topic. An application or user that needs to consume messages from the topic must use the orders_consumer
service account.
Destroy infrastructure
Now that you have completed this tutorial, destroy the Confluent Cloud resources you provisioned to avoid incurring unnecessary costs. Respond yes
to confirm the operation.
Next steps
In this tutorial, you used the Confluent Terraform provider to create a Kafka cluster, service accounts with granular RBAC permissions, and a topic in your Confluent Cloud account. You also created a source connector to generate and write sample data to a topic.
View the following resources to learn more about Confluent Cloud, Kafka, and Terraform.
- Browse Confluent's sample Terraform Confluent Cloud configurations for other usage scenarios
- Review the Confluent Cloud documentation
- Learn about Confluent Cloud Pricing