This example demonstrates how to use StreamNative Terraform Provider to provision a Serverless cluster and use the Pulsar Terraform Provider to provision Pulsar resources in the Serverless cluster.
This tutorial basically provisions the following resources:
- Provisions a Serverless Instance
sl-instance
. - Provisions a Serverless Cluster
sl-clu
. - Provisions a Service Account
sample-app-sa
. - Provisions an API Key for the Service Account
sample-app-apikey
. - Provisions a Pulsar Tenant
sample-app-tenant
- Provisions a Pulsar Namespace
sample-app-ns
and grantsproduce
andconsume
permissions to the Service Account on the namespace. - Provisions a Pulsar Topic with 4 partitions
sample-app-topic
. - After provisioning all the resources, we will verify the resources by using the pulsarctl command.
git clone https://github.com/streamnative/examples.git
cd examples/terraform/basic
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 examples/terraform/basic
folder.
You need to expose the org_id
variable to the Terraform commands. Please replace <your-org-id>
with your actual organization id.
export TF_VAR_org_id=<your-org-id>
First, initialize the Terraform working directory.
terraform init
Secondly, validate the Terraform configuration files.
terraform validate
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.
Run a targeted plan to see the changes and preview the resources that will be created.
terraform plan --target=module.streamnative_cloud.streamnative_apikey.app-apikey
After that, run a targeted apply to create the resources.
terraform apply --target=module.streamnative_cloud.streamnative_apikey.app-apikey
Run terraform plan
to see the changes and preview the resources that will be created.
terraform plan
After that, run terraform apply
to create the resources.
terraform apply
You should see a similar output as follows:
apikey = "<...>"
pulsarctl_command = "pulsarctl context set -s <pulsar-web-service-url> --token <apikey> sl-clu && pulsarctl topics get sample-app-tenant/sample-app-ns/sample-app-topic"
service_urls = {
"kafka_bootstrap_url" = "..."
"pulsar_broker_service_url" = "..."
"pulsar_web_service_url" = "..."
}
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.
pulsarctl context set -s <pulsar-web-service-url> --token <apikey> sl-clu && pulsarctl topics get sample-app-tenant/sample-app-ns/sample-app-topic
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
sample-app-tenant
- A Pulsar namespace named
sample-app-ns
- A Pulsar topic named
sample-app-topic
produce
andconsume
permissions are granted tosample-app-sa
on the namespacesample-app-tenant/sample-app-ns
You should see the output as follows:
{
"partitions": 4
}