Learning objectives
By the end of this module, you should be able to:
- Explain declarative infrastructure as code and why it improves repeatability.
- Describe what Terraform configuration, providers and state each contribute.
- Run the core CLI workflow safely with a provider-free training resource.
Infrastructure as code makes intent reviewable
Infrastructure as code represents desired infrastructure in version-controlled configuration. A change becomes a reviewable diff instead of an undocumented sequence of console clicks.
Terraform is declarative: configuration describes the desired result. Terraform compares configuration, prior state and provider observations to construct a plan.
Terraform has a predictable core workflow
terraform init prepares a working directory, terraform plan previews proposed changes, and terraform apply executes an approved plan. terraform destroy is an explicit teardown operation and deserves the same review discipline as creation.
State is part of the control system
Terraform state maps resource addresses in configuration to managed objects and cached attributes. State is operational data, not source code. Treat it as sensitive and protect concurrent writes.
Worked examples
See the idea in practice.
Provider-free first resource
terraform {
required_version = ">= 1.6, < 2.0"
}
resource "terraform_data" "hello" {
input = "gnu-academy"
}
output "message" {
value = terraform_data.hello.output
}
- terraform_data is built in and requires no external provider.
- It is ideal for learning lifecycle behavior without creating cloud infrastructure.
Hands-on lab
Lab — Execute the safe Terraform lifecycle
Goal: Initialize, format, validate, plan and apply a provider-free configuration.
Before you start
- Terraform CLI 1.6+; the course validator is aligned to Terraform 1.16.x.
- No cloud account or provider credentials are required.
STEP 1Create the configuration
mkdir -p ~/gnu-terraform-labs/module01 && cd ~/gnu-terraform-labs/module01
cat > main.tf <<'EOF'
terraform {
required_version = ">= 1.6, < 2.0"
}
resource "terraform_data" "hello" {
input = "gnu-academy"
}
output "message" {
value = terraform_data.hello.output
}
EOF
STEP 2Initialize and validate
terraform init
terraform fmt
terraform fmt -check
terraform validate
STEP 3Review the plan
terraform plan
STEP 4Apply and inspect output
terraform apply -auto-approve
terraform output message
Verify
- terraform validate reports success.
- The plan shows one terraform_data resource to add.
- The output resolves to gnu-academy.
Expected outcome
- You can explain what init, plan and apply do before using a cloud provider.
If it fails
- If terraform is not found, install the CLI and verify terraform version.
- If fmt -check fails, run terraform fmt and retry.
Real-world connectionProduction workflows usually save or remotely execute plans, require review for privileged changes, and keep state in a protected backend.
Avoid these traps
Common mistakes
- Running apply before reading the plan.
- Treating state as an ordinary text file to edit by hand.
- Assuming Terraform itself is a cloud provider.
Knowledge check
Can you explain it without looking back?
Open each answer only after you have tried to answer the question yourself.
1What does terraform plan do?
It calculates and displays proposed changes without applying them.
2Why is configuration called declarative?
It describes the desired end state rather than scripting every imperative step.
3Why protect state?
It maps managed objects and can contain sensitive infrastructure data.