Advanced
Searching inSummaries
How to search
  • Words find lesson titles, text and code. Filter by type (guides, courses, programs, books) or stay in this book.
  • book:rhcsa and ch:8 narrow the search; #code finds a word only in commands of the lessons.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Course 9Name in the course: Crush the Terraform Associate

Crush the Terraform Associate

Course 9 of 13 in the Alta3 Academy library. Alta3 Academy self-paced course.

Note

My result — Lessons read: all 56/56; Course progress: 100%.

  • 24 Lessons
  • 14 Commands
  • 2 Labs

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

9.1

What the course covers

What Terraform is. It is an infrastructure as code (IaC) tool. You write files, and Terraform builds the real infrastructure.

  • HCL (blocks, arguments, labels, and data types), and the core CLI loop: init, plan, apply, and destroy.
  • Variables, locals, and output values. These make a module reusable.
  • Modules. A root module calls child modules, like a playbook calls a role.
  • State files, workspaces, terraform state mv, and resource drift.
  • Providers used in the labs: Docker, AWS, Azure, Google Cloud, HTTP, random, time, and null.
  • Team tools: Terraform Cloud, OPA, GitLeaks, GitHub Actions, and Terragrunt.
  • Exam practice. The last part is a large mock exam for the HashiCorp Terraform Associate test.

9.2

Welcome to Alta3 Research Labs!

The course starts in the Alta3 lab environment. It is a Linux desktop with a terminal.

  • VIRGIL is the lab coach. You can ask it about the lab you are on, about code, and about errors.
  • The lessons show how to copy and paste commands into the terminal, and list basic Linux commands.

9.3

Required Tools

Vim is a modal editor. Press i to insert text, Esc to stop, :wq to save and quit, :q! to quit and drop changes.

  • Tmux is a terminal multiplexer. You press Ctrl-B and then a key to give it a command.
  • Ctrl-B then % splits the screen side by side. Ctrl-B then " splits it top and bottom. This helps you run a plan in one pane and edit code in another.

9.4

Source Code Management

