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

# Terraform Provider

<Note>
  The Bytebase Provider itself is free to use. Some advanced resource operations require Pro or Enterprise Plan.

  Bytebase Terraform Provider handles control plane configuration such as settings, policies, access controls.
  It does not handle data plane operations such as database creation, schema migrations, DML execution, query.
</Note>

Bytebase provides the [Terraform Provider](https://registry.terraform.io/providers/bytebase/bytebase) to let you manage your Bytebase resources via Terraform.

```plain theme={null}
Terraform Provider → Bytebase Configuration
                      ├── Instances, Projects & Environments
                      ├── Access Policies
                      └── Settings such as Approval Workflows

Bytebase UI/API → Database Operations
                   ├── Database Creation
                   ├── Schema Migrations (DDL)
                   ├── Data Changes (DML)
                   └── Query
```

## Create Service Account

The Terraform Provider uses [service account](/integrations/api/authentication/#service-account) to call the Bytebase API to manage the resources and settings.

After creating the service account with the `Workspace Admin` role, you can copy the service key as `service_key` and the email as `service_account` to initialize the Terraform provider in the next step.

<img src="https://mintcdn.com/dbx/hVml3kA8vNK6vcVL/content/docs/get-started/work-with-terraform/service-account.webp?fit=max&auto=format&n=hVml3kA8vNK6vcVL&q=85&s=5a50aee6e843d38ebbe70438cafd0c7a" alt="service-account" width="946" height="160" data-path="content/docs/get-started/work-with-terraform/service-account.webp" />

### Optional: Separate Accounts for Plan and Apply

If you prefer to use different service accounts for `terraform plan` and `terraform apply`, you can:

* **For `terraform apply`**:
  Assign the `Workspace Admin` role.

* **For `terraform plan`**:
  Create a **custom role** with only read permissions under **IAM & Admin > Custom Roles**, then assign it to the plan-only service account.

The read-only role should include the following permissions:

* All `.list` and `.get` permissions
* `bb.databases.export`
* `bb.databases.query`
* `bb.databases.getSchema`
* `bb.databases.getSecrets`
* `bb.auditLogs.export`
* `bb.auditLogs.search`
* `bb.issues.subscribe`
* `bb.projects.getIamPolicy`

## Tutorials

**Resources**

<Columns cols={3}>
  <Card horizontal icon="earth" title="Environment" href="/tutorials/manage-environments-with-terraform" />

  <Card horizontal icon="database" title="Database" href="/tutorials/manage-databases-with-terraform" />

  <Card horizontal icon="folder" title="Project" href="/tutorials/manage-projects-with-terraform" />
</Columns>

**Access Control**

<Columns cols={3}>
  <Card horizontal icon="users" title="User & Group" href="/tutorials/manage-users-and-groups-with-terraform" />

  <Card horizontal icon="lock" title="IAM Policy" href="/tutorials/manage-database-access-control-with-terraform" />

  <Card horizontal icon="eye-off" title="Data Masking" href="/tutorials/manage-database-access-control-with-terraform" />
</Columns>

**Workspace Settings**

<Columns cols={2}>
  <Card horizontal icon="settings" title="General & Approval Flow" href="/tutorials/manage-general-settings-with-terraform" />

  <Card horizontal icon="stamp" title="SQL Review Rules" href="/tutorials/manage-sql-review-rules-with-terraform" />
</Columns>

## Troubleshoot

### status: 404

This often happens when someone deletes the resources from the Bytebase UI and then attempts to
run `terraform apply`.

To recover, please run `terraform refresh` to refresh the state first.

<img src="https://mintcdn.com/dbx/hVml3kA8vNK6vcVL/content/docs/integrations/terraform/troubleshoot/404.webp?fit=max&auto=format&n=hVml3kA8vNK6vcVL&q=85&s=6f3f47a67f9b084615c703c9c7ad4052" alt="404" width="3104" height="1896" data-path="content/docs/integrations/terraform/troubleshoot/404.webp" />
