
OpenTofu (OTF) is an infrastructure as code (IaC) tool that lets you define and provision infrastructure by writing it as code. Most of your day-to-day work runs through a handful of CLI commands, and you can get a lot done once you know what they do. Here are ten OpenTofu commands worth knowing, with notes on when you reach for each.
tofu init: before you can start working with OTF in a project directory, you need to initialize it using tofu init. This command downloads the necessary providers and modules defined in your configuration files, ensuring that your environment is properly set up.tofu plan: use tofu plan to generate an execution plan. OTF will analyze your configuration files, compare them to your existing infrastructure (if any), and display a summary of what changes will be made when you apply the configuration. This step helps you understand the impact of your changes before actually making them.tofu apply: once you're satisfied with the execution plan, you can apply your changes using tofu apply. This command creates or updates the resources specified in your configuration files. OTF will prompt you to confirm the changes before proceeding.tofu destroy: if you want to remove the infrastructure you've created, use tofu destroy. It will destroy all resources defined in your configuration files, so be cautious when running this command in a production environment.tofu validate: to check the syntax and validity of your OTF configuration files, run tofu validate. This command helps catch any syntax errors or configuration issues before attempting to apply changes.tofu fmt: maintaining consistent code style is essential in collaborative projects. tofu fmt automatically formats your configuration files according to OTF's conventions, making them more readable and consistent.tofu state: managing OTF's state files is crucial for tracking resource state and dependencies. The tofu state command provides various subcommands (e.g., list, show, mv) to interact with and manage the state.tofu import: if you have existing resources that you want to manage with OTF, you can use tofu import to bring them under OTF control. This command helps you define OTF resources based on the existing infrastructure.tofu workspace: allows you to manage multiple environments (e.g., development, staging, production) within the same configuration. tofu workspace lets you create, select, and delete workspaces, making it easier to manage infrastructure for different stages.tofu output: use tofu output to display the values of output variables defined in your OTF configuration. This is especially useful for retrieving information about resources created by OTF, such as IP addresses or DNS names.These ten commands cover most of what you do with OpenTofu day to day. As you go deeper, you'll pick up more commands and options that fit your specific workflow. Be careful with anything that changes production, and keep your OTF configurations under version control and documented.
OpenTofu big shiny tool. Caveman use for make cloud rocks appear without climb mountain every time. Caveman write wish on stone tablet, tool read tablet, tool build. Smart people call this "infrastructure as code". Caveman call it rock magic. Before caveman do rock magic, caveman must learn ten grunt words. Caveman teach now.
tofu init: first grunt. Caveman shout this in new cave before do anything else. Tool go fetch helper spirits (tool call them "providers") and borrowed tools ("modules"). No init, no magic. Cave stay empty, tribe laugh.tofu plan: smart caveman no smash first. Caveman grunt tofu plan, tool show picture of what WILL happen if caveman go through with it. Caveman look at picture, nod or frown. No rock moved yet. Very safe grunt.tofu apply: caveman like picture? Caveman grunt tofu apply. NOW rocks move. Tool ask "caveman sure?" before big change. Caveman say yes, cloud rocks appear.tofu destroy: angry grunt. Make ALL rocks in tablet go away. Very dangerous. Caveman who grunt this near production cave become legend around campfire. Bad legend.tofu validate: caveman check tablet for scribble mistakes before show tribe. Tool say "tablet good" or "tablet bad, look line 12". Save caveman from shame at campfire.tofu fmt: tribe share tablets. Messy tablet make other cavemen angry. This grunt make all tablets look same way. Pretty tablet, happy tribe.tofu state: tool keep memory stone of every rock it made. This grunt have little helper grunts (list, show, mv) so caveman can peek at memory stone, count rocks, rename rocks. Caveman who lose memory stone cry many moons.tofu import: some rocks exist from before caveman had tool. No problem. This grunt tell tool "this rock yours now". Old rock join tribe, get written on memory stone.tofu workspace: caveman have many caves — test cave, almost-real cave, REAL cave. This grunt let caveman use same tablet for all caves without mixing them up. Mixing up caves very bad. Ask caveman who deleted REAL cave.tofu output: after rocks built, caveman want know things about rocks — rock address, rock name. This grunt make tool say numbers it wrote down. No need climb rock and squint.Ten grunts done. Caveman now dangerous with tablet. Caveman practice in test cave first, keep all tablets in version cave (tribe call it git), and never ever grunt destroy near REAL cave. Ugh. Go make rocks.
