Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 2Name in the course: Objective 2: Terraform fundamentals

Install and use providers, and understand state

Providers are the plugins that let Terraform talk to a platform's API. State is Terraform's memory of what it built. This chapter shows how to choose and pin provider versions, use several providers and aliases, and why state matters.

  • 5 Lessons
  • 3 Commands

$ run as a normal user# run as root (or with sudo)

Skills in this chapter

  • Install, version and use providers (also several)

2.1

Install and pin providers

Declare providers in required_providers with a source and a version constraint. terraform init downloads them and writes the exact versions into .terraform.lock.hcl.

versions.tf
terraform {
  required_version = ">= 1.12"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 4.20"
    }
    random = {
      source  = "hashicorp/random"
      version = ">= 3.6, < 4.0"
    }
  }
}
ConstraintAllows
= 4.20.0Only this version
>= 4.20This and newer
~> 4.204.20 and newer 4.x (not 5.0)
~> 4.20.14.20.1 and newer 4.20.x
>= 3.6, < 4.0A range
terraform init
terraform init -upgrade
terraform providers
terraform version

Remember

Commit .terraform.lock.hcl to Git, so every person and the pipeline use the same provider versions. Do not commit the .terraform/ folder.

2.2

Know how Terraform uses providers

A provider turns resources in your code into API calls (create, read, update, delete). Each provider brings its own resource types and data sources.

  • Names are TYPE_NAME: azurerm_virtual_network, aws_s3_bucket, kubernetes_namespace.
  • The source address is HOSTNAME/NAMESPACE/TYPE — registry.terraform.io/hashicorp/azurerm (the hostname is usually left out).
  • Tiers in the Terraform Registry: official (HashiCorp), partner, community.

2.3

Use several providers and aliases

One configuration can use many providers — and the same provider several times with different settings, using an alias.

providers.tf
provider "azurerm" {
  features {}
  subscription_id = var.sub_prod
}
 
provider "azurerm" {
  alias           = "dr"
  features {}
  subscription_id = var.sub_dr
}
 
resource "azurerm_resource_group" "dr" {
  provider = azurerm.dr
  name     = "rg-web-dr"
  location = "northeurope"
}

Exam tip

Without provider = …, a resource uses the default (un-aliased) provider of its type.

2.4

Log in providers safely

Never write passwords or keys in .tf files. Use the platform's normal login or environment variables.

az login
export ARM_SUBSCRIPTION_ID=00000000-0000-0000-0000-000000000000
export ARM_USE_OIDC=true

In pipelines, use OIDC (workload identity federation): no secret is stored at all — see Deploy to Azure with OIDC.

2.5

Understand state

Terraform saves what it created in a state file (terraform.tfstate): the mapping between your code and the real resource IDs, plus their attributes.

State is used toExample
Map code to real objectsazurerm_resource_group.web = /subscriptions/…/resourceGroups/rg-web-dev
Know what to change or deleteA resource removed from code → destroy it
Speed up plansCached attributes
Track dependenciesDestroy in the right order
terraform state list
terraform show

Watch out

State can contain secrets in plain text (passwords, keys). Store it in a protected remote backend, never in Git. More: Manage state.

All commands in this chapter