Terraform
Installing Terraform
1. Download Terraform.
2. Install Terraform.
3. Set the Terraform path in the environmental variables.
4. To verify that Terraform is installed, type terraform
at the command line to display an options list.
Authentication: Setting your ClearBlade credentials
Navigate to the ClearBlade IoT Core Developer Portal and log in with your existing ClearBlade IoT Core account. Within the ClearBlade IoT Core Console, generate your ClearBlade service account credentials and save the JSON file with your service account's credentials.
Use the provider block to configure the path to your service account's JSON file for authentication. The following environment variable can be used to set your credentials in your terminal or IDE environment:
export CLEARBLADE_CONFIGURATION=/path/to/file.json
Downloading the provider
1. Head to the ClearBlade Terraform Provider page.
2. Click the Use Provider button in the top-right corner.
3. Copy and paste the source code into a file named versions.tf
.
4. Place all of the required .tf
files into the same folder.
5. Run terraform init
.
6. Run terraform plan
.
7. Run terraform apply
.
Using the provider
This section describes using the Terraform Provider to provision a registry resource in ClearBlade IoT Core. First, you must include the ClearBlade Terraform Provider in your provider's list. If you already have a Terraform file set up, add the following:
terraform {
required_providers {
clearblade = {
source = "ClearBlade/clearblade"
version = "0.3.0"
}
}
}
provider "clearblade" {
# Configuration options
credentials = var.clearblade-creds
project = var.gcp_project_id
region = var.gcp_region
}
resource "clearblade_iot_registry" "example" {
id = var.registry_id
event_notification_configs = [
{
pubsub_topic_name = var.event_topic_name
subfolder_matches = var.event_subfolder_matches
},
{
pubsub_topic_name = var.event_topic_name
subfolder_matches = ""
}
]
state_notification_config = {
pubsub_topic_name = var.state_topic_name
}
mqtt_config = {
mqtt_enabled_state = "MQTT_ENABLED"
}
http_config = {
http_enabled_state = "HTTP_ENABLED"
}
log_level = var.log_level
}
resource "clearblade_iot_registry" "example1" {
id = var.registry_id_1
event_notification_configs = [
{
pubsub_topic_name = var.event_topic_name
subfolder_matches = var.event_subfolder_matches
},
{
pubsub_topic_name = var.event_topic_name
subfolder_matches = ""
}
]
state_notification_config = {
pubsub_topic_name = var.state_topic_name
}
mqtt_config = {
mqtt_enabled_state = "MQTT_ENABLED"
}
http_config = {
http_enabled_state = "HTTP_DISABLED"
}
log_level = var.log_level
}
You should use a file named variables.tf
that can hold things like your credentials, which would look like this:
variable "clearblade-creds" {
type = string
default = "Path to ClearBlade's service-account JSON Auth File"
}
variable "gcp_project_id" {
type = string
default = "gcp_project_id"
}
variable "gcp_region" {
type = string
default = "gcp_region_here"
}
variable "registry_id" {
type = string
default = "registry_id_here"
}
variable "registry_id_1" {
type = string
default = "registry_id_here"
}
variable "event_subfolder_matches" {
type = string
default = "test-path"
}
variable "event_topic_name" {
type = string
default = "projects/gcp_project_id_here/topics/rootevent"
}
variable "state_topic_name" {
type = string
default = "projects/gcp_project_id_here/topics/rootevent"
}
variable "log_level" {
type = string
default = "INFO"
}
Importing an existing registry created elsewhere, e.g., via the console UI, under Terraform management:
Creating a module
This section describes using the Terraform provider with a Terraform module to provision registries in ClearBlade IoT Core. In a new directory, create your root module by constructing a new main.tf
configuration file. Then, create a directory called modules that contains another folder called clearblade-registry
. You will work with three Terraform configuration files inside the clearblade-registry
directory: main.tf
, variables.tf
, and versions.tf
.
First, create the directory for your new module. Create a file named versions.tf
inside the module and include the ClearBlade Terraform Provider in your provider's list. If you followed the steps above, you should already have a file named versions.tf
. If you have not, add the following to your versions.tf
in modules/clearblade-registry
:
Add this ClearBlade IoT registry resource to your main.tf
file inside the modules/clearblade-registry
directory:
Navigate to the variables.tf
file in your module and add the following code:
Return to your root directory with the main.tf
and create two additional Terraform configuration files: locals.tf
, and versions.tf
.
Navigate to your locals.tf
and add this reference:
Add the following to the versions.tf
:
Add this to your main.tf
in the root directory:
Installing the local module
When a new module is added to a configuration, Terraform must install it before it can be used. The terraform get
or terraform init
command will install and update the module.
Install the module:
Provision your registries:
Respond yes to the prompt. Your registries will be provisioned.
Deleting a registry
Delete a registry by commenting out that registry’s module block. Run terraform plan
or terraform apply
.
Importing a registry
DeviceRegistry can be imported using this format:
terraform import module.bas-4.clearblade_iot_registry.registry bas-4
.