# Terraform

Manage Envshed organizations, projects, environments, members, service tokens, and secrets the same way you manage the rest of your infra — declared in HCL, applied by `terraform apply`, reviewed in pull requests.

:::note Planned
The Terraform provider (`envshed/envshed`) is planned for the public registry. Until then, the [REST API workaround](#workaround-today) is enough to bootstrap projects and rotate service tokens from a Terraform pipeline.
:::

## How it will work

- **`envshed_project`, `envshed_environment`, `envshed_secret`, `envshed_service_token`** — first-class resources that map 1:1 to the dashboard.
- **State is the source of truth** — drift between Envshed and your `.tf` files surfaces in `terraform plan`. No more "did someone rotate that secret in the UI?" surprises.
- **Plan-safe secret values** — `envshed_secret.value` is treated as sensitive: never printed in `plan` output, stored encrypted in remote state.
- **Read-only data sources** — `data.envshed_secret` and `data.envshed_project` so you can pull values into other providers (Vercel, AWS) without writing them back.

## Workaround today

Two patterns work today, depending on whether you want full lifecycle control or just one-shot bootstrap.

### Option 1 — Terraform `http` provider for one-shot bootstrap

Use the [`http` data source](https://registry.terraform.io/providers/hashicorp/http/latest/docs) with the [Envshed REST API](../cli/pull) to read secrets at plan time and feed them into other providers (for example, Vercel or AWS Secrets Manager).

```hcl
data "http" "database_url" {
  url = "https://app.envshed.com/api/v1/secrets/my-org/my-project/production/DATABASE_URL"

  request_headers = {
    Authorization = "Bearer ${var.envshed_token}"
  }
}

resource "vercel_project_environment_variable" "db_url" {
  project_id = vercel_project.app.id
  key        = "DATABASE_URL"
  value      = jsondecode(data.http.database_url.response_body).value
  target     = ["production"]
  sensitive  = true
}
```

This pattern is read-only — Envshed remains the source of truth for the secret value, Terraform just propagates it into the provider that needs it.

### Option 2 — `null_resource` + `local-exec` for write paths

For provisioning service tokens or rotating secrets from a Terraform run, wrap the [CLI](../cli/overview) in a `null_resource`:

```hcl
resource "null_resource" "rotate_db_url" {
  triggers = { value_sha = sha256(var.new_db_url) }

  provisioner "local-exec" {
    command     = "echo -n \"$NEW_VALUE\" | envshed secret set DATABASE_URL -e production"
    environment = {
      ENVSHED_TOKEN = var.envshed_token
      NEW_VALUE     = var.new_db_url
    }
  }
}
```

`envshed secret set` reads the value from stdin to keep it out of shell history and `terraform plan` output. This pattern is fine for bootstrap and rotation but does not give you drift detection — that gap is what the native provider closes.

## Want this sooner?

The Terraform provider is planned on the [public roadmap](https://envshed.com/roadmap). If managing Envshed as code would unblock your team, email [contact@envshed.com](mailto:contact@envshed.com?subject=Terraform%20provider%20%2B1) with a line about which resources you would manage first: that drives which resources we ship in v0.

## Related

- [Service tokens](../features/service-tokens) — what the provider will create
- [Vercel](./vercel), [Netlify](./netlify) — common downstream providers for the workaround
- [Public roadmap](https://envshed.com/roadmap)
