Protocol Overview
Terraform plugins follow a local RPC model:Process Model
Terraform Core (client):- Discovers plugin executables
- Launches plugins as child processes
- Communicates via loopback interface
- Controls plugin lifecycle
- Exposes gRPC services
- Prints handshake to stdout
- Listens on local port
- Terminates when no longer needed
docs/plugin-protocol/README.md
Protocol Versioning
The protocol uses major and minor versioning:Major Versions
- Breaking changes require new major version
- Version encoded in protobuf package name (
tfplugin5,tfplugin6) - Both sides negotiate mutually-supported version
- Multiple versions can coexist
Minor Versions
- Backward-compatible enhancements
- Optional features that can be ignored
- No protocol package name change
- Feature detection at runtime
Current versions:
- Protocol 5.x: Introduced with Terraform 0.12
- Protocol 6.x: Latest version with tidier naming
Version Compatibility
If no mutual version exists, Terraform returns an error duringterraform init.
See: docs/plugin-protocol/README.md:104
Protocol Buffers Definition
The protocol is defined using Protocol Buffers:docs/plugin-protocol/tfplugin5.proto, docs/plugin-protocol/tfplugin6.proto
Wire Format for Values
Terraform values are serialized usingDynamicValue:
MessagePack Encoding
Preferred format - compact binary representation.- Primitive Types
- Collection Types
- Special Types
docs/plugin-protocol/object-wire-format.md:36
Unknown Values
Unknown values use MessagePack extensions:docs/plugin-protocol/object-wire-format.md:94
Block Encoding
Resource blocks become MessagePack maps:SINGLE: Single value or nilLIST: Array preserving orderSET: Array in undefined orderMAP: Map with block labels as keysGROUP: LikeSINGLEbut synthesizes empty block if absent
docs/plugin-protocol/object-wire-format.md:50
JSON Encoding
Secondary format for compatibility:- Used when MessagePack not available
- Simpler but less efficient
- Cannot represent unknown values (limitation)
- Servers should always send MessagePack
docs/plugin-protocol/object-wire-format.md:187
Provider Methods
Key RPC methods in the provider protocol:GetProviderSchema
Returns schema for provider, resources, and data sources:ValidateResourceConfig
Validates resource configuration before planning:PlanResourceChange
Most important method - creates execution plan:- During planning - Configuration may contain unknown values
- During apply - All configuration values are known
- Return planned state matching type schema
- Preserve known config values in planned state
- Mark unpredictable values as unknown
- Indicate which changes require replacement
docs/resource-instance-change-lifecycle.md:134
ApplyResourceChange
Executes the planned change:- Make actual infrastructure changes
- Return new state matching planned state for known values
- Replace unknown values with actual results
- Not modify known values from planned state
docs/resource-instance-change-lifecycle.md:188
ReadResource
Refreshes resource state from infrastructure:- Query current infrastructure state
- Distinguish normalization vs. drift
- Preserve user-written values when semantically unchanged
- Report actual drift when infrastructure changed
docs/resource-instance-change-lifecycle.md:252
UpgradeResourceState
Migrates state from older provider versions:docs/resource-instance-change-lifecycle.md:224
ImportResourceState
Adopts existing infrastructure:ReadResource will complete.
See: docs/resource-instance-change-lifecycle.md:340
Diagnostics
All responses can include diagnostics:- ERROR: Operation failed, cannot continue
- WARNING: Potential issue, operation continues
Provider Capabilities
Providers can advertise optional capabilities:Deferred Changes
Providers can defer changes (protocol 6.x):SDK Implementation
Most providers use SDKs rather than implementing the protocol directly:Terraform Plugin Framework
Modern Go SDK with protocol 6 support
Terraform Plugin SDK
Legacy Go SDK (protocol 5)
- Generate protocol buffer stubs
- Handle gRPC communication
- Marshal/unmarshal values
- Provide higher-level abstractions
- Manage plugin lifecycle
Further Reading
Resource Lifecycle
How provider methods fit together
Plugin Protocol Docs
Complete protocol specifications