# Terraform provider

> Manage your workspace's flows, connections and alert policies with Terraform, sign in with an API key, import what already exists and read runs as data.

Source: https://docs.go-lumen.ai/api/terraform-provider

The Lumen Terraform provider lets you declare flows, connections and alert policies next to the rest of your infrastructure, review changes in a plan, and keep them in version control. It uses the same server-side apply as the CLI, so Terraform and `lumen apply` see the same resources.

-   Terraform 1.x
-   API key with Build scope

## Install the provider

The provider is a Go program, `terraform-provider-lumen`, with the source address `buji-code/lumen`.

> **Not on a public registry yet:**
> 
> Where the provider is distributed has not been decided. Until it is, build it from the repository and point Terraform at it with `dev_overrides`, which needs no `terraform init` for this provider.

```bash
go build -o ~/.terraform.d/lumen-dev/ ./cmd/terraform-provider-lumen
```

```hcl title="~/.terraformrc-lumen"
provider_installation {
  dev_overrides {
    "buji-code/lumen" = "/Users/you/.terraform.d/lumen-dev"
  }
  direct {}
}
```

```bash
export TF_CLI_CONFIG_FILE=~/.terraformrc-lumen
```

Then require it in your configuration:

```hcl title="versions.tf"
terraform {
  required_providers {
    lumen = {
      source = "buji-code/lumen"
    }
  }
}
```

## Configure it

Every setting is optional. Each falls back to an environment variable, then to the current context of the `lumen` CLI, token included.

| Setting | Environment variable | Default |
| --- | --- | --- |
| `server` | `LUMEN_SERVER` | The context's server. |
| `namespace` | `LUMEN_NAMESPACE` | The context's namespace. Your workspace's namespace is `tenant-` plus its name. |
| `token` | `LUMEN_TOKEN` | The token `lumen login` stored, refreshed when it expires. |
| `context` | `LUMEN_CONTEXT` | The current CLI context. |
| `config_path` | `LUMEN_CONFIG` | `~/.config/lumen/config.yaml`. |
| `field_manager` | `LUMEN_FIELD_MANAGER` | `lumen-terraform`. |
| `force_conflicts` |  | `false`. Set it to take over fields another manager owns. |

