Understanding OpenTofu config files
Published on 04 Sep 2025 by Adam Lloyd-Jones
OpenTofu, like Terraform, utilizes a structured set of files to define, manage, and deploy infrastructure as code. These files are essential for organizing configurations, managing state, and providing documentation.
Here are the main files and their purposes within an OpenTofu project:
-
.tf
Files (Terraform Configuration Files) OpenTofu processes files ending with the.tf
extension in the current directory. A consistent naming convention is recommended for clarity among team members.main.tf
: This file typically serves as the primary entry point for a module or project. It defines the core infrastructure resources and logic.variables.tf
: This file declares input variables, which allow users to customize the behavior of a module or project without altering its core code. Variables often include descriptions for better understanding.outputs.tf
: This file defines output variables that expose specific values from the deployed infrastructure, such as IP addresses or DNS names. These outputs can be used by other modules or for external consumption.providers.tf
: This file specifies the required cloud providers (e.g., AWS, Azure, Google Cloud) and their versions that the OpenTofu configuration will interact with.- Other
.tf
files: For larger projects, the core functionality can be broken down into multiple.tf
files (e.g.,iam.tf
,s3.tf
,database.tf
) to improve organization and readability, rather than having an excessively largemain.tf
.
-
.tofu
Files (OpenTofu-specific configuration files) Introduced in OpenTofu v1.8,.tofu
files allow module developers to write code compatible with both OpenTofu and Terraform while enabling OpenTofu to leverage new features.- Terraform ignores
.tofu
files. - OpenTofu processes both
.tf
and.tofu
files. - If a
.tofu
file has the same name as a.tf
file, OpenTofu will prioritize and use the.tofu
file, ignoring the.tf
file. This feature is useful for maintaining compatibility and testing OpenTofu-specific functionalities.
- Terraform ignores
-
.opentofu-version
File This configuration file is used by thetenv
tool (a version manager for OpenTofu and Terraform) to specify which version of OpenTofu a project should use. The file can contain an exact version number, a version constraint, or keywords likelatest
orlatest-stable
. -
State Files (
terraform.tfstate
) By default, OpenTofu, like Terraform, stores the state of the managed infrastructure in a local file namedterraform.tfstate
. This file is a critical component as it records which real-world resources correspond to the configuration, their attributes, and other metadata.- It’s essential for tracking changes, planning updates, and identifying discrepancies between the desired and actual infrastructure state.
- State files can contain sensitive information, so caution is advised when inspecting them directly. While typically stored locally for development, shared storage backends are used for team environments.
-
README.md
AREADME.md
file is crucial for documenting the project or module. It explains what the module does, its purpose, how to use it, and how to modify it. This documentation is often pulled into module registries, making it easier for others to understand and reuse. Writing theREADME.md
first can aid in the initial design and planning of a module. -
Other Important Directories and Files:
.terraform/
: This hidden directory is automatically created by OpenTofu (or Terraform) whenterraform init
is run. It stores provider plugins, downloaded modules, and other cached data necessary for operations.modules/
: A common directory for organizing reusable submodules within a larger project, with each submodule having its own.tf
files (e.g.,main.tf
,variables.tf
,outputs.tf
).templates/
: This directory is used to store template files, such as shell scripts (user-data.sh
) that can be dynamically rendered and executed during provisioning.examples/
: Contains example configurations that demonstrate how to use a module or project in different scenarios. These are valuable for documentation and for showcasing functionality..tflint.hcl
: This file contains the configuration for TFLint, a linter used to enforce code quality and stylistic guidelines for OpenTofu (and Terraform) code..pre-commit-config.yml
: Used to configurepre-commit
Git hooks, which automate code quality checks (like formatting and linting) before commits are finalized.
In essence, OpenTofu’s file system is designed to promote modularity, reusability, and clear documentation, enhancing the development and deployment of infrastructure as code.
Related Posts
- The Diverging Paths of Infrastructure as Code: How OpenTofu Handles State Management Differently from Terraform
- The Essential Guide to Docker for Packaging and Deploying Microservices
- Making infrastructure as code (IaC) better: A modular and scalable approach
- What is OpenTofu? Terraform’s open-source alternative
- What are the different files used by Terraform?
- Why developers are moving away from Terraform—and what they're choosing instead
- How Infrastructure as Code delivers unprecedented time savings
- ClickOps vs. IaC: Why Terraform wins in the modern cloud era
- What is Terraform?