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

# Terraform

> Manage Formal infrastructure as code with the Terraform provider

## Overview

The [Formal Terraform Provider](https://registry.terraform.io/providers/formalco/formal/latest) enables you to manage Connectors, Resources, Policies, Users, and all other Formal objects as infrastructure-as-code.

## Installation

Add the provider to your Terraform configuration:

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
terraform {
  required_providers {
    formal = {
      source  = "formalco/formal"
      version = "~> 4.32"
    }
  }
}

provider "formal" {
  api_key = var.formal_api_key
  url     = "https://api.formal.ai" # optional
}
```

## Authentication

Create an API key in the [Control Plane](https://app.formal.ai/api-keys):

1. Navigate to [API Keys](https://app.formal.ai/api-keys)
2. Click **Create API Key**
3. Name the key (e.g., "terraform-production")
4. Copy the key immediately

Set the key as an environment variable or Terraform variable:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
export FORMAL_API_KEY="your-api-key-here"
```

Or in Terraform:

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
variable "formal_api_key" {
  description = "Formal API key"
  type        = string
  sensitive   = true
}
```

### OIDC

Instead of an API key, the provider can authenticate with a workload's OIDC token through an [OIDC integration](https://docs.formal.ai/docs/guides/integrations/oidc.md):

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
provider "formal" {
  oidc {
    audience = "oidc.formal.ai/<INTEGRATION_ID>"
    env      = "SPACELIFT_OIDC_TOKEN"
  }
}
```

## Keep Secrets Out of State

Set `retrieve_sensitive_values = false` on the provider. Terraform then doesn't store sensitive values that the API returns, such as Connector API keys and machine role access tokens.

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
provider "formal" {
  api_key                   = var.formal_api_key
  retrieve_sensitive_values = false
}
```

For secrets that you send to Formal, use write-only arguments. Terraform 1.11 and later sends them without storing them in state. Increment the matching `_version` argument to rotate the secret.

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
resource "formal_native_user_v3" "admin" {
  resource_id = formal_resource.db.id
  label       = "admin"

  basic {
    username = "app_admin"
    password {
      literal_wo         = var.db_admin_password
      literal_wo_version = 1
    }
  }
}
```

| Resource | Write-only argument |
| - | - |
| `formal_native_user_v3` | `literal_wo` |
| `formal_workflow_secret` | `value_wo` |
| `formal_resource_oauth_settings` | `client_secret_wo` |

A native user can also read its secret from an `environment_variable` on the Connector or from a `secret_manager`. The secret then stays out of Terraform.

## Examples

### Full Production Stack

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
terraform {
  required_providers {
    formal = {
      source  = "formalco/formal"
      version = "~> 4.32"
    }
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.17.0"
    }
  }
}

# Variables
variable "name" {
  description = "Base name for resources"
  type        = string
  default     = "formal-production"
}

variable "datadog_application_key" {
  description = "Datadog Application Key"
  type        = string
  default     = "datadog-account-id"
  sensitive   = true
}

variable "datadog_api_key" {
  description = "Datadog API key"
  type        = string
  sensitive   = true
  default     = "datadog-api-key"
}

# Configure the Formal Provider
provider "formal" {
  api_key = "<your-api-key-here>"
  retrieve_sensitive_values = false
}

# Configure AWS Provider
provider "aws" {
  region = "us-east-1"
}
# Create a Space
resource "formal_space" "production" {
  name = "production"
}

# Create a Resource
resource "formal_resource" "postgres" {
  name       = "production-postgres"
  technology = "postgres"
  hostname   = "prod-db.us-east-1.rds.amazonaws.com"
  port       = 5432
  space_id   = formal_space.production.id

  termination_protection = false
}

# Create a Connector
resource "formal_connector" "production" {
  name     = "production-connector"
  space_id = formal_space.production.id
}

# Configure Connector Settings
resource "formal_connector_configuration" "production" {
  connector_id = formal_connector.production.id
  log_level    = "info"
  # metrics collector endpoint. If there is no collector at the endpoint specified, the
  # connector will not send metrics
  otel_endpoint_hostname = "localhost"
  otel_endpoint_port     = 4317
}

# Create Connector Listener
resource "formal_connector_listener" "production_postgres" {
  connector_id = formal_connector.production.id
  name         = "production-postgres-listener"
  port         = 5432
}

# Add Listener Rule for Resource
resource "formal_connector_listener_rule" "postgres" {
  connector_listener_id = formal_connector_listener.production_postgres.id
  type                  = "resource"
  rule                  = formal_resource.postgres.id
}

# Create Users
resource "formal_user" "alice" {
  first_name = "Alice"
  last_name  = "Smith"
  email      = "alice@example.com"
  type       = "human"
}

resource "formal_user" "looker_app" {
  name = "Looker Application"
  type = "machine"
}

# Create Groups
resource "formal_group" "engineering" {
  name        = "Engineering Team"
  description = "Engineers with database access"
}

resource "formal_group_user_link" "alice_eng" {
  group_id = formal_group.engineering.id
  user_id  = formal_user.alice.id
}

# Create a Policy
resource "formal_policy" "mask_pii" {
  name        = "mask-pii-data"
  description = "Mask PII fields for non-privileged users"
  status      = "active"

  module = <<-EOT
    package formal.v2

    import future.keywords.if
    import future.keywords.in

    response := {
      "action": "mask",
      "type": "nullify",
      "columns": pii_columns
    } if {
      not "pii_access" in input.user.groups

      pii_columns := [col |
        col := input.row[_]
        col["data_label"] in ["email_address", "social_security_number", "phone_number"]
      ]

      count(pii_columns) > 0
    }
  EOT
}

# AWS S3 Bucket for Logs
resource "aws_s3_bucket" "logs" {
  bucket        = "${var.name}-logs"
  force_destroy = true
}

# AWS Integration
resource "formal_integration_cloud" "aws" {
  name         = "aws-production"
  cloud_region = "us-east-1"

  aws {
    template_version          = "latest"
    enable_rds_autodiscovery  = true
    enable_eks_autodiscovery  = true
    allow_s3_access           = true
    s3_bucket_arn             = "${aws_s3_bucket.logs.arn}/*"
  }
}

# Log Integration
resource "formal_integration_log" "datadog" {
  name = "datadog-logs"

  datadog {
    account_id = var.datadog_application_key
    api_key    = var.datadog_api_key
    site       = "datadoghq.com"
  }
}

```

## Resource Documentation

Full documentation for all resources:

**Core Resources:**

* [formal\_connector](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/connector)
* [formal\_resource](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/resource)
* [formal\_user](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/user)
* [formal\_group](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/group)
* [formal\_policy](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/policy)
* [formal\_hook](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/hook)
* [formal\_space](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/space)
* [formal\_satellite](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/satellite)
* [formal\_workflow](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/workflow)
* [formal\_form](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/form)

**Integrations:**

* [formal\_integration\_cloud](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/integration_cloud)
* [formal\_integration\_log](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/integration_log)
* [formal\_integration\_bi](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/integration_bi)

**Data Sources:**

* [formal\_connector](https://registry.terraform.io/providers/formalco/formal/latest/docs/data-sources/connector) - Look up a connector by ID or name
* [formal\_group](https://registry.terraform.io/providers/formalco/formal/latest/docs/data-sources/group) - Look up a group by ID or name
* [formal\_groups](https://registry.terraform.io/providers/formalco/formal/latest/docs/data-sources/groups) - List all groups
* [formal\_resource](https://registry.terraform.io/providers/formalco/formal/latest/docs/data-sources/resource) - Look up a resource by ID or name
* [formal\_space](https://registry.terraform.io/providers/formalco/formal/latest/docs/data-sources/space) - Look up a space by ID or name
* [formal\_user](https://registry.terraform.io/providers/formalco/formal/latest/docs/data-sources/user) - Look up a user by ID or identity
* [formal\_users](https://registry.terraform.io/providers/formalco/formal/latest/docs/data-sources/users) - List all users

## Example Repositories

Formal provides complete Terraform examples:

* [AWS Connector Deployment](https://github.com/formalco/terraform-provider-formal/tree/main/examples/deployments/aws)
* [Policy Example](https://github.com/formalco/terraform-provider-formal/tree/main/examples/policies)

Clone and customize for your needs:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
git clone https://github.com/formalco/terraform-provider-formal.git
cd terraform-provider-formal/examples/deployments/aws/connector
```

## Best Practices

> [accordion] Use Version Constraints
>
> Pin provider versions to avoid unexpected changes:
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> formal = {
>   source  = "formalco/formal"
>   version = "~> 4.32.0"  # Allow patches, not minor versions
> }
> ```

> [accordion] Store State Remotely
>
> Use remote state backends (S3, Terraform Cloud) for team collaboration:
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> terraform {
>   backend "s3" {
>     bucket = "my-terraform-state"
>     key    = "formal/production/terraform.tfstate"
>     region = "us-east-1"
>   }
> }
> ```

> [accordion] Use Variables
>
> Parameterize configurations for reusability:
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> variable "environment" {
>   type = string
> }
>
> resource "formal_connector" "main" {
>   name = "${var.environment}-connector"
> }
> ```

> [accordion] Separate Environments
>
> Use workspaces or separate state files for prod/staging/dev:
>
> ```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> terraform workspace new production
> terraform workspace select production
> ```

> [accordion] Enable Termination Protection
>
> Protect production resources:
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> resource "formal_resource" "prod_db" {
>   name                   = "production-postgres"
>   termination_protection = true
>   # ...
> }
> ```

## Importing Existing Resources

Bring existing Formal objects under Terraform management:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# Import a Resource
terraform import formal_resource.postgres <resource-id>

# Import a Connector
terraform import formal_connector.main <connector-id>

# Import a User
terraform import formal_user.alice <user-id>
```

Get object IDs from the Control Plane or API.

## Outputs

Export useful information:

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
output "connector_token" {
    description = "Connector API token for deployment"
    value       = formal_connector.production.api_key
    sensitive   = true
  }

  output "connector_listener_port" {
    description = "Configured listener port"
    value       = formal_connector_listener.production_postgres.port
  }

  output "resource_ids" {
    description = "Map of resource names to IDs"
    value = {
      (formal_space.production.name)                       = formal_space.production.id
      (formal_resource.postgres.name)                      = formal_resource.postgres.id
      (formal_connector.production.name)                   = formal_connector.production.id
      (formal_connector_listener.production_postgres.name) = formal_connector_listener.production_postgres.id
      (formal_group.engineering.name)                      = formal_group.engineering.id
    }
  }
```

## Next Steps

> [card] [Provider Docs](https://registry.terraform.io/providers/formalco/formal/latest/docs)
>
> Complete Terraform provider documentation

> [card] [Examples](https://github.com/formalco/terraform-provider-formal)
>
> Deployment examples on GitHub


## Related topics

- [Terraform Provider](https://docs.formal.ai/docs/changelog/terraform-provider.md)
- [OIDC Integration](https://docs.formal.ai/docs/guides/integrations/oidc.md)
- [Log Forwarding](https://docs.formal.ai/docs/guides/integrations/log.md)
- [Native Users](https://docs.formal.ai/docs/guides/core-concepts/resources/native-users.md)
- [HTTP](https://docs.formal.ai/docs/guides/core-concepts/resources/http.md)


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