# Terraform Provider

Hatchet's [terraform provider](https://registry.terraform.io/providers/hatchet-dev/hatchet/latest/docs) allows you to automate administrative tasks on your tenants.

## Creating a management token

The first step is creating a management token. Unlike API tokens, management tokens are not bound to a tenant but rather an organization.
Because they can take administrative actions on tenants, you can also specify [tags](/v1/user-groups#tag-syncing), which limit which tenants a management token can affect.
A management token with tags can only act on tenants whose tags are a subset of those on the token. It cannot access untagged tenants or manage user groups, and it must set at least one tag on any tenant it creates.

<figure style={{ margin: "2rem auto", maxWidth: "100%", textAlign: "center" }}>
  <img
    src="/terraform/management_token_1.png"
    alt="Management tokens settings page"
    style={{ width: "100%", height: "auto", borderRadius: "8px" }}
  />
</figure>

<figure style={{ margin: "2rem auto", maxWidth: "50%", textAlign: "center" }}>
  <img
    src="/terraform/management_token_2.png"
    alt="Management token creation modal"
    style={{ width: "100%", height: "auto", borderRadius: "8px" }}
  />
</figure>

<figure style={{ margin: "2rem auto", maxWidth: "50%", textAlign: "center" }}>
  <img
    src="/terraform/management_token_3.png"
    alt="Newly created management token"
    style={{ width: "100%", height: "auto", borderRadius: "8px" }}
  />
</figure>

## Examples

### Configure the provider

```bash
export HATCHET_CLOUD_MANAGEMENT_TOKEN="<your-management-token>"
```

```hcl
terraform {
  required_providers {
    hatchet = {
      source  = "hatchet-dev/hatchet"
      version = "~> 1.1"
    }
  }
}

provider "hatchet" {
  # The token will either come from the HATCHET_CLOUD_MANAGEMENT_TOKEN env var, or it can be manually supplied here.
  # We recommend using the env var in production.
  # token = "eyJhbGciOiJFUzI1NiIs..."
}
```

### Create a tenant

> **Warning:** Changing a tenant's `name`, `slug`, or `region` deletes the tenant and creates
>   a new one. Changing `tags` does the same if `slug` or `region` is not set. Set
>   `prevent_destroy` to block this, and review `terraform plan` before applying.

```hcl
resource "hatchet_tenant" "production" {
  name   = "Production"
  slug   = "acme-production"
  region = "aws:us-west-2"

  lifecycle {
    prevent_destroy = true
  }
}
```

### Create a tenant API token

> **Warning:** Each later `terraform apply` revokes this token and creates a new one. Update
>   any workers that use it with the new value.

```hcl
resource "hatchet_tenant_api_token" "worker" {
  tenant_id  = hatchet_tenant.production.id
  name       = "worker"
  expires_at = "720h"
}

output "worker_token" {
  value     = hatchet_tenant_api_token.worker.token
  sensitive = true
}
```

### Invite an organization member

```hcl
resource "hatchet_organization_member" "jane" {
  email = "jane@example.com"
  role  = "MEMBER"
}
```

### Create a user group

```hcl
resource "hatchet_user_group" "platform_admins" {
  name = "platform-admins"
  role = "ADMIN"
  tags = ["platform"]
}
```
