OpenTofu Commands Guide

OpenTofu Commands Guide


Introduction

OpenTofu offers a versatile command-line interface for managing your infrastructure. With a diverse range of commands, you can initiate, validate, and manipulate configurations with ease.

Command Basics:

Run the tofu command by itself to get a list of available commands and their purposes. The general structure is:

tofu [global options] <subcommand> [arguments]

Central Commands:

  • init: Initializes your workspace for OpenTofu tasks.
  • validate: Reviews your configuration for any issues.
  • plan: Lists out the changes your current configuration will enact.
  • apply: Constructs or amends the infrastructure.
  • destroy: Deletes the established infrastructure.

Secondary Commands:

  • console: Test OpenTofu expressions in a real-time prompt.
  • fmt: Adjust your configuration to match the standard format.
  • get: Fetch or renew remote OpenTofu modules.

... (and many others. Check docs for more)

While it's standard to navigate to the directory containing your .tf files, OpenTofu provides flexibility with the -chdir option. This lets you specify another directory for OpenTofu to act within:

tofu -chdir=path/to/directory subcommand

Do note, certain references, like settings in the CLI Configuration and path.cwd in your configuration, still point to the original directory.

Utilizing Global Options:

These options are placed before any subcommands:

  • -chdir=DIR: Shifts to a designated directory before running the subsequent command.
  • -help: Pulls up the general or command-specific help guide.
  • -version: A quick way to check your OpenTofu version.

Gaining More Insight:

For a deep dive into a specific command, add -help to the end. For instance:

tofu validate -help

This will give you a detailed breakdown of the validate command. However, for exhaustive details on each command, the official documentation is the go-to.

Shell Integration:

If you're using bash or zsh, OpenTofu provides tab-completion, making command entry more efficient.

To activate this feature, input:

tofu -install-autocomplete

Remember to restart or reload your shell after installation.

To deactivate:

tofu -uninstall-autocomplete

Dive into OpenTofu with this guide as your starting point, and always turn to the official resources for comprehensive instruction. Feel free to go through the OpenTofu docs for more information.