Terraform

Terraform Providers Overview & How To Use Them

Terraform providers

In this article, we will discuss Terraform providers. First, we will look at the purpose of a provider in Terraform, and how they can be used and referenced. We will then look at some of the available providers with examples.

What we’ll cover:

  1. What is a provider in Terraform?
  2. How to use Terraform providers?
  3. Terraform provider configuration
  4. Commonly used Terraform providers
  5. Other useful providers

What is a provider Terraform?

A provider in Terraform is a plugin that enables interaction with an API. This includes Cloud providers and software-as-a-service providers. The providers are specified in the Terraform configuration code. They tell Terraform which services it needs to interact with.

How many providers does Terraform have?

There are hundreds of Terraform providers, which makes it a highly versatile tool. Much of Terraform’s popularity comes from its platform-agnostic approach: you can use it across many services, unlike platform-specific options such as Azure ARM templates or Bicep, which only interact with the Azure API.

How to use Terraform providers?

Once a provider is specified, each provider makes a list of resources and data types available for use in the Terraform code. These are listed in the documentation available on the Terraform Registry.

Terraform providers are released independently of Terraform itself. Each provider follows its own release cadence, adding features and bug fixes over time. Providers are typically maintained by HashiCorp, the company behind the product (for example, MongoDB for the mongodb provider), or community contributors.

  • Providers maintained by Hashicorp are marked as ‘official’ on the documentation pages.
  • Providers marked as ‘verified’ are owned by official third-party technology vendors. Vendors are also members of the HashiCorp Technology Partner Program.
  • Providers marked as ‘community’ are published by members or groups in the Terraform community.

You can develop a provider in the unlikely event that it doesn’t already exist for the infrastructure you are trying to provision using Terraform. Or you can develop it for your product. Providers are written in Go and utilize the Terraform Plugin SDK.

How to download and install the Terraform provider?

Terraform installs providers when you run terraform init. It initializes the working directory (and typically the backend), downloads modules, installs the required provider plugins, and creates/updates .terraform.lock.hcl to record the selected provider versions and checksums.

Where do providers come from?

By default, Terraform installs providers from the public Terraform Registry. If you need different sources (for example, a private registry, a local filesystem mirror, or a network mirror), configure provider_installation in your Terraform CLI configuration file (Windows: %APPDATA%/terraform.rc; macOS/Linux: ~/.terraformrc or ~/.terraform.d/terraform.rc).

When Terraform re-downloads a provider?

Terraform installs a provider into the working directory’s .terraform/ directory when it isn’t present locally (or doesn’t match the version/checksums Terraform expects from the lock file). If you have a cache/mirror configured, Terraform will reuse that rather than downloading over the network.

If you change a provider version constraint in required_providers, Terraform will select a different version only if the currently locked version no longer matches the constraint. If you want Terraform to actively look for newer versions that still satisfy the constraint, run:

terraform init -upgrade

Speeding things up with a plugin cache

To avoid downloading the same provider binaries across multiple projects, enable a global plugin cache in your CLI configuration file:

plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"

Caching is only effective if your runner preserves the cache directory between runs. For example, HCP Terraform’s default remote execution uses single-use worker VMs, so filesystem changes made during a run aren’t persisted to later runs.

Read more about the elements of Terraform architecture and how to write a custom Terraform provider.

Terraform provider configuration

Provider configurations should be declared in the root module of your Terraform project. They can be declared in any .tf file. We would recommend either putting them in the main.tf , creating a providers.tf file specifically for the Providers and nothing else, or alternatively a versions.tf file which would include the required_providers block and specify the Terraform version. Any child modules receive their provider configuration from the root module.

Terraform Provider Block Example

Providers are configured using a provider block. Some providers require certain information to be configured, such as endpoint URLs or cloud regions. Depending on which provider you need to use, the associated documentation should detail which settings you need to configure, along with examples.

