Skip to content

Deploying and Managing InfluxDB Resources with Terraform

Published: at 12:00 AM

Introduction

Table of contents

Open Table of contents

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, enabling seamless resource creation and management.

Let’s begin by configuring the provider.

terraform {
  required_providers {
    influxdb = {
      source  = "komminarlabs/influxdb"
      version = "1.0.0"
    }
  }
}
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 provider offers various data sources and resources.

Data Sources:

Resources:

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.

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.

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).

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

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.

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"
      }
  }]
}

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.

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
}

Additional Resources

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 them promptly.


Previous Post
How to bridge a Terraform Provider to Pulumi
Next Post
Effortless Infrastructure - Mastering Automated Deployments with Terraform and GitHub Actions