Deploying and Managing InfluxDB Resources with Terraform
Tags: provider, terraform, iot, iiot
Published: 2024-04-05 16:21:30

---
title: Deploying and Managing InfluxDB Resources with Terraform
published: true
date: 2024-02-15 17:45:23 UTC
tags: provider,terraform,iot,iiot
canonical_url: 
---

### Introduction:

- Terraform is a powerful infrastructure as code tool that automates cloud infrastructure provisioning and management through simple configuration files. If you’re interested in learning more, I’ve written a short [blog](https://thulasirajkomminar.com/what-is-hashicorp-terraform-42d419a61cb9) outlining some key components of Terraform.
- InfluxDB is a specialized time-series database tailored for efficiently handling large volumes of time-stamped data. In this blog, we’ll explore how to leverage Terraform for provisioning and managing resources within InfluxDB.

![](https://cdn-images-1.medium.com/max/1024/1*8wpyVg6vy_4xqzZwpYMu6g.png)

### Prerequisites:

> To install Terraform, you can easily follow the steps outlined in this blog post: [Install Terraform](https://developer.hashicorp.com/terraform/install).

> Before diving in, ensure you have a basic understanding of InfluxDB and its components. For installation guidance, refer to this resource: [Install InfluxDB](https://docs.influxdata.com/influxdb/v2/install/).

### Provider Configuration:

To create and manage InfluxDB resources using Terraform, it utilizes specialized plugins known as providers to interface with InfluxDB. I’ve developed and published a provider for InfluxDB on the [Terraform registry](https://registry.terraform.io/providers/komminarlabs/influxdb/latest), enabling seamless resource creation and management.

Let’s begin by configuring the provider.

- Create a file named `versions.tf` and insert the following code to declare the InfluxDB provider. This allows Terraform to install and utilize the provider. Provider dependencies are specified within a `required_providers` block.

```hcl
terraform {
  required_providers {
    influxdb = {
      source  = "komminarlabs/influxdb"
      version = "1.0.0"
    }
  }
}
```

- Create another file named `main.tf` to initialize the InfluxDB provider. We’ll use a provider block to configure it. You can specify the provider configuration using arguments such as url and token, or alternatively, utilize environment variables like `INFLUXDB_TOKEN` and `INFLUXDB_URL`.

```hcl
export INFLUXDB_TOKEN="influxdb-token"
export INFLUXDB_URL="http://localhost:8086"

provider "influxdb" {
  token = "influxdb-token"
  url   = "http://localhost:8086"
}
```

### Creating and Managing InfluxDB Resources:

The [komminarlabs/influxdb](https://registry.terraform.io/providers/komminarlabs/influxdb/latest/docs) provider offers various data sources and resources.

#### Data Sources:

- `influxdb_authorization`
- `influxdb_authorizations`
- `influxdb_bucket`
- `influxdb_buckets`
- `influxdb_organization`
- `influxdb_organizations`

#### Resources:

- `influxdb_authorization`
- `influxdb_bucket`
- `influxdb_organization`

We’ll begin by creating resources and then utilize data sources to query the created resources. Let’s create two files: `resources.tf` and `datasources.tf`.

#### Resources

Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects.

- **Organization:**

An InfluxDB organization is a workspace for a group of users. All dashboards, tasks, buckets, members, etc., belong to an organization. Add the following code to create our organisation.

```hcl
resource "influxdb_organization" "iot" {
  name        = "IoT"
  description = "An IoT organization"
}
```

After running a Terraform plan and verifying everything looks good, let’s proceed with applying the changes.

![](https://cdn-images-1.medium.com/max/1024/1*zICyNzJKVX8mMZQbzu1PXA.png)
_Terraform execution plan and apply results_

![](https://cdn-images-1.medium.com/max/508/1*rRwX8abPvmuf9mLXyBTlKw.png)
_Switch to the new organization in InfluxDB_

- **Bucket:**

An InfluxDB bucket is a named location where time series data is stored. All buckets have a retention period, a duration of time that each data point persists. InfluxDB drops all points with timestamps older than the bucket’s retention period. A bucket belongs to an organization.

Let’s proceed by creating a bucket named signals with a retention period of 14 days (1209600 seconds).

```hcl
resource "influxdb_bucket" "signals" {
  org_id           = influxdb_organization.iot.id
  name             = "signals"
  description      = "This is a bucket to store signals"
  retention_period = 1209600
}
```

![](https://cdn-images-1.medium.com/max/1024/1*F0XJxEGm__3v6HspIie2-Q.png)
_Terraform execution plan and apply results_

![](https://cdn-images-1.medium.com/max/1024/1*Npj8M3_QTqNcAZmFBsE47A.png)

- **Authorization:**

Authorizations are InfluxDB Read/Write API tokens that grants read access, write access, or both to specific buckets in an organization.

In the following step, we will generate an authorization that enables both read and write access to the bucket established in the prior phase.

```hcl
resource "influxdb_authorization" "signals_rw" {
  org_id      = influxdb_organization.iot.id
  description = "Read & Write access signals bucket"

  permissions = [{
    action   = "read"
    resource = {
      id     = influxdb_bucket.signals.id
      org_id = influxdb_organization.iot.id
      type   = "buckets"
    }
    },
    {
      action   = "write"
      resource = {
        id     = influxdb_bucket.signals.id
        org_id = influxdb_organization.iot.id
        type   = "buckets"
      }
  }]
}
```

![](https://cdn-images-1.medium.com/max/1024/1*jwPlfb1Yw78DiiMRn7zuqA.png)
_Terraform execution plan and apply results_

![](https://cdn-images-1.medium.com/max/1024/1*TycVtTQiX5aw7pwRF2IryA.png)
_API token shown in InfluxDB_

#### Data Sources

With all the necessary resources created in InfluxDB to manage our time series data, we can now utilize datasources to list all the resources we’ve created.

A data source is accessed via a special kind of resource known as a data resource, declared using a data block.

```hcl
data "influxdb_organization" "iot" {
  name = "IoT"
}

output "iot_organization" {
  value = data.influxdb_organization.iot
}

data "influxdb_bucket" "signals" {
  name = "signals"
}

output "signals_bucket" {
  value = data.influxdb_bucket.signals
}
```

![](https://cdn-images-1.medium.com/max/1024/1*YbdFNy-4ap6yu9AfDoawDQ.png)
_Terraform execution plan and apply results_

### Additional Resources:

- [What is HashiCorp Terraform?. Introduction | by Thulasiraj Komminar | Medium](https://thulasirajkomminar.com/what-is-hashicorp-terraform-42d419a61cb9)
- [Install | Terraform | HashiCorp Developer](https://developer.hashicorp.com/terraform/install)
- [Install InfluxDB | InfluxDB OSS v2 Documentation (influxdata.com)](https://docs.influxdata.com/influxdb/v2/install/)
- [komminarlabs/influxdb | Terraform Registry](https://registry.terraform.io/providers/komminarlabs/influxdb/latest)
- [Overview — Configuration Language | Terraform | HashiCorp Developer](https://developer.hashicorp.com/terraform/language)
- [terraform-provider-influxdb/examples at main · komminarlabs/terraform-provider-influxdb (github.com)](https://github.com/komminarlabs/terraform-provider-influxdb/tree/main/examples)

### Conclusion:

Now that we’ve explored how to leverage Terraform for creating and managing InfluxDB resources, it’s time to start utilizing it. If you encounter any bugs or issues while using the provider, be sure to [report](https://github.com/komminarlabs/terraform-provider-influxdb/issues) them promptly.