# Terraform

> Manage your workspace as code with the Underpass Terraform provider: auth keys, access rules, settings, DNS and machines.

The Underpass Terraform provider keeps your workspace's setup in code, next to the rest of your infrastructure: review changes in pull requests, and recreate a workspace from scratch.

## Set up

1. In the dashboard, open **Keys → API tokens** and create a token with **Read & write** scope. A token only reaches the workspace it was made in.
2. Give it to Terraform as `UNDERPASS_API_TOKEN` (or `api_token` in the provider block).
3. Add the provider:

```terraform
terraform {
  required_providers {
    underpass = {
      source  = "getunderpass/underpass"
      version = "~> 0.1"
    }
  }
}

provider "underpass" {}
```

If you run your own coordination service, set `api_url` (or `UNDERPASS_API_URL`).

## What it manages

| Resource | What |
|---|---|
| `underpass_auth_key` | An [auth key](/manage/auth-keys) for adding servers, containers and CI. Its secret is in `key`, marked sensitive. Keys can't be changed: changing an argument makes a new one, and destroying one revokes it. |
| `underpass_acl` | The [access rules](/features/access-controls), as one JSON document (`jsonencode()`). Saving is refused if a test in the policy fails. Destroying it goes back to no rules. |
| `underpass_settings` | Key expiry, device approval, network logs, automatic updates and [LAN games](/features/lan-games). Only the settings you set are managed. |
| `underpass_dns` | Nameservers, split DNS, search domains, the workspace's own name and HTTPS. It owns all the DNS settings: what isn't set is cleared. |
| `underpass_machine` | A machine already in the workspace: its name, key expiry, approval and the routes approved for it. Destroying it stops managing the machine, which stays. |

| Data source | What |
|---|---|
| `underpass_machines` | The workspace's machines, all or by tag. |
| `underpass_machine` | One machine, by name or ID. |

## Example: servers that join on their own

```terraform
resource "underpass_auth_key" "servers" {
  reusable      = true
  pre_approved  = true
  no_key_expiry = true
  tags          = ["tag:servers"]
}

resource "underpass_acl" "this" {
  policy = jsonencode({
    groups = { "group:engineering" = ["sara@acme.com", "tom@acme.com"] }
    rules = [
      { src = ["group:engineering"], dst = ["tag:servers"], proto = "tcp", ports = ["22", "443"] },
    ]
    tests = [
      { src = "user:tom@acme.com", accept = ["tag:servers:22"] },
    ]
  })
}
```

Pass `underpass_auth_key.servers.key` to your servers' setup (cloud-init, an image build) as `underpass up --auth-key`.

## Good to know

- **Access rules changed in the dashboard** show up as a change in the next plan, so Terraform can put them back. Rules saved by Terraform don't, even though Underpass gives each rule an ID and fills in defaults when it saves them.
- **What API tokens can't change** can't be managed here either: remote control, shared clipboard, file drops, wake, restart, AI agent and terminal settings, the remote control and clipboard rules, and people. See [API](/manage/api#what-tokens-cant-do).
- Import existing setup: `terraform import underpass_acl.this acl`, or `terraform import underpass_machine.web <machine ID>`.
