Skip to main content
Welcome to this lesson on OpenTofu state management. In this guide, we’ll cover where the state file lives, how OpenTofu tracks resources, default filenames, inspecting state, and working with both local and AWS resources.

Default State File Location

After you provision resources with OpenTofu, the state file is created in your current working directory by default.
The image shows a KodeKloud OpenTofu Lab interface with a task question on the left and a Visual Studio Code editor on the right, displaying a welcome message and terminal.

Disabling State

OpenTofu always relies on a state file to track existing resources. There is no option to disable this behavior.
OpenTofu always maintains a local state file (terraform.tfstate). You cannot disable state management.
The image shows a Visual Studio Code editor with a welcome message for KodeKloud OpenTofu Lab on the right, and a task question about disabling state on the left.

State File Format

Open the terraform.tfstate file and you’ll see it’s formatted in JSON.
The JSON format makes it easy to parse the state file with tools like jq or programmatic scripts.
The image shows a Visual Studio Code editor with a welcome message for the KodeKloud OpenTofu Lab on the right, and a multiple-choice question about file formats on the left.

Commands and State Refresh

Some OpenTofu commands automatically refresh the state to match real-world resources:
The image shows a Visual Studio Code editor with a welcome message for KodeKloud OpenTofu Lab on the right, and a multiple-choice question about command usage on the left.

Default State File Name

By default, the state file created is:
The image shows a Visual Studio Code interface with a welcome message for KodeKloud OpenTofu Lab on the right and a multiple-choice question about a Terraform state file on the left.

Working with State in a Configuration

Navigate into your configuration directory:
You’ve already run opentofu init, so the .terraform folder exists—but no terraform.tfstate file yet, since opentofu apply hasn’t been executed.

1. Showing the State

No output appears because no state file exists yet.

2. Applying the Configuration

Run:
When prompted, type yes. Sample output:

3. Inspecting the New State

Now re-run:
Search for the resource local_file.speed_force. In HCL form, it appears as:
Here, the suffix fab2981 in the filename reflects the resource ID.
Add an EC2 instance and an S3 bucket to your configuration:
After running opentofu apply, inspect the JSON state directly or with:
A snippet for the EC2 instance in terraform.tfstate:
Search for "private_ip" to locate the instance’s private IP:

References

Watch Video

Practice Lab