OpenTofu Practice
Exercises
Exercise 1: Local Resources (No Cloud Required)
- Create an OpenTofu configuration that generates multiple local files
- Use
count or for_each to create 3 files with different content
- Use variables and outputs
- Run
tofu plan, tofu apply, verify the files, then tofu destroy
Goal: Get comfortable with the core OpenTofu workflow
- Take an existing Terraform configuration (or use one from
topics/terraform/basics/)
- Follow the migration guide: https://opentofu.org/docs/intro/migration/
- Run
tofu init -upgrade to migrate
- Verify state is intact with
tofu state list
- Run
tofu plan and confirm no changes are detected
Goal: Understand OpenTofuโs Terraform compatibility
Exercise 3: State Encryption (OpenTofu-Exclusive)
- Create a simple configuration with a local resource
- Add an
encryption block using the pbkdf2 key provider
- Apply the configuration โ observe the state file is now encrypted
- Verify you cannot read the state file directly with
cat
- Run
tofu state list to confirm Tofu can still read it
Reference: https://opentofu.org/docs/language/state/encryption/
Exercise 4: Writing and Running Tests
- Create a module that manages a local directory structure
- Write a
.tftest.hcl test file with at least 2 test cases
- Run
tofu test and observe results
- Intentionally break a test assertion to see the failure output
Reference: https://opentofu.org/docs/language/tests/
Exercise 5: Provider Configuration with Multiple Environments
- Create a configuration with
dev and prod workspaces
- Use
terraform.workspace to set different values per environment
- Use
tofu workspace new dev and tofu workspace new prod
- Apply to each workspace and compare state files
Reference: https://opentofu.org/docs/language/state/workspaces/