You pick GitHub or GitLab. Both work for this course. Git runs on your machine; GitHub and GitLab host the copy online.

  • You create a public repository called mycode, with a README and a license. This stores your Terraform code.
  • A .gitignore file keeps secrets out of git. For Terraform it ignores **/.terraform/*, *.tfstate, terraform.tfvars, and *.auto.tfvars.
  • The state file can hold passwords and resource IDs, so it must never be committed.

9.5

Introduction to Terraform

Terraform is declarative. You describe the end state, and Terraform works out the steps.

9.6

Terraform Modules

HCL has primitive types (string, number, bool), collection types (list, set, map), and structural types (tuple, object). null means "leave it out".

  • A block looks like <TYPE> <LABEL> { ... }. Common block types are terraform, provider, resource, data, variable, output, locals, and module.
  • A first project uses the kreuzwerker/docker provider. It makes an nginx image and a container, then maps port 8000 on the host to port 80 in the container.
  • terraform init downloads the provider. terraform plan shows the change. terraform apply builds it after you type yes. docker ps proves the container runs.

Inputs and outputs

  • Variables live in a variable block and are read as var.name. They can have default, type, description, validation, sensitive, and nullable.
  • Locals give a name to an expression, so you do not repeat it. Think of a module as a function, variables as arguments, outputs as return values, and locals as temporary variables.
  • Output values are printed at the end of a run. They are also the way a parent module reads data from a child module.
  • The course warns against the :latest image tag. It is only a default tag, it can hide the real version, and rollback becomes hard.

9.7

Beyond Basics

Workspaces keep separate state for the same code. default always exists, and a new workspace is selected as soon as you create it.

Module inputs, data, and import

  • A child module is a folder with its own main.tf, variables.tf, and outputs.tf. The root module calls it with a module block and can override its inputs.
  • terraform.tfvars and *.auto.tfvars load automatically. Other files need -var-file. Command line -var wins over all files.
  • Data sources only read data. The http data source sends a GET and returns response_body, response_headers, and status_code.
  • Import takes control of infrastructure that already exists. You write the import block and a matching resource block, and the real IDs are added to terraform.tfstate.

9.8

Loops

count repeats a resource a number of times. for_each repeats it once per item in a map or set.

  • The course prefers for_each. Each object gets a stable key, so changing the list does not rebuild every object.
  • The lab uses for_each = toset(local.jones) on a null_resource, then adds a name to the set to show a second object appear.
  • terraform fmt fixes layout, and terraform validate checks the syntax.

9.9

Provisioning

A local-exec provisioner runs a command on the machine that runs Terraform. It is a last resort, not a normal design.

  • Its inputs are command, working_dir, interpreter, environment, and when (for example when = destroy).
  • The time_sleep resource adds a delay with create_duration and destroy_duration. depends_on forces an order when Terraform cannot infer it. The lesson says to treat both as a last resort, not a normal design.
  • templatefile(path, vars) renders a .tftpl file. The lab builds a websocket backend list with %{ for addr in ip_addrs ~}.

9.10

Terraform Cloud

Terraform Cloud is the hosted version. Terraform Enterprise is the same software, self-hosted.

  • It gives teams remote state, shared secrets, access control, and a record of every run.
  • terraform login stores an API token. A CLI-driven workspace runs plans in the cloud, not on your laptop.
  • A workspace can watch a Git repository. A new commit starts a run, so a merge becomes a real deploy.

9.11

Dynamic Blocks

A dynamic block builds repeated nested blocks from a for_each. It keeps the code DRY.

  • It works inside resource, data, provider, and provisioner blocks.
  • The lab makes several ports blocks for a Docker container from a list, instead of writing each one by hand.
  • The docs warn that too many dynamic blocks make code hard to read. Write plain blocks when you can.

9.12

AWS

Credentials come from environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_DEFAULT_REGION. Never put them in the code.

  • The hashicorp/aws provider builds an aws_instance. The free tier uses t2.micro.
  • Outputs print the instance ID and public IP. terraform state list shows what is tracked.

Loops and drift

  • One lab creates several IAM users with count, then rewrites it with for_each. The lesson explains the difference between collection types and structural types.
  • Drift means the real cloud no longer matches the state. The fix is terraform refresh, then a new plan and apply.
  • The AWS CLI (aws configure, aws ec2 ...) is used to make a manual change, which creates the drift on purpose.

9.13

Azure

Terraform talks to Azure through the Azure CLI. You install it, run az login, and pick the subscription with az account set --subscription "<id>".

  • The azurerm provider then builds a resource group. The lab checks the portal, then destroys the group.
  • The same rule applies: add a .gitignore before you commit, because the state can hold credentials.

9.14

Google Cloud Platform

You install the Google Cloud CLI, log in, and pick a project.

  • The google provider builds a compute instance. The lab ends with terraform destroy and a check in the console.
  • Each cloud lab repeats the same pattern: provider, auth, one resource, output, destroy.

9.15

Terraform and Enterprise

One lab deploys a small Go REST API in a Docker container with Terraform. The API answers /, /ping, and /spock, and listens on HTTP_PORT (default 9876).

  • The "Terraform vs. Ansible" lesson draws the line: Terraform builds infrastructure, Ansible configures it.
  • The next lab proves it. Terraform creates the containers and network, then Ansible installs and configures software inside them.
  • This is the common real pattern: provision first, configure second.

9.16

Helpful DevOps Tools (OPTIONAL)

Open Policy Agent (OPA) checks a plan before it is applied. The lab saves terraform plan --out tfplan.binary, turns it into JSON, and tests it against a policy.

  • GitLeaks is a SAST tool. It finds hardcoded secrets in a repository, and a GitHub Action runs it on every push.
  • A GitHub Actions workflow can run terraform plan on a pull request and terraform apply after the merge, with Terraform Cloud holding the state.
  • Terragrunt is a thin wrapper around Terraform. It keeps configurations DRY, works with many modules, and manages remote state.

9.17

Terraform Associate Exam

The mock exam has more than 160 multiple-choice questions with folded answers.

  • It covers terraform fmt, state and drift, remote backends, providers, variables, and the first command to run in a new directory (terraform init).
  • The exam style is "pick the best answer", so the explanations matter as much as the score.

9.18

Continuing Education

The last talk is about Alta3 planning and consulting. It is not a technical lesson.

  • It suggests using a subject matter expert to apply the new skills in your own company.

9.19

Appendix

The glossary defines the core words: API, apply, attribute, backend, block, CI/CD, data source, HCL, IaC, module, provider, registry, resource, root module, and state.

  • A curriculum map shows where Terraform leads next, for example Ansible, Packer, Jenkins, Kubernetes, and Rancher.

9.20

Key commands and tools

9.21

Key terms

The words of this course in one table. Learn them before the quiz or exam.

TermMeaning
ApplyThe step that makes the planned changes real.
ArgumentA name = value line inside a block. It sets one setting.
BackendThe place where state is stored, for example locally, in S3, or in Terraform Cloud.
BlockA group of lines in HCL. It has a type (like resource), maybe a label, and a body in { }.
Child moduleA module called by the root module or by another module.
Data sourceA read-only lookup. It gets data but does not create anything.
DestroyThe command that removes the resources in the state.
DriftA difference between the real infrastructure and the state file, often after a manual change.
Dynamic blockA block that builds repeated nested blocks from a for_each, to keep code DRY.
HCLHashiCorp Configuration Language. The normal language of Terraform files (*.tf).
ImportTaking control of an object that already exists, so it is added to the state.
Infrastructure as Code (IaC)Managing servers and cloud resources with files, not by hand in a web page.
Local valueA name for an expression inside a module. You read it as local.name.
ModuleA folder of Terraform code. A module can be called by other code.
Output valueData a module sends back. Terraform prints it at the end of a run.
PlanA preview of the changes Terraform will make.
ProviderA plugin that lets Terraform talk to one platform, for example Docker, AWS, or Azure.
ProvisionerA block that runs a local or remote command after a resource is created. It is a last resort.
RegistryThe public library of providers and modules, at registry.terraform.io.
ResourceA real object Terraform creates and manages, such as a container or a server.
Root moduleThe module in the folder where you run Terraform.
State fileterraform.tfstate. Terraform's list of the real objects it built and their IDs.
tfvars fileA file of variable values. terraform.tfvars and *.auto.tfvars load by themselves.
VariableAn input value. You declare it in a variable block and read it as var.name.
WorkspaceA separate state inside the same folder. The default workspace always exists.

9.22

Check yourself

Answer each question first, then open it to check the answer.

1What is the difference between terraform plan and terraform apply?

plan only shows the changes. It changes nothing. apply makes the changes real, after you confirm.

2Why should you avoid the :latest image tag?

:latest is only a default tag. It does not always point to the newest image. You cannot tell which version runs, and rollback is hard. Use a version like 1.23.4.

3What is the difference between a variable, a local, and an output?

A variable is an input from outside the module. A local is a name for an expression inside the module. An output is data the module sends back, and it is printed after a run.

4When do you use count, and when do you use for_each?

Use count for identical copies. Use for_each when each copy needs its own key and values. The course prefers for_each.

5What is state drift, and how do you fix it?

Drift means the real infrastructure no longer matches the state, often after a manual change. Run terraform refresh, then plan, then apply to bring them back together.

Practice

All commands in this chapter