provider block reference
Use theprovider block to declare and configure Terraform plugins, called providers. Providers let Terraform manage real-world infrastructure with provider-defined resources and data sources.
Hands-on: Try thePerform CRUD Operations with Providers tutorial.
Background
Theprovider block configures a named provider, which is a plugin that lets Terraform interact with cloud providers, SaaS providers, and other APIs.
HashiCorp's publicTerraform registry distributes providers separately from Terraform itself, and each provider has its own release cadence, documentation, and versions. If you are using HCP Terraform, you can use theprivate registry to share providers within an organization.
Anyone can develop a Terraform provider and use it locally, or publish it to the Terraform public registry or HCP Terraform private registry. To learn more about authoring providers, refer to thePlugin framework.
Define provider configurations in the root module of your Terraform configuration. Child modules receive their provider configurations from their parent modules, so we strongly recommend against definingprovider blocks in child modules. To learn how to declare providers in your configuration, refer toProvider requirements.
Configuration model
Theprovider block supports the following arguments:
provider "<PROVIDER_NAME>"block<PROVIDER_ARGUMENTS>variousaliasstringversionstring (Deprecated)
Complete configuration
All available arguments are defined in the followingprovider block. Thealias andversion arguments are optional, and you cannot use both in the same block.
provider "<PROVIDER_NAME>" { <PROVIDER_ARGUMENTS> alias= "<ALIAS_NAME>" version= "<VERSION_CONSTRAINT>" # Deprecated}Specification
Aprovider block supports the following configuration.
provider "<PROVIDER_NAME>"
You must specify the name of the provider that you want to configure as an inline argument. Refer toLocal names for more information.
You can specify the following arguments in aprovider block:
| Argument | Description | Type | Required? |
|---|---|---|---|
| Provider-specific arguments | Configuration arguments that the provider developer defined. | Various | Varies by provider |
alias | A unique identifier for a specific provider configuration, letting you use multiple configurations for the same provider. | String | Optional |
version | A version constraint for the provider. This argument is deprecated. | String | Optional |
Provider-specific arguments
The body of theprovider block contains configuration arguments for the provider, which are defined by the provider itself.
provider "<PROVIDER_NAME>" { <PROVIDER_ARGUMENTS>}A provider's documentation lists which configuration arguments it expects. For providers distributed on theTerraform registry, versioned documentation is available on each provider's page.
If you do not explicitly define aprovider block, Terraform assumes and creates an empty default configuration for that provider. However, if a provider has required arguments, Terraform raises an error because it can't create that provider without the required values.
You can useexpressions to configure provider arguments, but you can only reference values that Terraform knows before it applies your configuration. You can reference input variables and arguments that you specify directly in your configuration, but you cannot reference computed resource attributes, such asgoogle.web.public_ip.
Many providers support shell environment variables or other alternate sources for their configuration values, which helps keep credentials out of your version-controlled Terraform configuration. Refer to a provider's documentation to learn more about the assignment methods each argument supports.
Summary
- Data type: Various, depending on the provider
- Default: None, but requirements vary by provider
- Required: Varies by provider
- Example:Select an alternate provider configuration
alias
Optionally use thealias argument to define multiple configurations for the same provider. Defining multiple provider aliases lets you specify which provider configuration to use for individual resources, data sources, or modules.
To create multiple configurations for a given provider, include multipleprovider blocks with the same provider name, then add thealias argument to each additional provider configuration to give it a unique identifier.
provider "exampleName" { region= "us-east-1"}provider "exampleName" { alias= "west" region= "us-west-1"}To refer to a provider alias, use<PROVIDER_NAME>.<ALIAS> in theprovider argument of the following blocks:
If there are multiple aliases for a provider, theprovider block without analias argument is the default configuration for that provider. Resources, data sources, and modules that don't specify theprovider meta-argument use the default provider configuration that matches the resource type name.
Summary
- Data type: String
- Default: None
- Refer to the following examples to learn more about aliasing providers:
version
Theversion argument in provider configurations is deprecated, and Terraform will remove it in a future version. Instead, declare provider version constraints intheterraform block'srequired_providers block.
Summary
- Data type: String
- Default: None
- Deprecated
Examples
The following examples demonstrate common use cases forprovider blocks.
Basic provider configuration
The following example configures thegoogle provider with a specific project and region. In theterraform block'srequired_providers block you define the provider version you want to use, where to source the provider from, and the local name of the provider:
terraform.tf
terraform { required_providers { google= { source= "hashicorp/google" version= "~> 4.0" } }}You can configure the provider with theprovider block in the root module of your configuration.
main.tf
provider "google" { project= "acme-app" region= "us-central1"}Multiple provider configurations
In the following example, two configurations for the AWS provider support different regions. The default configuration targets theus-east-1 and the aliased configuration targetsus-west-2:
main.tf
provider "aws" { region= "us-east-1"}provider "aws" { alias= "west" region= "us-west-2"}Resources that begin withaws_ use the defaultaws provider configuration unless you supply theprovider argument.
Select an alternate provider configuration
You can make a resource, data source, or module use an alternate provider configuration by setting theprovider meta-argument to a<PROVIDER_NAME>.<ALIAS> reference.
In the following example, theaws_instance.foo resource uses theaws.west provider configuration, andaws_instance.bar uses the defaultaws provider configuration:
provider "aws" { region= "us-east-1"}provider "aws" { alias= "west" region= "us-west-2"}resource "aws_instance" "foo" { provider= aws.west # …}resource "aws_instance" "bar" { # …}Provider without a default configuration
If there are multiple aliases for a provider in your configuration, theprovider block without analias argument is the default configuration for that provider. If everyprovider block in your configuration uses an alias, Terraform creates an implied empty default configuration for that provider. Any resource that does not specify aprovider meta-argument uses the empty default configuration.
In the following example, both AWS provider configurations use aliases, but theaws_s3_bucket does not specify aprovider argument:
provider "aws" { alias= "east" region= "us-east-1"}provider "aws" { alias= "west" region= "us-west-2"}resource "aws_s3_bucket" "default_provider" { bucket= "uses-implied-empty-config"}Terraform uses the implied empty default configuration for theaws_s3_bucket resource because the resource doesn't specify aprovider argument. If a provider requires specific arguments, Terraform returns an error when resources try to use the default configuration because the default is not properly configured.
Pass provider configurations to a child module
When you define a provider in your root module, Terraform implicitly passes that provider configuration to any child modules to ensure all modules use the same configuration.
In the following example, the root module defines an AWS provider configuration and Terraform implicitly passes that configuration to a child module:
main.tf
provider "aws" { region= "us-west-2"}module "vpc" { source= "./modules/vpc"}The child module uses the provider configuration passed from the root module:
modules/vpc/main.tf
terraform { required_providers { aws= { source= "hashicorp/aws" version= "~> 5.0" } }}resource "aws_vpc" "main" { cidr_block= "10.0.0.0/16" tags= { Name= "Main VPC" }}Theaws_vpc resource inherits the same AWS provider configuration from the root module, and Terraform creates theaws_vpc resource in theus-west-2 region.
Child modules do not inherit provider source or version requirements, so you must explicitly define those within a child module. Learn more aboutinheriting providers in modules.
Using an alternate provider configuration in a child module
To use an aliased provider configuration in a child module, the child module must declare the alias using theconfiguration_aliases argument in therequired_providers block.
In the following example, the root module passes theaws.west alias to theweb-server child module:
main.tf
provider "aws" { region= "us-east-1"}provider "aws" { alias= "west" region= "us-west-2"}module "web-server" { source= "./modules/web-server" providers= { aws.west= aws.west }}The followingconfiguration_aliases argument declares that the child module expects to receive a provider configuration it can reference asaws.west. Without this declaration, Terraform raises an error when the module tries to referenceaws.west in its resources.
modules/web-server/main.tf
terraform { required_providers { aws= { source= "hashicorp/aws" version= "~> 5.0" configuration_aliases= [aws.west] } }}data "aws_ami" "amazon_linux" { provider= aws.west #...}Modules have special requirements for providers, refer toProviders within modules to learn more.
Provider configuration with expressions
In the following example, the provider configuration uses input variables and local values to set its arguments:
variable "aws_region" { description= "The AWS region to deploy resources in" type= string default= "us-west-2"}locals { common_tags= { Environment= "production" Project= "web-app" }}provider "aws" { region= var.aws_region default_tags { tags= local.common_tags }}Provider configuration without arguments
In the following example, the provider block defines an empty configuration for therandom provider. Terraform also assumes an empty default configuration for any provider that you do not explicitly configure with aprovider block:
provider "random" { }You can choose to omit this block entirely if you don't need to configure any provider-specific arguments.