DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

TerraformLesson 3 of 17
Your progress · 0%
On this page
  1. What a provider is
  2. The Terraform Registry
  3. Configuring providers
  4. Version pinning
↖ Course roadmap
Lesson 3 · Providers

Providers and version pinning

What a provider is, how to find one on the Terraform Registry, configure it with regions and aliases, and pin its version so every machine builds the same thing.

25 min read · beginner · hands-on

By the end of this lesson you'll be able to

0 of 5 complete
Lab status

Verified lab: terraform init and terraform providers with pinned providers. Run on Terraform 1.17.1; the output shown is real.
Code walkthrough: the AWS provider block, default_tags and the second-region alias: explained, not run.
Cloud lab: the alias example needs an AWS account to apply.

Terraform itself can't create a single server or file. Everything it manages goes through a provider, a plugin that knows one platform's API. This lesson shows where providers come from, how to configure them, and how to pin their versions so your laptop, your teammate's laptop and the CI runner all build the same thing.

What a provider is

Core plans and orders the work; each provider turns actions into API calls for its platform.© Diagram: DevOps Academy

Core plans and orders the work; each provider turns actions into API calls for its platform.© Diagram: DevOps Academy

When you run terraform apply:

  1. Terraform core reads your .tf files and the state, and builds a dependency graph.
  2. For each resource, it asks that resource's provider to plan the change: create, update, replace or nothing.
  3. On apply, core tells the provider "create this", and the provider makes the real API call (for AWS, RunInstances to start an EC2 instance) and returns the result.

Providers are separate programs, released on their own schedule. The AWS provider adds new AWS services every week without any change to Terraform itself.

The Terraform Registry

registry.terraform.io lists thousands of providers. Each has a source address of the form namespace/name, for example hashicorp/aws, hashicorp/random or cloudflare/cloudflare. Each also has a tier:

TierPublished byExample
OfficialHashiCorphashicorp/aws, hashicorp/local
PartnerThe technology's own company, verified by HashiCorpcloudflare/cloudflare, datadog/datadog
CommunityAnyoneNiche tools; check activity and maintainers

Every provider page has documentation for each resource and data source: its arguments (inputs), its attributes (outputs), and examples. You'll spend more time on these pages than anywhere else. Search for the resource, copy the example, then remove what you don't need.

Configuring providers

Two blocks are involved. required_providers (inside terraform {}) says which provider and which versions. The provider block says how to configure it:

# versions.tf
terraform {
  required_version = ">= 1.11"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~> 3.7"
    }
  }
}

# providers.tf
provider "aws" {
  region = "ap-south-1"

  default_tags {
    tags = {
      project    = "shop"
      managed_by = "terraform"
    }
  }
}

default_tags is an AWS-provider feature that adds the same tags to every resource it creates, so nobody forgets the owner tag. Providers like random and local need no configuration at all.

Credentials stay out of the code

Never put access keys in a provider block. Providers read the standard places their platform's tools use. For AWS, that's environment variables (AWS_PROFILE, AWS_REGION), the shared config in ~/.aws/, SSO sessions, or an IAM role when running in AWS or in CI through OIDC (the CI/CD lesson covers it). Keys in .tf files end up in Git history and in the state file.

Several configurations with alias

One configuration can use the same provider twice, for example to put a TLS certificate in us-east-1 (CloudFront requires it there) while everything else is in Mumbai:

provider "aws" {
  region = "ap-south-1"
}

provider "aws" {
  alias  = "us_east_1"
  region = "us-east-1"
}

resource "aws_acm_certificate" "cdn" {
  provider          = aws.us_east_1 # use the aliased configuration
  domain_name       = "shop.example.com"
  validation_method = "DNS"
}

Resources without a provider argument use the default (un-aliased) configuration.

Version pinning

Providers change. A new major version can rename arguments or change defaults, and an unpinned project can behave differently on Monday than it did on Friday. A version constraint sets which versions are allowed:

ConstraintAllowsTypical use
"= 2.9.1"exactly 2.9.1Rarely; blocks even security fixes
">= 2.9"2.9 and anything newer, even 3.xLibraries that must stay flexible
"~> 2.9">= 2.9, < 3.0The usual choice for providers
"~> 2.9.0">= 2.9.0, < 2.10.0Only patch releases
">= 1.11, < 2.0"a rangerequired_version for Terraform itself

~> is the "pessimistic" operator: only the rightmost number you wrote may increase. Pair the constraint with the lock file from the next lesson, which records the exact version that was chosen. The constraint says what's allowed; the lock file says what's actually used.

Check what a project needs with terraform providers, which printed this in the lab:

$ terraform providers

Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/random] ~> 3.7
└── provider[registry.terraform.io/hashicorp/local] ~> 2.9
terraform providers
  • terraforminfrastructure as code

registry.terraform.io/hashicorp/random is the full source address; you write the short form hashicorp/random and Terraform fills in the registry host.

In production

Pin every provider with ~> on the major version, and pin Terraform with required_version. Upgrade on purpose: bump the constraint in a pull request, run terraform init -upgrade and read the plan carefully, especially after a major provider release, which can come with an upgrade guide.

⌘
Mini mission

Write a pinned versions.tf

Create a versions.tf that requires Terraform 1.11 or newer and pins hashicorp/local to 2.x and hashicorp/random to 3.x using the pessimistic operator. Run terraform init and terraform providers, and confirm the installed versions fall inside your constraints.

Input: An empty folderOutput: versions.tf
Reveal one safe solution
$ terraform init | grep Installed
$ terraform providers
$ grep -A2 'provider "registry' .terraform.lock.hcl
terraform init | grep Installed
  • terraforminfrastructure as code
  • initdownload providers, set up the folder
  • |pipe: send output to the next command
  • grepfind lines that match a pattern

On the lab, init installed hashicorp/random v3.9.1 for ~> 3.7 and hashicorp/local v2.9.1 for ~> 2.9. Newer patch or minor versions are fine, as long as the major version matches your constraint.

Try it yourself

0 of 5 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 4

What does a provider do?

Go deeper

Official reading for this lesson

  • DocsProviders overviewHashiCorp ↗
  • DocsProvider requirements (required_providers)HashiCorp ↗
  • DocsVersion constraintsHashiCorp ↗
  • DocsProvider configuration and aliasesHashiCorp ↗
  • ToolTerraform RegistryHashiCorp ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

← PreviousHCL: the Terraform languageNext lesson →terraform init and the lock file