$ run as a normal user# run as root (or with sudo)
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, anddestroy. - 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.
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.
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-Band then a key to give it a command. Ctrl-Bthen%splits the screen side by side.Ctrl-Bthen"splits it top and bottom. This helps you run a plan in one pane and edit code in another.
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
.gitignorefile 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.
Introduction to Terraform
Terraform is declarative. You describe the end state, and Terraform works out the steps.
- A configuration can be HCL in
*.tffiles, or JSON in*.tf.jsonfiles. HCL is the normal choice. - The install lesson adds the HashiCorp apt repository and its GPG key, then runs
sudo apt-get install terraform. terraform -helplists commands.terraform -help planexplains one command.
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 areterraform,provider,resource,data,variable,output,locals, andmodule. - A first project uses the
kreuzwerker/dockerprovider. It makes an nginx image and a container, then maps port 8000 on the host to port 80 in the container. terraform initdownloads the provider.terraform planshows the change.terraform applybuilds it after you typeyes.docker psproves the container runs.
Inputs and outputs
- Variables live in a
variableblock and are read asvar.name. They can havedefault,type,description,validation,sensitive, andnullable. - 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
:latestimage tag. It is only a default tag, it can hide the real version, and rollback becomes hard.
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.
terraform consoleis a quick calculator for HCL. The lab usestype(),tolist(), andtomap()to show how types convert.- A resource marked "tainted" is rebuilt on the next apply. Since v0.15.2 the better command is
terraform apply -replace="aws_instance.example[0]". terraform state mvrenames or moves a resource in state without destroying the real object.
Module inputs, data, and import
- A child module is a folder with its own
main.tf,variables.tf, andoutputs.tf. The root module calls it with amoduleblock and can override its inputs. terraform.tfvarsand*.auto.tfvarsload automatically. Other files need-var-file. Command line-varwins over all files.- Data sources only read data. The
httpdata source sends a GET and returnsresponse_body,response_headers, andstatus_code. - Import takes control of infrastructure that already exists. You write the
importblock and a matchingresourceblock, and the real IDs are added toterraform.tfstate.
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 anull_resource, then adds a name to the set to show a second object appear. terraform fmtfixes layout, andterraform validatechecks the syntax.
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, andwhen(for examplewhen = destroy). - The
time_sleepresource adds a delay withcreate_durationanddestroy_duration.depends_onforces 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.tftplfile. The lab builds a websocketbackendlist with%{ for addr in ip_addrs ~}.
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 loginstores 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.
Dynamic Blocks
A dynamic block builds repeated nested blocks from a for_each. It keeps the code DRY.
- It works inside
resource,data,provider, andprovisionerblocks. - The lab makes several
portsblocks 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.
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/awsprovider builds anaws_instance. The free tier usest2.micro. - Outputs print the instance ID and public IP.
terraform state listshows what is tracked.
Loops and drift
- One lab creates several IAM users with
count, then rewrites it withfor_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.
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
azurermprovider then builds a resource group. The lab checks the portal, then destroys the group. - The same rule applies: add a
.gitignorebefore you commit, because the state can hold credentials.
Google Cloud Platform
You install the Google Cloud CLI, log in, and pick a project.
- The
googleprovider builds a compute instance. The lab ends withterraform destroyand a check in the console. - Each cloud lab repeats the same pattern: provider, auth, one resource, output, destroy.
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.
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 planon a pull request andterraform applyafter 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.
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.
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.
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.
Key commands and tools
terraform init— download providers and set up the working directory.terraform plan— show what will change.terraform apply— build the change. Add--auto-approvein labs only.terraform destroy— remove everything in the state.terraform fmt/terraform validate— style and syntax checks.terraform console— test expressions and functions.terraform workspace list | new | select | show | deleteterraform state list/terraform state mv OLD NEWterraform apply -replace="aws_instance.example[0]"— rebuild one object.terraform apply -var-file="testing.tfvars"/-var "key=value"terraform import(or animportblock) — adopt existing infrastructure.terraform login— connect the CLI to Terraform Cloud.terraform show -json— turn a saved plan into JSON for OPA.docker ps,docker build -t name:tag .,docker tag,docker imagesgit clone,git commit,git push— the SCM labs.export AWS_ACCESS_KEY_ID=...,export AWS_SECRET_ACCESS_KEY=...,export AWS_DEFAULT_REGION=us-west-2aws configure,aws --version— the AWS CLI for the drift lab.az login,az account set --subscription "<id>"— Azure CLI.gcloud— Google Cloud CLI.opa— policy engine.terragrunt— Terraform wrapper.curl,wget,unzip,tree,vim,tmux— support tools used in the labs.
Key terms
The words of this course in one table. Learn them before the quiz or exam.
| Term | Meaning |
|---|---|
| Apply | The step that makes the planned changes real. |
| Argument | A name = value line inside a block. It sets one setting. |
| Backend | The place where state is stored, for example locally, in S3, or in Terraform Cloud. |
| Block | A group of lines in HCL. It has a type (like resource), maybe a label, and a body in { }. |
| Child module | A module called by the root module or by another module. |
| Data source | A read-only lookup. It gets data but does not create anything. |
| Destroy | The command that removes the resources in the state. |
| Drift | A difference between the real infrastructure and the state file, often after a manual change. |
| Dynamic block | A block that builds repeated nested blocks from a for_each, to keep code DRY. |
| HCL | HashiCorp Configuration Language. The normal language of Terraform files (*.tf). |
| Import | Taking 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 value | A name for an expression inside a module. You read it as local.name. |
| Module | A folder of Terraform code. A module can be called by other code. |
| Output value | Data a module sends back. Terraform prints it at the end of a run. |
| Plan | A preview of the changes Terraform will make. |
| Provider | A plugin that lets Terraform talk to one platform, for example Docker, AWS, or Azure. |
| Provisioner | A block that runs a local or remote command after a resource is created. It is a last resort. |
| Registry | The public library of providers and modules, at registry.terraform.io. |
| Resource | A real object Terraform creates and manages, such as a container or a server. |
| Root module | The module in the folder where you run Terraform. |
| State file | terraform.tfstate. Terraform's list of the real objects it built and their IDs. |
| tfvars file | A file of variable values. terraform.tfvars and *.auto.tfvars load by themselves. |
| Variable | An input value. You declare it in a variable block and read it as var.name. |
| Workspace | A separate state inside the same folder. The default workspace always exists. |
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.
Useful links
Official documentation and pages for this course.
- Alta3 Research — training, planning, and consulting.
- Alta3 Research on YouTube — free videos.
- Alta3 Research on GitHub — lab code and examples.
- Terraform downloads
- Terraform CLI docs
- Terraform Registry
- Terraform Cloud
- Open Policy Agent
- Terragrunt