Skip to main content
This tutorial demonstrates how to use StreamNative Terraform Provider to deploy a Serverless cluster and use the Pulsar Terraform Provider to provision Pulsar resources in the Serverless cluster. This tutorial provisions the following resources (assuming we name the application as sl-app):
  1. Provisions a Serverless Instance (i.e., sl-instance).
  2. Provisions a Serverless Cluster (i.e., sl-clu).
  3. Provisions a Service Account named sl-app-sa.
  4. Provisions an API Key for the Service Account sl-app-apikey.
  5. Provisions a Pulsar Tenant named sl-app-tenant
  6. Provisions a Pulsar Namespace sl-app-ns and grants produce and consume permissions to the Service Account on the namespace.
  7. Provisions a Pulsar Topic with 4 partitions sl-app-topic.
  8. After provisioning all the resources, we will verify the resources by using the pulsarctl command.
The code examples is available in the examples/terraform/serverless folder.

0. Prerequisites

Create a new directory anywhere you’d like for this project.

1. Create a Super-Admin Service Account

First, you need to create a service account called tf-runner with Super Admin access. Please refer to Create a Service Account for details. After you have created the service account, download the OAuth2 credentials file and save it as tf-runner.json in the terraform-getting-started folder that you created earlier.

2. Create Terraform Configuration Files to provision StreamNative Cloud resources

2.1 Create a Module Folder

Create a module folder streamnative_cloud.

2.2 Create Variables File

Create a variables.tf file inside the streamnative_cloud folder and add the following code snippet to prepare the variables. Remember to replace <your-organization-id> with your StreamNative Cloud organization id.
  • org_id: Get your StreamNative Cloud organization id from here and replace <your-organization-id> with it.
  • instance_name: The name of the Pulsar instance. Default value is sl-instance.
  • cluster_name: The name of the Pulsar cluster. Default value is sl-clu.
  • app_name: The name of the application. Default value is sl-app.

2.3 Create Terraform Configuration File

Create a main.tf file inside the streamnative_cloud folder and add the following code snippet to create the resources.
Use the StreamNative Terraform Provider version 0.7.0 or later.

3. Create Terraform Configuration Files to provision Pulsar resources

Go back to the root folder terraform-getting-started.

3.1 Create Variables File

You can copy the variables.tf file from the streamnative_cloud module folder.

3.2 Create Terraform Configuration File

Create a main.tf file in the root folder and add the following code snippet to create the resources.

4. Run Terraform Commands

Before running the Terraform commands, you need to expose the following variables:
Please replace the above placeholders with your actual values:
  • <your-org-id>: Your StreamNative Cloud organization ID
  • <your-instance-name>: A unique name for your Pulsar instance (e.g., “sl-instance”)
  • <your-cluster-name>: A unique name for your Pulsar cluster (e.g., “sl-clu”)
  • <your-app-name>: A unique name for your application (e.g., “sl-app”)
An example of exposing the variables is as follows:
After exposing the variables, you can run the following Terraform commands to provision the resources. First, initialize the Terraform working directory.
Secondly, validate the Terraform configuration files.
Since we use two providers in this example (the StreamNative Provider and the Pulsar Provider), we need to provision the resources in two steps. The Pulsar Provider resources depend on the StreamNative Provider resources being created first.

4.1 Provision the Cloud Resources

Run a targeted plan to see the changes and preview the resources that will be created.
After that, run a targeted apply to create the resources.

4.2 Provision all the Resources

Run terraform plan to see the changes and preview the resources that will be created.
After that, run terraform apply to create the resources.
You should see a similar output as follows:

5. Verify the Resources

Use the pulsarctl command to verify all the resources created. Make sure you have installed pulsarctl before running the command. Copy the pulsarctl_command output and run it in your terminal.
This command will set the context and get the topic details. It will verify the following resources are created:
  • A Pulsar cluster named sl-clu
  • A Pulsar tenant named sl-app-tenant
  • A Pulsar namespace named sl-app-ns
  • A Pulsar topic named sl-app-topic
  • produce and consume permissions are granted to sl-app-sa on the namespace sl-app-tenant/sl-app-ns
You should see the output as follows: