
Permit.io Terraform Provider
The Permit.io Terraform Provider allows you to manage your authorization policies, resources, roles, and permissions as infrastructure code. This enables you to version control your authorization policies, automate deployments, and maintain consistency across environments.
What is Infrastructure as Code (IaC)?
Infrastructure as Code is the practice of managing and provisioning computing infrastructure through machine-readable definition files rather than physical hardware configuration or interactive configuration tools. With Terraform, you can:
- Version Control: Track changes to your authorization policies in Git
- Automation: Deploy policies consistently across environments
- Collaboration: Review and approve policy changes through pull requests
- Reproducibility: Ensure identical environments across development, staging, and production
The Permit CLI has the ability to export environments to Terraform scripts
Quick Start
Prerequisites
Before you begin, ensure you have:
- Terraform >= 1.0 or OpenTofu >= 1.0 installed
- A Permit.io account with an API key
- Basic understanding of Terraform syntax
1. Get Your API Key
- Go to app.permit.io
- Navigate to Settings → API Keys
- Copy your environment-level API key
2. Create Your First Terraform Configuration
Create a new directory for your Terraform project and add the following files:
main.tf - Main Terraform configuration:
terraform {
required_providers {
permitio = {
source = "registry.terraform.io/permitio/permit-io"
version = "~> 0.0.14" # Use the latest version
}
}
}
# Configure the Permit.io provider
provider "permitio" {
api_key = "YOUR_API_KEY" # Replace with your actual API key
# api_url = "https://api.permit.io" # Optional: defaults to https://api.permit.io
}
# Define a resource (e.g., documents in your application)
resource "permitio_resource" "document" {
key = "document"
name = "Document"
description = "A confidential document that users can read and write"
actions = {
"read" = {
"name" = "Read"
"description" = "Read a document"
}
"write" = {
"name" = "Write"
"description" = "Write to a document"
}
"delete" = {
"name" = "Delete"
"description" = "Delete a document"
}
}
attributes = {
"title" = {
"description" = "The title of the document"
"type" = "string"
}
"owner" = {
"description" = "The owner of the document"
"type" = "string"
}
}
}
# Define roles with specific permissions
resource "permitio_role" "reader" {
key = "reader"
name = "Reader"
description = "Can read documents but cannot modify them"
permissions = ["document:read"]
depends_on = [permitio_resource.document]
}
resource "permitio_role" "writer" {
key = "writer"
name = "Writer"
description = "Can read and write documents"
permissions = ["document:read", "document:write"]
depends_on = [permitio_resource.document]
}
resource "permitio_role" "admin" {
key = "admin"
name = "Administrator"
description = "Full access to documents including deletion"
permissions = ["document:read", "document:write", "document:delete"]
depends_on = [permitio_resource.document]
}
variables.tf - Define variables for better configuration management:
variable "permit_api_key" {
description = "Permit.io API key"
type = string
sensitive = true
}
variable "permit_api_url" {
description = "Permit.io API URL"
type = string
default = "https://api.permit.io"
}
terraform.tfvars - Set your actual values (don't commit this file):
permit_api_key = "your-actual-api-key-here"
permit_api_url = "https://api.permit.io"
3. Initialize and Apply
# Initialize Terraform
terraform init
# Plan your changes
terraform plan
# Apply the configuration
terraform apply