To find this for each provider, click the ‘use provider’ button in the top-right corner of the documentation page.

use provider - terraform providers
how to use this provider - terraform providers

In the example shown above for the Microsoft Azure provider azurerm, the provider source and version are specified in the required_providers section. The provider block then contains the configuration options required by the provider. For example, azurerm includes features , clientid , subscription_id , tenant_id amongst others. Usually, provider configuration options include various ways to authenticate to the platform.

Two meta-arguments can also be specified in the Providers block, version and alias . version is deprecated and should no longer be used, this should be specified in the required_providers block instead.

Multiple configurations of the same provider (aliases)

The alias meta-argument is useful when you need multiple configurations of the same provider. A common example is Azure, where the same Terraform configuration needs to work across multiple subscriptions.

See: How to Use a Terraform Provider Alias

In this setup, you configure the provider more than once, using different subscription_id values. Then you reference the provider configuration by its alias name.

provider "azurerm" {
  subscription_id = "4321-4321-4321-4321"
}
provider "azurerm" {
  alias = "subscriptionA"
  subscription_id = "1234-1234-1234-1234"
}

A resource can then explicitly select the provider configuration it should use:

resource "azurerm_resource_group" "rg1" {
  provider = azurerm.subscription_a
  name     = "rg-sub-a"
  location = "eastus"
}

If you configure a provider without an alias, Terraform treats it as the default configuration. Terraform uses the default unless you explicitly choose another provider configuration.

Passing providers into modules

If a child module needs a specific provider configuration, pass it using the providers meta-argument:

module "networking" {
  source = "./modules/networking"

  providers = {
    azurerm = azurerm.subscription_a
  }
}

If the module needs access to multiple configurations of the same provider, declare the aliases it expects and then pass them through.

In the module (./modules/networking):

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"

      # Tells Terraform this module can accept the aliased configuration.
      configuration_aliases = [azurerm.subscription_a]
    }
  }
}

In the root module:

module "networking" {
  source = "./modules/networking"

  providers = {
    azurerm                = azurerm
    azurerm.subscription_a = azurerm.subscription_a
  }
}

Providers are the link between your configuration and the target platform, so incomplete setup (or version download issues) can lead to errors such as “provider configuration not present” and “failed to query available provider packages.”

For a smoother experience, be sure to review how to fix missing provider configurations and how to resolve provider package query failures when these errors arise.

List of commonly used Terraform providers

Here are the most popular Terraform providers:

1. Azure

A cloud computing platform and service provided by Microsoft. It offers a wide range of cloud-based services and solutions, including infrastructure as a service (IaaS), platform as a service (PaaS), and software as a service (SaaS) that can be used for various computing, storage, analytics, databases, networking, machine learning, and application deployment needs.

Azure Resource Providers are components that Terraform uses to interact with Azure resources. Azure Resource Providers in Terraform correspond to different Azure services or resource types that you can manage and provision using Terraform configurations.

Each Azure service or resource type has its own resource provider in Terraform, which defines the Terraform resources and data sources that map to the corresponding Azure resources. The Azure provider for Terraform acts as the bridge between your Terraform configurations and the Azure API.

terraform {
 required_providers {
   azurerm = {
     source = "hashicorp/azurerm"
     version = "3.78.0"
   }
 }
}

provider "azurerm" {
 # Configuration options
}

2. AWS

Amazon Web Services (AWS) is a comprehensive cloud computing platform and service provided by Amazon. AWS offers a vast array of cloud-based computing resources, services, and tools that enable individuals, businesses, and organizations to build, deploy, and manage applications, websites, and services in a highly scalable, cost-effective manner.

Each AWS service or resource type corresponds to a specific resource provider in Terraform. These providers define Terraform resources and data sources that map to the corresponding AWS resources. The AWS provider for Terraform acts as the intermediary between your Terraform configurations and the AWS API.

terraform {
 required_providers {
   aws = {
     source = "hashicorp/aws"
     version = "5.23.0"
   }
 }
}

