Provider Development Overview
Terraform providers are standalone plugin binaries that communicate with Terraform Core using the gRPC protocol. This architecture enables providers to be developed independently in any language that supports gRPC.Plugin Protocol
Terraform uses HashiCorp’sgo-plugin library for the plugin system, implementing communication over gRPC.
Protocol Versions
Terraform supports multiple protocol versions for backward compatibility (internal/plugin/serve.go:12):
Handshake Configuration
The plugin handshake ensures Terraform Core and providers are compatible (internal/plugin/serve.go:26):
- Verify provider compatibility
- Prevent accidental execution of non-provider binaries
- Negotiate protocol version
Implementing a Provider
Provider Interface
Implement theproviders.Interface (internal/providers/provider.go:17) to create a provider:
Serving the Provider
Use theplugin.Serve function to expose your provider (internal/plugin/serve.go:52):
Provider Factory
For built-in or testing providers, use theFactory pattern (internal/providers/factory.go:6):
Schema Definition
Provider Schema
Define the schema for your provider configuration:Resource Schema
Define schemas for each resource type:Resource Lifecycle Implementation
Read Operation
Refresh the current state of a resource (internal/providers/provider.go:455):
Plan Operation
Compute planned changes for a resource (internal/providers/provider.go:536):
Apply Operation
Execute planned changes and return final state (internal/providers/provider.go:604):
Import Operation
Import existing infrastructure (internal/providers/provider.go:658):
gRPC Communication
TheGRPCProvider handles translation between Terraform and the provider (internal/plugin/grpc_provider.go:49):
Client-Side Translation
Schema Caching
Providers can declareGetProviderSchemaOptional to enable schema caching (internal/plugin/grpc_provider.go:86):
Message Size Limits
Large schemas require increased message size limits (internal/plugin/grpc_provider.go:115):
Error Handling and Diagnostics
Diagnostic Types
Providers return diagnostics for errors and warnings:Creating Diagnostics
State Upgrade
Handle schema version changes with state upgrade (internal/providers/provider.go:392):
Testing Providers
Unit Testing
Test provider logic in isolation:Integration Testing
Use Terraform’s test framework for end-to-end testing with actual Terraform operations.Best Practices
Use the Terraform Plugin SDK
While you can implement the provider interface directly, use the official Terraform Plugin SDK for production providers. It provides:- Schema helpers
- CRUD operation scaffolding
- Testing utilities
- Automatic protocol handling
Implement Graceful Degradation
Handle API errors gracefully:Validate Early
ImplementValidateResourceConfig to catch errors before apply:
Use Private Metadata
Store provider-specific data in thePrivate field:
Handle Partial Updates
Return partial state on errors during apply:Next Steps
- Terraform Plugin SDK Documentation
- Provider Design Principles
- Providers Overview - Understand provider architecture
- Provider Configuration - Learn about provider configuration