Provider Configuration Basics
Providers are configured usingprovider blocks in your Terraform configuration. Each provider has its own configuration schema that defines the arguments it accepts.
Declaring Provider Requirements
Before using a provider, declare it in arequired_providers block:
Provider Source Addresses
Thesource attribute uses the format: [hostname/]namespace/type
- hostname (optional): Registry hostname (defaults to
registry.terraform.io) - namespace: Organizational namespace (e.g.,
hashicorp,aws) - type: Provider type name (e.g.,
aws,azurerm)
hashicorp/aws→registry.terraform.io/hashicorp/awsaws→ legacy provider reference (not recommended)example.com/myorg/custom→ custom registry provider
Version Constraints
Version constraints use semantic versioning operators:= 1.0.0- Exact version!= 1.0.0- Exclude specific version> 1.0.0- Greater than>= 1.0.0- Greater than or equal to< 2.0.0- Less than<= 2.0.0- Less than or equal to~> 1.2.0- Pessimistic constraint (>= 1.2.0, < 1.3.0)
Configuring Providers
After declaring requirements, configure the provider with aprovider block:
Provider Configuration Validation
When you configure a provider, Terraform performs validation in multiple stages:1. Schema Validation
Terraform validates the configuration against the provider’s schema fromGetProviderSchema. This checks:
- Required attributes are present
- Attribute types match the schema
- Unknown attributes are flagged as errors
2. Provider-Specific Validation
Providers can implement custom validation logic in theValidateProviderConfig method (internal/providers/provider.go:30):
- Checking for conflicting configuration options
- Validating credential formats
- Verifying API endpoint reachability
3. Configuration Application
During initialization, Terraform callsConfigureProvider (internal/providers/provider.go:437):
- Establishes connections to remote APIs
- Authenticates with credentials
- Initializes internal clients and state
- Returns diagnostics if configuration is invalid
Multiple Provider Configurations
You can define multiple configurations for the same provider using aliases:Provider Configuration Inheritance
In module hierarchies, provider configurations are inherited:Root Module
Explicit Provider Passing
You can explicitly pass provider configurations to modules:Environment Variables
Many providers support environment-based configuration:Provider Meta-Arguments
Some meta-arguments can be used in provider blocks:alias
Create multiple configurations for the same provider:version (deprecated)
Older syntax for version constraints (userequired_providers instead):
Sensitive Values
Provider configurations often include sensitive values. Use variables with sensitive flag:Provider Configuration Context
Providers receive additional context during configuration (internal/providers/provider.go:437):
- TerraformVersion: The version of Terraform executing the provider
- ClientCapabilities: Features supported by the Terraform client
- Ensure compatibility with the Terraform version
- Enable/disable features based on client capabilities
- Provide version-specific warnings or errors
Configuration Lifecycle
The provider configuration lifecycle follows these steps:- Parse: Terraform parses the HCL configuration
- Schema Lookup: Retrieves provider schema via
GetProviderSchema - Decode: Decodes configuration against the schema
- Validate: Calls
ValidateProviderConfigfor custom validation - Configure: Calls
ConfigureProviderto initialize the provider - Ready: Provider is ready for resource operations
Best Practices
Use Variables for Credentials
Never hardcode credentials:Pin Provider Versions
Use version constraints to ensure reproducible deployments:Use Required Providers Block
Always declare providers inrequired_providers for explicit dependency management:
Next Steps
- Provider Installation - Learn about provider installation and caching
- Provider Development - Build your own providers
- Providers Overview - Understand provider architecture