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

# Terraform Provider

> Set up the Runlayer Terraform provider and manage Runlayer resources from Terraform.

## Beta access

The Runlayer Terraform provider is currently in beta.

If you need help — private network access when your Runlayer API is not public, or the right base URL for your environment:

* Reach out to us in Slack
* Or email [support@runlayer.com](mailto:support@runlayer.com)

## Full schema reference

In your Runlayer app, open **Help**, then select **Terraform Reference**.

The reference includes provider configuration, resource and data-source schemas, nested fields, examples, and resource import instructions.
Each page shows the provider version it documents. Select the version to open its entry in the reference's **Changelog** page, which lists notable resource and data-source changes by provider release.

## What you need

* Terraform CLI 1.0 or later (OpenTofu works too)
* Network access to your Runlayer API
* A Runlayer user API key

The provider installs from Runlayer's registry at `downloads.runlayer.com` with a plain `terraform init` — no credentials, no manual downloads.

## Setup

**1. Get private access first**

If your Runlayer API is only reachable over VPN or another private network, make sure that is working before you start Terraform.

**2. Declare the provider**

```hcl theme={null}
terraform {
  required_providers {
    runlayer = {
      source  = "downloads.runlayer.com/runlayer/runlayer"
      version = "~> 1.8, >= 1.8.2"
    }
  }
}

provider "runlayer" {}
```

> [!NOTE]
>
> Pin 1.8.1 or later to use the resources added in 1.8: the detection, smart rule, team member and team group binding resources, and the new `runlayer_agent` attributes. 1.8.0 was never published to the registry. If your backend runs 1.28.1227 through 1.28.1287 and uses the detection resources, pin 1.8.2 or later: 1.8.0 and 1.8.1 wrongly report the `detections` feature as off on those backends.

**3. Export your Runlayer credentials**

```bash theme={null}
export RUNLAYER_API_KEY="rl_..."
export RUNLAYER_BASE_URL="https://your-runlayer-base-url"
```

> [!NOTE]
>
> Use a user API key here.
>
> Organization API keys are not sufficient for Terraform resource management endpoints such as policy creation.

> [!NOTE]
>
> `RUNLAYER_BASE_URL` should be the API base URL for your environment. If you are not sure which URL to use, ask us.

**4. Optional: set the base URL in the provider block**

If you prefer explicit provider configuration instead of environment variables, use the provider's `base_url` and `api_key` arguments:

```hcl theme={null}
variable "runlayer_base_url" {
  type = string
}

variable "runlayer_api_key" {
  type      = string
  sensitive = true
}

provider "runlayer" {
  base_url = var.runlayer_base_url
  api_key  = var.runlayer_api_key
}
```

If you use this form, the variable name can be `runlayer_base_url`, but the provider argument itself is `base_url`.

**5. Run Terraform**

```bash theme={null}
terraform init
terraform plan
```

`terraform init` downloads the provider from the Runlayer registry and verifies its checksum and GPG signature.

**6. Verify the setup**

A good first check is:

* `terraform init` installs the provider and writes it to `.terraform.lock.hcl`
* once you add a real resource, `terraform plan` reaches the Runlayer API instead of failing on auth, host, or network setup

## Using variables for users, groups, and roles

If you want stable references in Terraform, declare variables like this:

```hcl theme={null}
variable "users" {
  type = map(string)
}

variable "groups" {
  type = map(string)
}

variable "roles" {
  type = map(string)
}
```

Then use them in resources:

```hcl theme={null}
principal = {
  type = "group"
  ids  = [var.groups.engineers]
}
```

## How the CLI can help

The CLI includes `uvx runlayer terraform export` to generate a tfvars file with stable names for users, groups, and roles.

Example:

```bash theme={null}
uvx runlayer terraform export --output runlayer.auto.tfvars
```

Example output:

```hcl theme={null}
users = {
  marcin_at_runlayer_com = "11111111-1111-1111-1111-111111111111"
}

groups = {
  engineers = "22222222-2222-2222-2222-222222222222"
}

roles = {
  admin = "33333333-3333-3333-3333-333333333333"
}
```

That file can be loaded by Terraform and referenced like:

```hcl theme={null}
var.users.marcin_at_runlayer_com
var.groups.engineers
var.roles.admin
```

This is useful when one person refreshes IDs occasionally and the rest of the Terraform code stays readable.

## Resources

