How providers are installed and configured
terraform init
, which automatically downloads and installs the providers in your local environment.Categories:
2 minute read
To install a provider you specify details about which one you need in your Terraform code. The actual plug-in is downloaded to the Terraform configuration working directory at run-time.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
}
}
}
provider "azurerm" {
features {}
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~>3.0"
}
}
}
provider "aws" {
region = "us-west-2"
}
In this Azure example, the required_providers
block within the terraform
block is used to specify that the Azure provider is needed. The source
attribute is used to specify where the provider can be found, and the version
attribute to specify which version to use. The ~>2.0
for the version means any version in the 2.x range.
The provider "azurerm"
block is used to configure the provider. The features {}
block inside is required by the Azure provider, even if you have no features to set. For the AWS configuration we set the region.
This is a minimal configuration. Depending on what resources you’re managing with Azure, and what your authentication method is, you might need to add more to this provider configuration.
required_providers
specifies which provider, version, and download source to use.
provider
configures the provider
Registry
There are many, many different providers available to use depending on your use case. The configuration in your Terraform code follows the same pattern. It’s just a matter of choosing the provider and other details as required.
You can browse the HashiCorp registry here for more details but keep in mind that a provider can come from anywhere you want, in-house developed on your own private registry or vendor specific from theirs.
Prefix
Each required_provider
block identifies the provider with a name. Normally you specify the name that the vendor suggests such as azurem
or aws
but you can set your own but this is not recommended.
Each resource and data source that is prefixed with the same name uses the configuration as specified in that provider block. So for the AWS example the location is set to us-west-2
which means that all resources or data sources starting with aws_
will be set to use this region.