This Terraform provider allows you to manage Grafbase resources using the Grafbase API.
Add the provider to your Terraform configuration:
terraform {
required_providers {
grafbase = {
source = "grafbase/grafbase"
}
}
}
provider "grafbase" {
# API key can be set via GRAFBASE_API_KEY environment variable
# or explicitly set here (not recommended for production)
}
export GRAFBASE_API_KEY="your-grafbase-api-key"
resource "grafbase_graph" "example" {
account_slug = "my-account"
slug = "my-graph"
}
output "graph_id" {
value = grafbase_graph.example.id
}
terraform init
terraform plan
terraform apply
-
Clone the repository:
git clone https://github.com/grafbase/terraform-provider-grafbase cd terraform-provider-grafbase
-
Build the provider:
go build -o terraform-provider-grafbase
-
(Optional) Install locally for development:
make install
For a complete development setup:
./scripts/dev-setup.sh
This script will:
- Check dependencies
- Build the provider
- Run tests
- Install the provider locally
- Create development configuration
The provider supports multiple authentication methods:
export GRAFBASE_API_KEY="your-api-key-here"
provider "grafbase" {
api_key = var.grafbase_api_key
}
variable "grafbase_api_key" {
description = "Grafbase API key"
type = string
sensitive = true
}
- Visit the Grafbase Dashboard
- Navigate to your organization's settings page
- Generate a new access token
- Store it securely (e.g., in your environment or secret management system)
The grafbase_graph
resource allows you to manage graphs. Graphs are the fundamental units in Grafbase that contain your GraphQL schema and configuration.
Basic Usage:
resource "grafbase_graph" "example" {
account_slug = "my-account"
slug = "my-graph"
}
With Variables:
variable "environment" {
description = "Environment name"
type = string
default = "dev"
}
resource "grafbase_graph" "app" {
account_slug = "my-account"
slug = "my-app-${var.environment}"
}
The following arguments are supported:
-
account_slug
(Required, String) - The slug of the Grafbase account where the graph will be created. This must be an existing account that you have access to. Changing this attribute forces replacement of the resource. -
slug
(Required, String) - The slug for the graph. Must be unique within the specified account and follow Grafbase naming conventions (lowercase letters, numbers, and hyphens). Changing this attribute forces replacement of the resource.
In addition to all arguments above, the following attributes are exported:
id
(String) - The unique identifier of the graph assigned by Grafbase.created_at
(String) - The RFC3339 timestamp when the graph was created.
Existing graphs can be imported using the format account_slug/graph_slug
:
# Import a specific graph
terraform import grafbase_graph.example my-account/my-graph
# Import with resource name matching the graph slug
terraform import grafbase_graph.my_graph my-account/my-graph
- Immutability: Both
account_slug
andslug
are immutable after creation. Changing either will destroy and recreate the graph. - Uniqueness: Graph slugs must be unique within an account.
- Naming: Follow Grafbase naming conventions for slugs (lowercase, alphanumeric, hyphens allowed).
- Permissions: You must have appropriate permissions in the specified account to create graphs.
The grafbase_branch
resource allows you to manage branches within a graph. Branches enable you to have different environments and configurations for your GraphQL API.
Basic Usage:
resource "grafbase_graph" "example" {
account_slug = "my-account"
slug = "my-graph"
}
resource "grafbase_branch" "main" {
account_slug = grafbase_graph.example.account_slug
graph_slug = grafbase_graph.example.slug
name = "main"
}
Multiple Branches:
resource "grafbase_graph" "app" {
account_slug = "my-account"
slug = "my-app"
}
resource "grafbase_branch" "main" {
account_slug = grafbase_graph.app.account_slug
graph_slug = grafbase_graph.app.slug
name = "main"
}
resource "grafbase_branch" "staging" {
account_slug = grafbase_graph.app.account_slug
graph_slug = grafbase_graph.app.slug
name = "staging"
}
resource "grafbase_branch" "feature" {
account_slug = grafbase_graph.app.account_slug
graph_slug = grafbase_graph.app.slug
name = "feature-new-schema"
}
The following arguments are supported:
-
account_slug
(Required, String) - The slug of the Grafbase account where the branch's graph exists. Changing this attribute forces replacement of the resource. -
graph_slug
(Required, String) - The slug of the graph where this branch will be created. Changing this attribute forces replacement of the resource. -
name
(Required, String) - The name of the branch. Must be unique within the graph and follow Grafbase naming conventions. Changing this attribute forces replacement of the resource.
In addition to all arguments above, the following attributes are exported:
id
(String) - The unique identifier of the branch assigned by Grafbase.environment
(String) - The environment type of the branch (eitherPREVIEW
orPRODUCTION
).operation_checks_enabled
(Boolean) - Whether operation checks are enabled for this branch.operation_checks_ignore_usage_data
(Boolean) - Whether usage data should be ignored when running operation checks.
Existing branches can be imported using the format account_slug/graph_slug/branch_name
:
# Import a specific branch
terraform import grafbase_branch.main my-account/my-graph/main
# Import a feature branch
terraform import grafbase_branch.feature my-account/my-graph/feature-auth
- Immutability: All input attributes (
account_slug
,graph_slug
, andname
) are immutable after creation. Changing any of them will destroy and recreate the branch. - Production Branch: The production branch (typically named "main") cannot be deleted. Attempting to delete it will result in an error.
- Branch Names: Branch names must be unique within a graph and follow Grafbase naming conventions.
- Dependencies: The graph must exist before creating branches. Use Terraform dependencies to ensure proper ordering.
Explore the examples/
directory for complete usage examples:
examples/main.tf
- Basic usage with variablesexamples/complete/
- Advanced multi-graph setupexamples/dev-setup/
- Local development configuration
- Go 1.21 or later
- Terraform 1.5 or later
- Make (optional, for using Makefile commands)
Use the provided development script for a complete setup:
./scripts/dev-setup.sh
This will handle all the setup steps automatically.
-
Clone and build:
git clone https://github.com/grafbase/terraform-provider-grafbase cd terraform-provider-grafbase go mod download make build
-
Run tests:
make test
-
Install locally:
make install
make build # Build the provider binary
make test # Run unit tests
make testacc # Run acceptance tests (requires TF_ACC=1 and valid API key)
make install # Install provider locally for development
make clean # Clean build artifacts
make fmt # Format Go code
make lint # Run linter (requires golangci-lint)
make docs # Generate documentation
make dev-setup # Setup development environment
make debug # Run provider in debug mode
go test ./...
Acceptance tests require a valid Grafbase API key and will create real resources:
export GRAFBASE_API_KEY="your-api-key"
export TF_VAR_account_slug="your-test-account"
TF_ACC=1 go test ./... -v
-
Build the provider:
go build -o terraform-provider-grafbase
-
Create a
.terraformrc
file:provider_installation { dev_overrides { "grafbase/grafbase" = "/path/to/terraform-provider-grafbase" } direct {} }
-
Use in your Terraform configuration:
terraform { required_providers { grafbase = { source = "grafbase/grafbase" } } }
For debugging the provider:
go run . -debug
This outputs instructions for setting TF_REATTACH_PROVIDERS
environment variable.
For verbose Terraform logging:
export TF_LOG=DEBUG
terraform apply
-
Authentication Failed
- Verify your API key is correct
- Check that
GRAFBASE_API_KEY
environment variable is set - Ensure the API key has the necessary permissions
-
Account Not Found
- Verify the account slug is correct
- Check that you have access to the specified account
-
Graph Already Exists
- Graph slugs must be unique within an account
- Use
terraform import
to import existing graphs
-
Provider Not Found
- Ensure you've run
terraform init
- Check your
.terraformrc
configuration for local development
- Ensure you've run
-
Enable debug logging:
export TF_LOG=DEBUG
-
Check provider installation:
terraform version
-
Validate configuration:
terraform validate
-
Test API connectivity:
curl -H "Authorization: Bearer $GRAFBASE_API_KEY" \ -H "Content-Type: application/json" \ -d '{"query": "query { __schema { queryType { name } } }"}' \ https://api.grafbase.com/graphql
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-new-feature
- Make your changes
- Add tests for new functionality
- Run the test suite:
make test
- Format your code:
make fmt
- Submit a pull request
- Follow Go best practices and conventions
- Add tests for new features and bug fixes
- Update documentation for user-facing changes
- Use descriptive commit messages
- Ensure all tests pass before submitting
- Make changes to the code
- Run
make build
to build the provider - Run
make test
to run unit tests - Test manually with example configurations
- Run
make fmt
to format code - Submit pull request
This project is licensed under the Mozilla Public License 2.0. See the LICENSE file for details.
- GitHub Issues: Report bugs and request features
- Documentation: Grafbase Docs
- Community: Grafbase Discord
When reporting issues, please include:
- Terraform version (
terraform version
) - Provider version
- Operating system and architecture
- Relevant Terraform configuration (sanitized)
- Error messages or unexpected behavior
- Steps to reproduce
For security vulnerabilities, please email [email protected] instead of creating a public issue.