The provider manages the resources below. Anything not listed here is still managed in the app or the API.

| Resource | Manages |
| - | - |
| `runlayer_server` | MCP server registrations (transport, auth fields) |
| `runlayer_policy` | Access policies (principals, scope, conditions) |
| `runlayer_policy_attribute_setting` | Org attributes that policies can reference |
| `runlayer_server_security_setting` | Per-server scanner/violation settings |
| `runlayer_security_scanner_settings` | Org-wide scanner defaults, a singleton that servers, clients and local tools inherit from. Unset attributes keep their stored value, so reset a scan budget to inherit in the UI. A label-action map is managed whole: labels left out of it are removed. Destroy only drops it from state, like `runlayer_ai_watch_enforce_settings`. Needs backend 1.28.1220 or later |
| `runlayer_ai_watch_enforce_settings` | AI Watch enforcement settings, a singleton (blocked tool types, built-in allowlist mode) |
| `runlayer_ai_watch_enforce_entry` | One AI Watch Enforce allowlist or Protect denylist entry (remote URL, stdio package or built-in); immutable, so a change replaces it |
| `runlayer_ai_watch_user_scoped_policy` | A user's AI Watch allow or deny policy for one scope |
| `runlayer_ai_watch_user_exemption` | Exempts one user from AI Watch blocking. Create turns blocking off for the user and destroy turns it back on. Needs the manage-security-policies capability |
| `runlayer_server_tool_security_setting` | One tool's scanner overrides on a server (unset fields inherit the server's settings) |
| `runlayer_detection_rule_setting` | One detection rule's settings (action, severity, risk categories, block message, local tools, scope), keyed by rule name, e.g. `lethal_trifecta`. An unset `scope` or `include_local_tools` keeps the stored value. Destroy resets the rule to its defaults. Needs the `detections` feature and backend 1.28.1238 or later; see the note below for `scope` and `include_local_tools` |
| `runlayer_detection_outbound_entry` | One Outbound list entry: a server, and optionally one of its tools (unset means every tool). Immutable, so a change replaces it. Needs the `detections` feature and backend 1.28.1227 or later |
| `runlayer_smart_rule` | One smart rule (name, instruction, threshold, scope, enabled). Unset `threshold`, `scope` and `enabled` keep their stored values. `scope` is returned in the order `prompts`, `responses`, `reasoning`, `tool_inputs`, `tool_outputs`; list it in that order. Needs the `smart-rules` feature and backend 1.28.1236 or later |
| `runlayer_smart_rule_test_case` | One saved test case for a smart rule (part, text, expected match, label); changing `rule_id` replaces it. Needs the `smart-rules` feature and backend 1.28.1236 or later |
| `runlayer_deployment` | Hosted MCP deployments, authored as YAML |
| `runlayer_agent_account` | Machine accounts with a one-time client secret |
| `runlayer_role_assignment` | Manual role grants (IdP-synced grants stay with the IdP) |
| `runlayer_agent` | Agents (prompt, model, connectors, skills, built-in tools, tags) |
| `runlayer_agent_schedule` | An agent's cron schedules (prompt, cron, timezone, active hours) |
| `runlayer_plugin` | Plugins (connectors with tool selections, skills) |
| `runlayer_skill` | Skills: name, description, tags and the complete file set (`files`, each a `title` and `content`). Reads return files sorted by title and tags sorted by name, case-insensitively; list them in that order, and spell each tag the way your organization's existing tag is spelled. `namespace` and `path` are set on create, so changing either replaces the skill, and the replace fails while a plugin uses the skill. Detaching the skill from its source in the app clears `namespace`: remove `namespace` from the config before the next apply, or the apply replaces the skill. Unset `files`, `tags`, `is_public`, `listed_in_workspace` and `is_verified` keep their stored values; setting `is_verified` needs the manage-skills capability. Every file change is scanned first, and a file set your skill risk policy blocks fails the apply without saving anything. Needs backend 1.28.1906 or later |
| `runlayer_auto_sync_item` | Org-wide device sync for one server, plugin or skill, keyed by `entity_type` and `entity_id`. `team_ids` limits a server's sync to members of those teams; `[]` syncs it to the whole organization. Destroy stops a server or plugin syncing and resets a skill to the org default. Needs backend 1.28.1693 or later |
| `runlayer_team` | Teams (name, icon, default share permission) |
| `runlayer_team_member` | One user's membership in a team, with their role (`owner` or `member`). Applying adopts an existing membership; destroy removes it. Needs backend 1.28.1233 or later |
| `runlayer_team_group_binding` | One group bound to a team, which grants its members access. A new group replaces the binding; destroy unbinds it and removes the members only that group granted. Needs backend 1.28.1237 or later |
| `runlayer_organization` | Organization settings, a singleton for the API key's organization |