On your own machine, `provider "lumen" {}` is enough once you have run `lumen login` (see [Install the CLI](https://docs.go-lumen.ai/api/install-the-cli.md)). In automation, use an [API key](https://docs.go-lumen.ai/api/api-keys.md) with the **Build** scope, and name the workspace, because a key alone does not set the provider's namespace:

```hcl title="provider.tf"
variable "lumen_api_key" {
  type      = string
  sensitive = true
}

provider "lumen" {
  server    = "https://api.go-lumen.ai"
  namespace = "tenant-acme"
  token     = var.lumen_api_key
}
```

Or leave `token` out and export `LUMEN_TOKEN`, so the key never appears in your configuration.

## Declare a connection and a flow

```hcl title="main.tf"
resource "lumen_connector" "inbox" {
  name = "invoices-inbox"
  spec = {
    class_ref = { name = "email-forward" }
    config    = jsonencode({ allowedSenders = ["*@supplier.example"] })
  }
}

resource "lumen_flow" "pdfs" {
  name = "invoice-pdfs"
  spec = {
    display_name = "Invoice PDFs, merged"
    triggers = [
      {
        id            = "email"
        ref           = { name = "email-arrives" }
        connector_ref = { name = lumen_connector.inbox.name }
        config        = jsonencode({ attachments = "separate", requireAttachment = true })
        next          = ["pdfs"]
      },
      {
        id   = "manual"
        ref  = { name = "manual-run" }
        next = ["pdfs"]
      },
    ]
    nodes = [
      { id = "pdfs", step_ref = { name = "keep-pdfs" }, next = ["merge"] },
      { id = "merge", step_ref = { name = "merge-pdfs" }, next = ["keep"] },
      { id = "keep", step_ref = { name = "keep-in-lumen" } },
    ]
  }
}

# The address that starts the flow, as the server reports it.
output "forward_to" {
  value = jsondecode(lumen_connector.inbox.observed_spec).config.address
}
```

```bash
terraform plan
terraform apply
```

The field names are the resource's own in snake case: `displayName` is `display_name`, `stepRef` is `step_ref`. Run `lumen explain flow.spec.nodes --recursive` to see every field and what it means.

### How a spec maps to HCL

-   Objects are nested attributes, lists are lists, and maps are maps.
-   A field with no fixed shape, such as a step's or a trigger's `config`, is a JSON string. Write it with `jsonencode()`.
-   Required fields, allowed values, patterns and size limits are checked at plan time. Rules that need the whole resource are left to the server and reported when you apply.
-   Keys the server adds, such as the address of a forwarding connection, are not differences. `observed_spec` holds the spec as the server has it.
-   Changing a connection's class replaces it.

### Already have the YAML?

`lumen_manifest` applies any object from a YAML or JSON file. It is the way to keep the files you already wrote for [`lumen apply`](https://docs.go-lumen.ai/api/flows-as-yaml.md), and to manage kinds newer than the provider:

```hcl
resource "lumen_manifest" "invoice_pdfs" {
  manifest = file("${path.module}/flows/gitops-invoice-pdfs.yaml")
}
```

## What the provider has

| Kind | Terraform name |
| --- | --- |
| Flow | `lumen_flow` |
| Connection | `lumen_connector` |
| Alert policy | `lumen_alert_policy` |
| Step | `lumen_step` |
| Form | `lumen_form` |
| Sheet, database | `lumen_sheet`, `lumen_sheet_base` |
| Knowledge collection | `lumen_knowledge_collection` |
| Model class | `lumen_model_class` |
| Worker pool | `lumen_worker_pool` |
| Any object | `lumen_manifest` |
| Run, file, alert | `data.lumen_run`, `data.lumen_file`, `data.lumen_alert` (read only) |

The provider also has resources for cluster-wide kinds (`lumen_tenant`, `lumen_plan`, `lumen_region`, `lumen_cluster_step`, and similar). They are for whoever operates a Lumen installation, and a workspace key cannot write them.

Read a run as data:

```hcl
data "lumen_run" "last" {
  name = "7c1f0a5e-3b52-4d6e-9a41-0b2c7e8d1f34"
}
```

## Import what exists

Import by name, in the provider's namespace, or by `namespace/name`:

```bash
terraform import lumen_flow.pdfs invoice-pdfs
terraform import lumen_flow.pdfs tenant-acme/invoice-pdfs
terraform import lumen_manifest.probe 'lumen.ai/v1alpha1,Probe,tenant-acme/api'
```

The last form is a manifest's import id: `apiVersion,kind,namespace/name`.

## Good to know

-   **Writes are server-side applies as `lumen-terraform`.** A field that someone else owns (the console, the CLI, `kubectl`) is a conflict, unless you set `force_conflicts = true`.
-   **Terraform owns what it creates.** A flow made through Terraform is owned by Git, so the console shows it as **Managed in Git** and read only. A flow built in the console is owned by the console, and Terraform cannot change it. See [Flows as YAML](https://docs.go-lumen.ai/api/flows-as-yaml.md#who-owns-a-flow).
-   **Labels and annotations** in state are only the ones you set. The ones Lumen adds, such as the owner label, are not differences.
-   **Secrets** such as a connection's password do not belong in a plan or in state. Make those connections with `lumen connect` and refer to them, or keep the secret in your own secret store.

## Next steps

[API keysCreate the Build key your pipeline uses.](https://docs.go-lumen.ai/api/api-keys.md)[Flows as YAMLThe same resources as plain files, applied with the CLI.](https://docs.go-lumen.ai/api/flows-as-yaml.md)[Install the CLISign in once and let the provider reuse it.](https://docs.go-lumen.ai/api/install-the-cli.md)
