Skip to content

Providers and the Terraform Plugin Model

Overview

Declare providers correctly, pin versions with required_providers, use aliases for multiple instances, and authenticate via environment or shared config — not committed secrets.

A provider is a plugin that teaches Terraform a resource schema and how to call an API. HashiCorp and partners publish providers on the Terraform Registry. Your root module pins source and version; init installs the binary; provider blocks configure regions, endpoints, and credentials.

This is a core tutorial in Module 5 · Providers of the REBASH Academy Terraform for Cloud & DevOps Engineers series — written for Cloud, DevOps, Platform, and SRE engineers.

Prerequisites

Learning Objectives

By the end of this tutorial, you will be able to:

  • Write required_providers with source and version constraints
  • Configure a provider block and explain default vs alias
  • List safe authentication patterns (env vars, OIDC, shared config)
  • Use the local provider for credential-free practice

Architecture

This topic’s control points and relationships are shown below.

Terraform providers

Theory

What it is

Terraform core does not know how to create an AWS VPC or a Kubernetes Deployment. Providers are separate plugins (often Go binaries) that implement the resource and data source types you use in HCL. Each provider has a Registry address such as hashicorp/aws or hashicorp/local.

You declare what you need in a terraform block:

terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.5"
    }
  }
}

A provider "local" {} block (sometimes empty) configures that plugin. For clouds you set region, assume-role, and similar arguments. Aliases (provider "aws" { alias = "dr" … }) let one configuration talk to multiple accounts or regions; resources select them with provider = aws.dr.

Why it matters

Provider version pins are production hygiene: a surprise major upgrade can rewrite plans. Multiple aliases are how real organisations model primary and disaster-recovery regions or shared-services vs workload accounts. Authentication design matters equally — CI should use short-lived roles (OIDC to cloud) rather than long-lived access keys in Git.

How it works

  1. Declare required_providers (source + version constraint).
  2. Run terraform init — CLI downloads plugins and records exact versions in .terraform.lock.hcl.
  3. Configure provider blocks (region, endpoints, default tags, aliases).
  4. Authenticate outside HCL when possible: AWS_PROFILE, ARM_CLIENT_ID, GOOGLE_APPLICATION_CREDENTIALS, kubeconfig, or workload identity.
  5. Resources inherit the default provider of their type, or an explicit provider = … meta-argument.

Illustrative alias (needs credentials — do not apply in this lab): provider "aws" { alias = "us"; region = "us-east-1" } then provider = aws.us on resources.

Key concepts and comparisons

Piece Responsibility
required_providers Which plugin + allowed versions
.terraform.lock.hcl Exact selected versions for the team
provider block Runtime configuration
Alias Extra named instance of the same provider
Credentials Usually env / SSO / OIDC — not Git

Common pitfalls

  • Hard-coding access keys in .tf files.
  • Omitting version constraints and letting init float unexpectedly.
  • Forgetting to pass provider = aws.alias on resources that must use a non-default instance.
  • Assuming one provider block covers every account — use aliases or separate root modules.
  • Committing .terraform/ provider binaries instead of the lock file.

Hands-on Lab

Create a workspace for this tutorial.

mkdir -p ~/rebash-terraform/module-05 && cd ~/rebash-terraform/module-05

Focus: Pin providers and inspect the plugin lock file

Step 1 – Declare required_providers and initialise

cat > main.tf <<'EOF'
terraform {
  required_version = ">= 1.5.0"
  required_providers {
    null = {
      source  = "hashicorp/null"
      version = "~> 3.2"
    }
  }
}
provider "null" {}
resource "null_resource" "plugin_demo" {}
EOF
terraform init
ls -la .terraform.lock.hcl .terraform/providers | head -n 20

Step 2 – Show provider versions and apply

terraform providers
terraform version
terraform apply -auto-approve
grep -A3 'provider "registry.terraform.io/hashicorp/null"' .terraform.lock.hcl || head -n 40 .terraform.lock.hcl

Final step – Cleanup note

terraform destroy -auto-approve
# Workspace kept for notes; remove with: rm -rf "$(pwd)" when finished

Validation

  • Lab commands run under ~/rebash-terraform/module-05/
  • You can explain each Theory section in your own words
  • You used modern tooling where it applies to this topic
  • You can describe one production failure mode for this topic

Code Walkthrough

Production practice for Providers and the Terraform Plugin Model always combines:

  1. Inspect before you change (status, plan, logs, dry-run)
  2. Prefer reversible, documented changes (Git, IaC, drop-ins, version pins)
  3. Capture evidence (command output, pipeline logs) for handovers
  4. Prefer current tools and APIs over legacy shortcuts
  5. Least privilege — escalate credentials only when required

Keep runbooks short enough to follow under pressure. Automate checks; keep humans for judgement.

Security Considerations

  • Treat credentials and tokens for terraform as privileged — never commit them
  • Prefer short-lived auth (OIDC, roles, SSO) over long-lived keys
  • Validate blast radius before apply/deploy/delete operations
  • Restrict who can approve production changes
  • Collect audit logs; limit who can read sensitive traces

Common Mistakes

Hard-coding access keys in .tf files.

Validate assumptions against the Theory section and official docs before changing production.

Omitting version constraints and letting init float unexpectedly.

Lab shortcuts (open security groups, admin roles, skip approvals) must not ship unchanged.

Changing production without a rollback path

Always know how to revert (previous artefact, prior release, state rollback, DNS failback).

Best Practices

  • Encode Providers and the Terraform Plugin Model changes as code and review them in pull requests
  • Pin versions (images, modules, actions, provider plugins)
  • Separate environments with clear promotion gates
  • Alert on symptoms with runbooks attached
  • Destroy lab resources; tag everything with owner and expiry where possible

Troubleshooting

Symptom Likely cause Fix
Auth / permission denied Wrong identity, policy, or scope Check caller identity, roles, and least-privilege policies
Timeout / no route Network, DNS, security group, or endpoint Trace path, DNS, and allow-lists before retrying
Drift / unexpected plan Manual change or wrong state/workspace Reconcile desired vs actual; avoid click-ops on managed resources
Pipeline/job red Flaky step, cache, or missing secret Read failing step logs; bisect recent workflow/config changes
Cost spike Idle load balancer, NAT, oversized compute Inventory billable resources; stop/delete labs promptly

Summary

Providers and the Terraform Plugin Model is essential for Cloud and DevOps engineers working with terraform. Practise the lab until the inspection and change path is muscle memory, then continue the track.

Interview Questions

  1. What does a Terraform provider plugin do?
  2. Why pin provider versions with required_providers and the lock file?
  3. What is the difference between provider source address and local name?
  4. How can overly loose version constraints cause production incidents?
  5. What happens during terraform init regarding plugins?

Sample answer — question 2

Pinning and committing .terraform.lock.hcl keeps plans reproducible across machines and CI. Without pins, new plugin releases can change behaviour unexpectedly.

Sample answer — question 4

Floating to the newest provider may introduce breaking resource schemas or behavioural changes during routine plans. Constrain versions and test upgrades deliberately.

References