> [!WARNING]
>
> Manage each identity with one `runlayer_ai_watch_enforce_entry` resource. An identity sits in one list at a time, so an allow resource and a deny resource for the same identity replace each other on every apply, and destroying the one applied last deletes the entry.

> [!NOTE]
>
> The detection and smart rule resources need the `detections` or `smart-rules` feature on your organization; contact Runlayer to enable it. While the feature is off, plan and apply fail with an error naming it, and Terraform keeps these resources in state, so turning the feature back on loses nothing.

> [!NOTE]
>
> In `runlayer_detection_rule_setting`, setting `scope` needs backend 1.28.1537 or later, and setting `include_local_tools` needs 1.28.1581 or later. On older backends, a plan that sets either fails with an error naming the version; leave them unset there. On backends 1.28.1438 to 1.28.1536, an apply that changes the rule also resets its scope to all.

> [!NOTE]
>
> Two `runlayer_detection_outbound_entry` resources for the same server and tool share one entry: the second adopts the first one's entry, and destroying either deletes it. Manage each entry with one resource.

> [!NOTE]
>
> `runlayer_auto_sync_item` imports by `<entity_type>/<entity_id>`, and an apply without import also adopts the existing item. `sync_enabled = false` is accepted only for skills, where it turns sync off org-wide; stop a server or plugin syncing by destroying its resource. `team_ids` applies to servers only, and an unset `team_ids` keeps the stored scope.

> [!NOTE]
>
> `runlayer_team_member` and `runlayer_team_group_binding` have no import: declare the resource and apply, and it adopts the existing membership or binding. Removing a member fails while a group bound to the team still grants them membership; unbind the group first. A team keeps at least one owner, and its creator stays an owner.

> [!NOTE]
>
> Moving an identity to the other list (in the app or the API) keeps its entry ID. Before the provider reads or deletes an entry, it checks which list the entry is in. If the entry has moved, the provider treats it as gone: it leaves the moved entry alone and plans to create the resource again in its own list. `runlayer_ai_watch_enforce_entry` needs Runlayer 1.28.1171 or later. Runlayer releases that include the `list` check on entry reads and deletes do both in a single step. On earlier releases these are two requests, so a move that lands between them can still delete the moved entry.

### Importing existing resources

Most resources can be imported with `terraform import`. The ID to pass depends on the resource:

| ID | Resources |
| - | - |
| The object's ID | `runlayer_server`, `runlayer_policy`, `runlayer_policy_attribute_setting`, `runlayer_ai_watch_enforce_entry`, `runlayer_detection_outbound_entry`, `runlayer_smart_rule`, `runlayer_deployment`, `runlayer_agent_account`, `runlayer_agent`, `runlayer_plugin`, `runlayer_skill`, `runlayer_team` |
| `<server_id>` | `runlayer_server_security_setting` |
| `<user_id>` | `runlayer_ai_watch_user_exemption` |
| `<user_id>/<policy_id>` | `runlayer_ai_watch_user_scoped_policy` |
| `<user_id>/<role_id>` | `runlayer_role_assignment` |
| `<agent_id>/<schedule_id>` | `runlayer_agent_schedule` |
| `<rule_id>/<case_id>` | `runlayer_smart_rule_test_case` |
| `<entity_type>/<entity_id>`, e.g. `server/<server_id>` | `runlayer_auto_sync_item` |
| The rule name, e.g. `lethal_trifecta` | `runlayer_detection_rule_setting` |

```bash theme={null}
terraform import runlayer_detection_rule_setting.lethal_trifecta lethal_trifecta
```

The rest have no import. `runlayer_team_member` and `runlayer_team_group_binding` adopt an existing membership or binding on apply. The singletons (`runlayer_organization`, `runlayer_security_scanner_settings`, `runlayer_ai_watch_enforce_settings`) and `runlayer_server_tool_security_setting` write their settings on apply, so declare them with the values you want.

