Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 2Naam in de cursus: 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 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Vaardigheden in dit hoofdstuk

  • Providers installeren, versies vastleggen en gebruiken (ook meerdere)

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

Onthoud

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"
}

Examentip

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

Let op

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

Alle commando's in dit hoofdstuk