What are Providers?
Providers are plugins that enable Terraform to interact with external APIs and services. They define the resource types and data sources that Terraform can manage, acting as the bridge between Terraform’s declarative configuration language and the actual infrastructure platforms.Provider Architecture
Terraform’s provider system is built on a plugin architecture that separates core Terraform functionality from provider-specific logic.Provider Interface
All providers implement theproviders.Interface defined in internal/providers/provider.go:17. This interface defines the complete contract for a resource provider plugin:
- GetProviderSchema: Returns the complete schema for the provider, including all resource types, data sources, ephemeral resources, and provider configuration
- ConfigureProvider: Initializes the provider with user-supplied configuration
- ReadResource: Refreshes a resource’s current state from the remote API
- PlanResourceChange: Computes the planned changes for a resource
- ApplyResourceChange: Executes the planned changes and returns the final state
Provider Schema
TheGetProviderSchemaResponse structure (internal/providers/provider.go:172) contains:
- Provider: Schema for the provider configuration itself
- ResourceTypes: Map of managed resource types to their schemas
- DataSources: Map of data sources to their schemas
- EphemeralResourceTypes: Short-lived resources (credentials, tokens)
- Functions: Provider-contributed functions
- ServerCapabilities: Optional features supported by the provider
Provider Lifecycle
1. Discovery and Installation
Terraform discovers and installs providers during theterraform init command:
- Parses
required_providersblock to determine which providers are needed - Consults provider sources to find available versions
- Downloads provider binaries matching the platform and version constraints
- Verifies package authenticity using cryptographic signatures
- Installs providers to the local plugin cache
2. Initialization
When Terraform needs a provider:- Launches the provider as a subprocess
- Establishes gRPC communication channel
- Performs plugin handshake to verify compatibility
- Calls
GetProviderSchemato retrieve the provider’s schema - Calls
ConfigureProviderwith user-supplied configuration
3. Operation
During plan and apply operations:-
Plan Phase:
- Calls
ReadResourceto refresh current state - Calls
PlanResourceChangeto compute proposed changes - Validates that planned changes are acceptable
- Calls
-
Apply Phase:
- Calls
ApplyResourceChangeto execute planned changes - Updates state with the new resource configuration
- Calls
4. Cleanup
After operations complete:- Calls
Stopto signal the provider to halt in-flight operations - Calls
Closeto shut down the provider gracefully - Terminates the plugin process
Provider Factory
Providers are instantiated through theFactory function type (internal/providers/factory.go:6):
- Built-in providers: Compiled directly into Terraform
- External providers: Separate plugin binaries
Communication Protocol
Providers communicate with Terraform core using gRPC over protocol version 5. TheGRPCProvider implementation (internal/plugin/grpc_provider.go) handles:
- Protocol serialization/deserialization
- Type conversion between Go types and protobuf messages
- Error handling and diagnostics propagation
- Schema caching for performance
Provider Capabilities
Providers can declare optional capabilities throughServerCapabilities (internal/providers/provider.go:277):
Provider Sources
Terraform supports multiple provider sources (internal/getproviders/source.go:14):
- Registry Source: Downloads providers from public or private registries
- Filesystem Mirror: Uses locally cached provider packages
- Network Mirror: Fetches providers from HTTP/HTTPS mirrors
- Dev Overrides: Points to locally built provider binaries for development
Next Steps
- Provider Configuration - Learn how to configure providers
- Provider Installation - Understand provider installation and caching
- Provider Development - Build your own providers