To adopt many objects at once and let Terraform write their configuration, see [Adopt Existing Resources](https://docs.runlayer.com/terraform-provider-adoption.md).

### Data sources

Every data source reads one existing object:

* Servers and policies: `runlayer_server`, `runlayer_server_security_setting`, `runlayer_server_tool_security_setting`, `runlayer_policy`, `runlayer_policy_attribute_setting`, `runlayer_deployment`
* Security: `runlayer_security_scanner_settings`, `runlayer_ai_watch_enforce_settings`, `runlayer_ai_watch_enforce_entry`, `runlayer_ai_watch_user_scoped_policy`, `runlayer_detection_rule_setting`, `runlayer_detection_outbound_entry`, `runlayer_smart_rule`, `runlayer_smart_rule_test_case`
* Agents, plugins and skills: `runlayer_agent`, `runlayer_agent_account`, `runlayer_agent_schedule`, `runlayer_plugin`, `runlayer_skill`, `runlayer_auto_sync_item`
* People and organization: `runlayer_user`, `runlayer_group`, `runlayer_role`, `runlayer_team`, `runlayer_team_member`, `runlayer_team_group_binding`, `runlayer_organization`

### Deployment example

```hcl theme={null}
resource "runlayer_deployment" "internal_tools" {
  name         = "internal-tools"
  yaml_content = templatefile("${path.module}/runlayer.yaml.tftpl", {
    image_digest = var.image_digest
  })
}
```

The YAML has the same shape as `runlayer.yaml`. Build and push the image in CI, then stamp the digest into the YAML.

### Agent account example

```hcl theme={null}
resource "runlayer_agent_account" "ci_bot" {
  name        = "ci-bot"
  description = "Runs the nightly evals"
}

output "ci_bot_secret" {
  value     = runlayer_agent_account.ci_bot.client_secret
  sensitive = true
}
```

> [!WARNING]
>
> `client_secret` is returned once, on create, and can never be read again. It stays in Terraform state (treat state as a secret); an account imported after creation has no secret — rotate credentials in the app if you need one.

### Agent example

```hcl theme={null}
resource "runlayer_agent" "support_triage" {
  name   = "support-triage"
  prompt = file("${path.module}/prompts/support-triage.md")
  tags   = ["support", "managed-by-terraform"]

  model = "claude-sonnet-5"

  servers = [{
    server_id  = runlayer_server.linear.id
    tool_names = ["search_issues", "get_issue"]
  }]

  skill_ids = [var.triage_skill_id]

  env = {
    TRIAGE_QUEUE = "support"
  }
}
```

> [!NOTE]
>
> Some attributes are request-only: Terraform sends `servers`, `skill_ids`, and `env` on create and update but does not refresh them from the API, because the API returns connectors in a different shape and masks `env` values. Changes made in the app to those three are not detected as drift. Use the computed `server_count`, `tool_count`, `skill_count`, and `env_keys` for read-side checks.

> [!NOTE]
>
> `description` is generated by Runlayer from the prompt and is read-only in Terraform, as are `harness` and the usage limits (`run_limits_by_model`, `daily_cost_limit_usd`, `weekly_cost_limit_usd`). `tags` are returned sorted, so list them sorted to avoid a permanent diff.

> [!NOTE]
>
> `open_to_collaboration` lets the agent work with agents that act for other people in your workspace. It needs backend 1.28.1590 or later. If you leave it out of config, Terraform keeps the value set in the app. The read-only `my_last_used_at` and `my_run_count` show your own last run and your run count over the last 30 days, for the user whose API key Terraform uses. They need backend 1.28.1543 or later.

## Upgrading from 0.10

Provider 1.0 is a breaking release:

* **Policy `value` fields must be valid JSON.** Wrap plain strings with `jsonencode`: `value = "delete"` becomes `value = jsonencode("delete")`. Existing policy states need a re-import: `terraform state rm` the policy, update the config, then `terraform import`.
* **Minimum backend version: 2026-08-19.** Ask us if you are unsure what your environment runs.
* Registering a deployment's connector on first apply requires a user API key (organization keys cannot register connectors yet).

## Policy examples

These examples are based on the provider e2e coverage and show common policy shapes.

### Allow one group full access to one server

```hcl theme={null}
resource "runlayer_policy" "allow_engineers_linear" {
  action = "allow"

  principal = {
    type = "group"
    ids  = [var.groups.engineers]
  }

  scope = {
    servers   = [runlayer_server.linear.id]
    tools     = ["*"]
    resources = ["*"]
  }

  description = "Engineers: full Linear access"
}
```

### Allow one group read-only access to a server

```hcl theme={null}
resource "runlayer_policy" "allow_contractors_deepwiki_readonly" {
  action = "allow"

  principal = {
    type = "group"
    ids  = [var.groups.contractors]
  }

  scope = {
    servers   = [runlayer_server.deepwiki.id]
    tools     = ["search_documentation", "get_library_docs", "resolve_library_id"]
    resources = ["*"]
  }

  description = "Contractors: read-only DeepWiki"
}
```

### Deny one group access to one server

```hcl theme={null}
resource "runlayer_policy" "deny_contractors_linear" {
  action = "deny"

  principal = {
    type = "group"
    ids  = [var.groups.contractors]
  }

  scope = {
    servers   = [runlayer_server.linear.id]
    tools     = ["*"]
    resources = ["*"]
  }

  description = "Contractors: no Linear access"
}
```

### Deny tools by name prefix

```hcl theme={null}
resource "runlayer_policy" "deny_destructive_tools" {
  action = "deny"

  principal = {
    type = "any"
  }

  scope = {
    servers   = [runlayer_server.linear.id]
    tools     = ["*"]
    resources = ["*"]
  }

  conditions = [{
    rules = [{
      field      = "tool_name"
      operator   = "begins_with"
      value      = jsonencode("delete")
      report_raw = false
    }]
  }]

  description = "Deny any tool starting with delete"
}
```

### Deny access outside a private IP range

```hcl theme={null}
resource "runlayer_policy" "deny_internal_outside_vpn" {
  action = "deny"

  principal = {
    type = "any"
  }

  scope = {
    servers   = [runlayer_server.internal_tools.id]
    tools     = ["*"]
    resources = ["*"]
  }

  conditions = [{
    rules = [{
      field      = "meta.request.ip"
      operator   = "not_ip_range"
      value      = jsonencode("10.0.0.0/8")
      report_raw = true
    }]
  }]

  description = "Deny internal tools access from outside VPN"
}
```

### Deny agent accounts without a production attribute

```hcl theme={null}
resource "runlayer_policy" "deny_nonprod_agents" {
  action = "deny"

  principal = {
    type = "any"
  }

  scope = {
    servers   = ["*"]
    tools     = ["*"]
    resources = ["*"]
  }

  # Rules within a group are ANDed: the subject.type guard scopes the
  # negative attribute rule to agent traffic only.
  conditions = [{
    rules = [
      {
        field      = "meta.subject.type"
        operator   = "equals"
        value      = jsonencode("agent")
        report_raw = false
      },
      {
        field      = "meta.actor.attributes.environment"
        operator   = "not_equals"
        value      = jsonencode("production")
        report_raw = false
      }
    ]
  }]

  description = "Global: agent accounts must carry environment=production"
}
```

### Deny everything except a small read-only tool set

```hcl theme={null}
resource "runlayer_policy" "deny_contractors_internal_writes" {
  action = "deny"

  principal = {
    type = "group"
    ids  = [var.groups.contractors]
  }

  scope = {
    servers   = [runlayer_server.internal_tools.id]
    tools     = ["*"]
    resources = ["*"]
  }

  conditions = [{
    rules = [
      {
        field      = "tool_name"
        operator   = "not_equals"
        value      = jsonencode("read_data")
        report_raw = false
      },
      {
        field      = "tool_name"
        operator   = "not_equals"
        value      = jsonencode("list_items")
        report_raw = false
      },
    ]
  }]

  description = "Contractors: deny internal tools except read_data and list_items"
}
```

## Troubleshooting

* `terraform init` cannot find the provider: check the `source` is exactly `downloads.runlayer.com/runlayer/runlayer` and that outbound HTTPS to `downloads.runlayer.com` is allowed
* Terraform still uses an old manual install: remove any `dev_overrides` block for the provider from your Terraform CLI config (`~/.terraformrc` or `TF_CLI_CONFIG_FILE`), then re-run `terraform init`
* `terraform apply` returns `403 Forbidden`: make sure you are using a user API key, not an organization API key
* API calls fail: verify `RUNLAYER_API_KEY`
* API calls hit the wrong environment: verify `RUNLAYER_BASE_URL`
* Requests time out or refuse to connect: confirm private network access first


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