> ## Documentation Index
> Fetch the complete documentation index at: https://paper.brimble.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Manage Brimble with Terraform

> Create and change services, databases, buckets, environments and their settings from Terraform, with plans you can review before anything changes.

The Brimble provider for [Terraform](https://developer.hashicorp.com/terraform) manages your workspace from `.tf` files: services, databases, buckets, environment variables, domains, environments, volumes, autoscaling, alerts, log drains, networking, rate limits and config files. `terraform plan` shows what will change, and `terraform apply` makes the change through Brimble's API.

## Create an API key

1. In the dashboard, switch to the workspace you want to manage and open **Settings → API keys**.
2. Create a key with the **Infrastructure as code** scope (`iac.manage`), plus the scopes for what you'll manage:

| Resources | Scopes |
| - | - |
| Environments | `environment.*` |
| Services, databases, database users, alert rules, config files | `project.*` |
| Environment variables | `env.*` |
| Buckets, bucket credentials, bucket links | `storage.*` |
| Domains | `domain.*` |
| Volumes | `volume.*` |
| Autoscaling groups | `autoscaling.*` |
| Log drains | `drain.*` |
| Networking | `networking.*` |
| Rate limits | `ratelimit.*` |

The provider checks for `iac.manage` before planning and stops with a clear error if the key doesn't have it.

> [!WARNING]
>
> Treat the key like a password. Pass it as a variable or the `BRIMBLE_API_KEY` environment variable, and never commit it.

## Configure the provider

```hcl theme={null}
terraform {
  required_providers {
    brimble = {
      source  = "brimblehq/brimble"
      version = "~> 0.1"
    }
  }
}

provider "brimble" {
  api_key = var.brimble_api_key # or BRIMBLE_API_KEY
}

variable "brimble_api_key" {
  type      = string
  sensitive = true
}
```

If the key can reach more than one workspace, set `team_id` (or `BRIMBLE_TEAM_ID`) to the workspace's ID.

## A first configuration

An environment with a service, a Postgres database, and a domain:

```hcl theme={null}
data "brimble_regions" "all" {}

locals {
  web_region      = [for region in data.brimble_regions.all.items : region.id if region.type == "web" && region.available][0]
  database_region = [for region in data.brimble_regions.all.items : region.id if region.type == "database" && region.available][0]
}

resource "brimble_environment" "production" {
  name = "Production"
}

resource "brimble_database" "db" {
  name           = "app-db"
  environment_id = brimble_environment.production.id
  region         = local.database_region
  engine         = "postgresql"
  version        = "17"
}

resource "brimble_service" "web" {
  name           = "web"
  environment_id = brimble_environment.production.id
  region         = local.web_region
  image          = "ghcr.io/acme/web:1.4.0"
  port           = 3000
}

resource "brimble_env_var" "database_url" {
  project_id = brimble_service.web.id
  name       = "DATABASE_URL"
  value      = brimble_database.db.connection_string
}

resource "brimble_domain" "www" {
  project_id = brimble_service.web.id
  name       = "www.example.com"
}

output "dns_records" {
  value = brimble_domain.www.dns_records
}
```

Run `terraform init`, then `terraform plan` to review, then `terraform apply`. `dns_records` lists the records to add at your DNS provider.

Each location has separate regions for services (`web`), databases (`database`) and sandboxes, and a region can be temporarily unavailable for new resources. Pick the region by `type` and `available`, as above, or copy an `id` from the `brimble_regions` data source. A new database usually takes a few minutes to provision, and `terraform apply` waits for it.

`engine` and `version` come from the `brimble_database_engines` data source. The engine name isn't case-sensitive, so `postgresql` and `PostgreSQL` are the same engine.

To deploy from a repository instead of an image, replace `image` with a `repository` block (`git`, `name`, `branch`, and `installation_id` for GitHub). Every resource, its settings, and its import format are documented on the [Terraform Registry](https://registry.terraform.io/providers/brimblehq/brimble/latest/docs).

## How changes deploy

`terraform apply` deploys a service only when a change needs a new deployment, such as the image or branch, build and start commands, port, compute, region, Dockerfile, root directory, health check path, cron, autoscaling group or volumes. Other settings, such as maintenance mode, preview deployments, password protection, alerting and networking, save without a deploy. A repository service deploys the latest commit on its branch, as **Redeploy** does.

Set `deploy_on_change = false` on a service to save every change without deploying, for example when you deploy only from git pushes. Volume changes still deploy, because Brimble can't attach or resize a volume without one.

## Resources managed by Terraform

When Terraform creates or imports a service, database or bucket, Brimble marks it as managed by infrastructure as code:

* Services and databases show a **Managed by IaC** badge, and their settings are read-only in the dashboard, so dashboard edits can't drift from your configuration.
* [`brimble.toml`](https://paper.brimble.io/config-as-code/brimble-toml) plans skip them, with the reason **Managed by IaC**.

Manage each resource with one tool. To move a resource out of Terraform:

1. Remove it from your state: `terraform state rm brimble_service.web`.
2. Release it with the API, using a key with `iac.manage`: `DELETE https://core.brimble.io/v1/projects/{id}/iac` for a service or database, or `DELETE https://core.brimble.io/v1/storage/buckets/{id}/iac` for a bucket.

## Import existing resources

Bring resources you created in the dashboard under Terraform by the names you see there:

```bash theme={null}
terraform import brimble_service.web web
terraform import brimble_database.db app-db
terraform import brimble_env_var.database_url web/production/DATABASE_URL
```

Or with an `import` block:

```hcl theme={null}
import {
  to = brimble_service.web
  id = "web"
}
```

A service or database name only has to be unique within an environment. If more than one environment has a service called `web`, Terraform tells you which, and you pick one with `staging/web`. Resources inside a service use the service name first, such as `web/High CPU` for an alert rule or `web/nginx.conf` for a config file. Each resource's page on the [Terraform Registry](https://registry.terraform.io/providers/brimblehq/brimble/latest/docs) shows its format.

Importing marks services, databases and buckets as managed by IaC.

## Terraform or brimble.toml?

| | Terraform | brimble.toml |
| - | - | - |
| Lives in | Your infrastructure repository | The service's own repository |
| Applies | When you run `terraform apply` | On every push |
| Covers | Everything in a workspace, across projects and environments | The services, databases and buckets in one repository |
| Review | `terraform plan` | Approval in the dashboard for risky changes |
| Secrets | Terraform variables and your state backend | Set in the dashboard, listed by name in the file |

Use Terraform when one team manages many projects, environments and shared resources together, or already runs Terraform for the rest of its infrastructure. Use brimble.toml when each repository should describe how its own service runs. Don't manage the same resource with both.

## Limits

* **Write-only values:**
  * Log drain header values, S3 keys for drains, and a service's `password` are sent to Brimble but never returned.
  * Terraform can't detect changes made to them outside Terraform; it does detect password protection being turned off.
* **Database user passwords:** changing `password` replaces the user, since rotating a password in place needs a 2FA confirmation.
* **Alert rules need a notification channel:** Brimble refuses an alert rule until the workspace can notify someone: email notifications not muted for the account, or a webhook URL in the workspace's notification settings.
* **Plan-gated settings:** maintenance mode, password protection and rate limits need a plan that includes them, also to turn them off or destroy them.
* **Secrets in state:** connection strings, bucket secrets and generated passwords are stored in your Terraform state. Use a state backend that encrypts at rest and limits access.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.