Skip to main content
Welcome to this hands-on lab on OpenTofu commands. Here, you’ll learn how to visualize, validate, plan, and apply your infrastructure-as-code (IaC) configurations using the tofu CLI. By the end of this guide, you’ll be comfortable generating dependency graphs, troubleshooting HCL errors, and managing provider plugins.

1. Visualizing Resources

To inspect resource dependencies, generate a DOT graph:
You can then render graph.dot with Graphviz to visualize your IaC topology.

2. Validating Configuration

Before creating any resources, validate your HCL syntax and catch typos:
  1. Change into your project directory:
  2. Run the validator:
The image shows a Visual Studio Code interface with a task description on the left about fixing configuration errors using the tofu validate command. On the right, there's a terminal and file explorer open, displaying a project directory structure.
If you see:
Always match algorithm-specific arguments. In this case, replace dsa_bits with rsa_bits for an RSA key.
Correct the block in main.tf:
Re-run tofu validate until no errors remain.

3. Planning and Applying

3.1 Generating a Plan

Create an execution plan to preview changes:
You’ll see which resources will be added, changed, or destroyed.

3.2 First Apply Attempt

Apply the plan:
If you encounter:
it means the syntax was valid but some resource arguments are incompatible.

4. Fixing the TLS Resource Block

Ensure your main.tf includes only RSA-compatible settings and the local file resource:
Re-initialize, plan, and apply:
If tofu apply completes without errors, your configuration is now correct.

5. Formatting Code

Keep your files consistent:
This enforces HCL canonical style across all .tf files.

6. Inspecting State

Query the state for a specific resource:
Check the filename attribute (e.g., /tmp/.pki/private_key.pem) to confirm it matches expectations.

7. Providers Subcommands

OpenTofu uses providers to interact with external APIs. To list available provider commands:
Common subcommands include:
  • mirror
  • list
  • install
  • remove

8. Reviewing Downloaded Providers

Without browsing the directory directly, list installed plugins:
The image shows a coding environment with a file explorer and a code editor displaying a JSON file related to Terraform configuration. There is also a terminal at the bottom with commands related to navigating directories and managing provider plugins.
You should see entries like:
  • registry.opentofu.org/hashicorp/aws
  • registry.opentofu.org/hashicorp/local

Watch Video

Practice Lab