provider "aws" {
 # Configuration options
}

3. Google Cloud

Google Cloud, often referred to as Google Cloud Platform (GCP), is a comprehensive suite of cloud computing services provided by Google. It offers a wide range of cloud-based solutions for computing, storage, databases, machine learning, data analytics, and more.

The Google Cloud Terraform Provider is used to configure your Google Cloud Platform infrastructure. It is collaboratively maintained by the Google Terraform team at Google and the Terraform team at HashiCorp.

terraform {
 required_providers {
   google = {
     source = "hashicorp/google"
     version = "5.3.0"
   }
 }
}

provider "google" {
 # Configuration options
}

4. Kubernetes

Kubernetes, often abbreviated as K8s, is an open-source container orchestration platform for automating the deployment, scaling, and management of containerized applications. It was originally developed by Google and is now maintained by the Cloud Native Computing Foundation (CNCF).

Kubernetes provides a powerful, flexible framework for container management, making it easier to deploy and operate applications in a distributed, scalable manner.

The official Kubernetes Terraform provider is provided by HashiCorp. This provider allows you to define Kubernetes resources in your Terraform configurations, such as deployments, services, and config maps.

terraform {
 required_providers {
   kubernetes = {
     source = "hashicorp/kubernetes"
     version = "2.23.0"
   }
 }
}

provider "kubernetes" {
 # Configuration options
}

5. Oracle Cloud

Oracle Cloud, often referred to as Oracle Cloud Infrastructure (OCI), is a comprehensive cloud computing platform and infrastructure service offered by Oracle Corporation. It provides a wide range of cloud-based services, including infrastructure as a service (IaaS), platform as a service (PaaS), and software as a service (SaaS).

This provider is managed by Oracle open source directly (not Hashicorp directly — hence the source being oracle/oci).

terraform {
 required_providers {
   oci = {
     source = "oracle/oci"
     version = "5.18.0"
   }
 }
}

provider "oci" {
 # Configuration options
}

6. Alibaba Cloud

Alibaba Cloud, also known as Alibaba Cloud Computing or Aliyun, is the cloud computing and data intelligence arm of Alibaba Group, one of the world’s largest e-commerce and technology companies.

Alibaba Cloud provides a comprehensive range of cloud-based computing services, data storage, artificial intelligence (AI) and machine learning (ML) capabilities, big data analytics, and more. It is one of the leading cloud service providers in China and has a growing global presence.

Alibaba Cloud, also known as Aliyun, has an official provider for HashiCorp Terraform called alicloud.

terraform {
 required_providers {
   alicloud = {
     source = "aliyun/alicloud"
     version = "1.211.2"
   }
 }
}

provider "alicloud" {
 # Configuration options
}

See also: How to Use Terraform Local Provider

Other useful providers

The Spacelift Terraform provider is open source, and its README always contains the latest available documentation. It’s also distributed as part of our Docker runner image and available through our own provider registry.

See how it can be used to incorporate Spacelift resources into your Infrastructure as Code. You can also check out Spacelift for free by creating a trial account!

Moov logo - white

All-in-one payments platform Moov seeks to take the complexity out of embedding payment functionality in software, but its own Terraform provider was making IaC management unnecessarily complicated. By migrating to Spacelift, they’ve discovered the flexibility and responsiveness they need in a partner.

Spacelift customer case study

Read the full story

Key points

Understanding how providers work in Terraform and knowing how to make use of the available providers found in the Terraform Registry are fundamental concepts when learning Terraform.

Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.

Manage Terraform Better with Spacelift

Build more complex workflows based on Terraform using policy as code, programmatic configuration, context sharing, drift detection, resource visualization and many more.

Start free trial

Terraform Essential Components Cheat Sheet

Whenever you’re embarking on a new journey

or seeking to refine your foundational knowledge.

Share your data and download the cheat sheet