Lesson 1 of 3 Terraform Providers

How Terraform Uses Providers

Providers are the plugins that give Terraform its knowledge of each platform. This lesson explains the plugin architecture, how providers are found and installed, and how they are configured.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the plugin architecture that separates Terraform core from providers.
  • Describe how providers are found, tiered and installed.
  • Identify safe ways to supply provider credentials.

The plugin architecture

Terraform core contains no code for any particular cloud. Everything platform-specific lives in providers, separate executable plugins. When a run starts, core launches each provider the configuration needs as a child process and communicates with it through remote procedure calls (RPC). Core asks the provider for its schema, asks it to validate configuration, to plan a change to a resource and finally to apply it; the provider makes the actual API calls.

A provider contributes three kinds of things. Resource types, such as aws_s3_bucket, describe objects Terraform manages. Data sources, such as aws_ami, describe information Terraform reads. Since Terraform 1.8, some providers also offer provider-defined functions, called as provider::aws::arn_parse(...). The part of a resource type before the first underscore normally names its provider.

Resource type
A kind of managed object defined by a provider, such as aws_instance or azurerm_resource_group.
Data source
A kind of read-only lookup defined by a provider, such as aws_caller_identity.
Provider-defined function
A function added by a provider, called with the provider:: prefix.
Sort into groups

Core or provider?

Exam skills 2.2.1

Decide whether Terraform core or a provider does each job.

Log in to try this activity Interactive activities are free with an account.

Independent versioning

Because providers are separate binaries, they are released on their own schedules. A cloud vendor can add a new service to its provider in a weekly release without waiting for a Terraform release, and Terraform core can improve the language without touching providers. The cost is that each configuration must say which provider versions it works with, which the next lesson covers.

Finding and installing providers

Providers are distributed through the Terraform Registry at registry.terraform.io, which labels each with a tier. Official providers, in the hashicorp namespace, are owned and maintained by HashiCorp. Partner providers are written and maintained by technology companies that are members of the HashiCorp partner programme. Community providers are published by individuals or groups. The tier indicates who maintains the provider, which matters for support and trust.

terraform init reads every module's provider requirements, selects versions and downloads the packages into .terraform/providers inside the working directory. Large providers can be hundreds of megabytes, so setting the TF_PLUGIN_CACHE_DIR environment variable to a shared directory avoids downloading the same provider for every working directory. Organisations without internet access configure a provider mirror in the CLI configuration file.

The following diagram shows how Terraform infers a provider source and why a partner provider needs a required_providers entry.

Why init tries to install hashicorp/cloudflare. Terraform infers the provider from the resource type prefix, cloudflare; with no required_providers entry it assumes the hashicorp namespace, but the Cloudflare provider is a partner provider published as cloudflare/cloudflare. Declaring the source in required_providers fixes it.
The Cloudflare provider is an example of a partner provider.
Match the pairs

Provider tiers and installation

Exam skills 2.2.2

Match each term about provider distribution with its meaning.

Log in to try this activity Interactive activities are free with an account.

Configuring a provider

A provider block supplies settings, such as a region, an endpoint or default tags. Provider blocks belong in the root module; reusable modules receive provider configurations from their callers.

provider "aws" {
  region = "eu-west-1"

  default_tags {
    tags = { ManagedBy = "terraform" }
  }
}

Credentials should not appear in provider blocks, because configuration files are committed to version control. Providers read credentials from environment variables, such as AWS_ACCESS_KEY_ID or ARM_CLIENT_ID, from the platform's shared configuration files, from an instance or workload role, or from HCP Terraform dynamic credentials. If a provider needs no settings, the block can be omitted.

Never hard-code credentials

Hard-coded access keys in a provider block end up in version control and in every copy of the repository. Use environment variables, credential files or dynamic credentials instead.

Select all that apply

Supplying credentials

Exam skills 2.2.3

Identify the sound ways to give a provider its credentials.

Log in to try this activity Interactive activities are free with an account.
Worked example

An unknown resource type

An engineer adds a cloudflare_dns_record resource to a configuration that already uses AWS and runs terraform plan. Terraform reports that the provider is not installed and suggests running init. After init, it tries to install hashicorp/cloudflare and fails. Why?

The decisive facts are how Terraform finds a provider and the default namespace.

Terraform infers the provider from the resource type prefix, cloudflare. With no required_providers entry, it assumes the source hashicorp/cloudflare, but the Cloudflare provider is a partner provider published as cloudflare/cloudflare. Adding cloudflare = { source = "cloudflare/cloudflare", version = "~> 5.0" } to required_providers and running terraform init installs the correct provider.

Check yourself

Which source address does Terraform assume for a provider used without a required_providers entry?

Show answer

hashicorp/<local name> on registry.terraform.io, which is wrong for partner and community providers.

Exam Tips

Remember for the exam

  • Providers are plugins that core runs over RPC; they add resource types, data sources and functions.
  • Tiers: Official (HashiCorp), Partner, Community.
  • init installs providers into .terraform/providers.
  • Keep credentials out of provider blocks.