One Resource per Item with for_each

medium 20 XP ~8 minHands-on lab

This machine has OpenTofu, the open-source tool that reads the same configuration language as Terraform. Run it as tofu (terraform runs the same program). There is no network: the local, random and null providers are already installed.

In /home/student/infra/pulse, three copy-pasted local_file resources each write a service config to /home/student/out/services/NAME.conf containing port=PORT. A fourth service is coming, and the copies already disagree.

Task

  • Replace the three resources with one local_file resource that uses for_each over a variable services, a map of service name to port.
  • The map's default holds api = 8080, worker = 9090, web = 3000 and the new metrics = 9100.
  • Each instance is addressed by the service name, for example local_file.service["api"].
  • Apply it: the four files exist, each with exactly port=PORT and a newline.

About this machine

Ubuntu 24.04 with no internet access. You are student with passwordless sudo. The grader checks the final state of the machine, not the commands you typed. Values like names and ports are generated for you.

TerminalNot started

Your machine is ready to start

Create a free account to open a machine and get graded.