Skip to content
Hands-on IT IT Drills

Day 0: The Training Ground

Goal: Create the lab VM, install Docker, OpenTofu, and k3s, fetch the companion code, and build the notes image. · Duration: approx. 45 min · Prerequisite: none

Briefing

  • You work in the Ubuntu 24.04 VM tf, as user ubuntu in ~/tf-lab. Everything OpenTofu manages runs on this VM: containers in Docker, later pods in k3s.
  • OpenTofu (tofu) reads configuration in HCL, compares it with what it has already created (the state), and proposes the necessary changes.
  • Providers are plugins that talk to an API: Docker, Kubernetes, Helm, Keycloak. OpenTofu downloads them from the registry during tofu init.
  • notes is the application that is built as code in this book: notes-api (Go) with Postgres, later with login through Keycloak.
 ~/tf-lab/tagNN/*.tf ─► tofu plan/apply ─► Provider docker      ─► Docker on the VM
                                         ─► Provider kubernetes ─► k3s on the VM
                       terraform.tfstate ◄┘ (what was created)

Terraform: OpenTofu was created in 2023 as a fork of Terraform when HashiCorp switched the license to the BSL. OpenTofu is licensed under the MPL 2.0 and belongs to the Linux Foundation. Configuration, providers, and state are compatible; where something differs, there’s a box like this one. Instead of tofu you then type terraform.

Drill 1: The Lab VM

On your laptop (Multipass from multipass.run):

multipass launch 24.04 --name tf --cpus 4 --memory 8G --disk 40G
multipass exec tf -- bash -c "echo '$(cat ~/.ssh/id_ed25519.pub)' \
  >> ~/.ssh/authorized_keys"
multipass info tf | grep IPv4
ssh ubuntu@<IP from the output>

From here on, everything runs inside the VM.

Save your progress? Sign up for free

Drill 2: Docker

sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
  -o /etc/apt/keyrings/docker.asc
sudo tee /etc/apt/sources.list.d/docker.sources >/dev/null <<'EOF'
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: noble
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF
sudo apt-get update -qq
sudo apt-get install -y -qq docker-ce docker-ce-cli containerd.io \
  docker-compose-plugin jq git unzip >/dev/null
sudo usermod -aG docker ubuntu
  • First the key Docker uses to sign its packages, then the package source. noble is the codename of Ubuntu 24.04.
  • usermod -aG docker allows the user ubuntu to use Docker without sudo.

Log out (exit) and back in so the docker group takes effect:

docker --version

Expected:

Docker version 29.9.2, build a82e528

Save your progress? Sign up for free

Drill 3: OpenTofu

OpenTofu is available as a package for Debian and Ubuntu:

ARCH=$(dpkg --print-architecture)
cd /tmp
U=https://github.com/opentofu/opentofu/releases/download/v1.13.2
curl -fsSLO $U/tofu_1.13.2_${ARCH}.deb
sudo dpkg -i tofu_1.13.2_${ARCH}.deb >/dev/null
tofu version

Expected:

OpenTofu v1.13.2
on linux_amd64

A single program, no runtime, no database. Everything else is downloaded by tofu init per project.

Save your progress? Sign up for free

Drill 4: k3s

From Day 6 on, OpenTofu also manages Kubernetes. k3s is a complete Kubernetes in a single program:

curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=v1.36.5+k3s1 \
  sh -s - --write-kubeconfig-mode 644 2>&1 | tail -1
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
sleep 20
kubectl get nodes

Expected:

[INFO]  systemd: Starting k3s
NAME   STATUS   ROLES           AGE   VERSION
tf     Ready    control-plane   20s   v1.36.5+k3s1

If it still says NotReady, wait a few seconds and ask again.

Save your progress? Sign up for free

Drill 5: Companion Code and notes

mkdir -p ~/tf-lab/setup
git clone -q https://github.com/thomaszachmann/opentofu-zum-anfassen.git ~/tf-lab/code
cp ~/tf-lab/code/setup/env.sh ~/tf-lab/setup/
echo 'source ~/tf-lab/setup/env.sh' >> ~/.bashrc
source ~/tf-lab/setup/env.sh
grep -v '^#' ~/tf-lab/setup/env.sh
cd ~/tf-lab/code/notes/notes-api
docker build -q --build-arg VERSION=0.4.0 -t notes-api:0.4.0 .
docker images notes-api:0.4.0

Expected (IDs differ):

export LAB="$HOME/tf-lab"
export VM_IP="${VM_IP:-$(hostname -I | awk '{print $1}')}"   # erste IP der VM
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml                  # k3s (ab Tag 6)
export TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache" # Provider nur einmal laden
mkdir -p "$TF_PLUGIN_CACHE_DIR"
export NOTES_API_KEY="lab-api-key-123"                       # nur Labor
true
sha256:23990cbdfb04122f8111cb14466ef203c08e967f414763c0ecd258814083cbd7
IMAGE             ID             DISK USAGE   CONTENT SIZE   EXTRA
notes-api:0.4.0   23990cbdfb04       16.4MB          4.51MB

TF_PLUGIN_CACHE_DIR makes OpenTofu download each provider only once, even though every training day has its own directory. From now on, every new shell loads env.sh automatically.

Save your progress? Sign up for free

Checkpoint

tofu version | head -1

Expected:

OpenTofu v1.13.2
kubectl get nodes

Expected: one node with status Ready.

NAME   STATUS   ROLES           AGE   VERSION
tf     Ready    control-plane   5m    v1.36.5+k3s1
docker images notes-api:0.4.0

Expected (the ID differs):

IMAGE             ID             DISK USAGE   CONTENT SIZE   EXTRA
notes-api:0.4.0   23990cbdfb04       16.4MB          4.51MB

Challenge

tofu console is an interactive playground for HCL expressions. Use it to print notes in uppercase and the length of the list ["db", "api", "proxy"].

Hint: The functions are called upper and length. The console also reads from stdin.

Solution

cd /tmp
echo 'upper("notes")' | tofu console
echo 'length(["db", "api", "proxy"])' | tofu console

Expected:

"NOTES"
3

The console knows all built-in functions. In a project folder it also sees variables and resources – you’ll use that on Day 2.

Save your progress? Sign up for free

Enterprise Note

  • Pin versions: In a team, the same OpenTofu version runs everywhere. Tools like tenv or a container image for CI take care of that.
  • One lab, many targets: Here everything points at one VM. In practice the same configuration talks to cloud APIs, VMware, Proxmox, or a Kubernetes cluster – the way of working stays the same.
  • Check the license: If you offer Terraform inside your own product, read the BSL. For your own infrastructure, both are free to use.