A Terraform project can fit in a single main.tf file. As soon as it holds
more than a few resources (the objects Terraform creates, such as a VM or a
disk), it is better to split it into files by theme: you find what you are
looking for faster. This page presents the structure recommended by the
official HashiCorp style guide, what each file contains, and how to apply it
from your first project.
What you will learn
- The official convention:
terraform.tf,providers.tf,main.tf,variables.tf,outputs.tf, plusterraform.tfvars - The role of each file: what goes where and why
- The
.terraform.lock.hclfile: what it does and why you commit it - The recommended
.gitignore: what must stay out of Git
The recommended minimal structure
Répertoiremy-project/
- terraform.tf (version constraints for Terraform and the providers)
- providers.tf (provider configuration)
- main.tf (resources and data sources)
- variables.tf (variable declarations)
- outputs.tf (values exposed after apply)
- terraform.tfvars (variable values, do not commit if sensitive)
Terraform loads all the .tf files of the current directory, in any
order: it resolves the dependencies between blocks by itself. These names are
therefore a convention. The only exception is override.tf and the
*_override.tf files, which Terraform loads last so that they override the
blocks of the other files. The convention presented here is the one published
by HashiCorp: its style guide (in French)
calls the version constraints file terraform.tf. The name
versions.tf, very common in community modules, is not the one this page
uses.
The role of each file
terraform.tf
This file declares the constraints on Terraform itself and the required
providers, in a single terraform block. It is the first thing to create
in any new project: without it, terraform init does not know where to
download a provider such as libvirt, which is not published by HashiCorp.
terraform { required_version = ">= 1.11.0" required_providers { libvirt = { source = "dmacvicar/libvirt" version = "~> 0.9.0" } }}providers.tf
This file configures the providers declared in terraform.tf: here, the
address of the libvirt daemon. The separation matters as soon as a project uses
modules (reusable pieces of configuration): a module declares the providers
it needs, but their configuration stays in the root module, that is, the
directory where you run terraform. Terraform then passes this configuration
down to the modules.
provider "libvirt" { uri = "qemu:///system"}main.tf
The heart of the project: the resources (objects that Terraform creates and
manages) and the data sources (existing objects that Terraform only reads,
see providers, resources and data sources).
On a small project, everything fits here. On a larger project, you create
files by theme (network.tf, compute.tf, storage.tf).
The example below declares a disk (libvirt_volume) and then the VM
(libvirt_domain) that uses it. The var.… references point to the variables
declared in variables.tf, right after.
resource "libvirt_volume" "disk" { name = "${var.vm_name}.qcow2" pool = "default" target = { format = { type = "qcow2" } } create = { content = { url = pathexpand(var.image_path) } }}
resource "libvirt_domain" "vm" { name = var.vm_name type = "kvm" memory = var.memory_mb memory_unit = "MiB" vcpu = var.vcpu_count running = true # without it, the VM is created but stays stopped
os = { type = "hvm" type_arch = "x86_64" type_machine = "q35" }
devices = { disks = [ { source = { file = { file = libvirt_volume.disk.path } } target = { dev = "vda", bus = "virtio" } driver = { type = "qcow2" } # without it, libvirt reads the disk as raw } ] interfaces = [ { model = { type = "virtio" } source = { network = { network = "default" } } } ] }}variables.tf
All the variable {} declarations are grouped here. This file documents the
project's input parameters.
variable "vm_name" { description = "Name of the libvirt VM" type = string default = "lab-vm"}
variable "memory_mb" { description = "RAM allocated in MiB" type = number default = 512}
variable "vcpu_count" { description = "Number of vCPUs" type = number default = 1}
variable "image_path" { description = "Path to the local cloud image (~ is expanded by pathexpand)" type = string default = "~/images/ubuntu-24.04-cloudimg.img"}outputs.tf
The values that Terraform prints at the end of a terraform apply and that you
can read again later: IP, name, ID, path…
output "vm_name" { description = "Name of the VM created" value = libvirt_domain.vm.name}
output "disk_path" { description = "Path of the disk in the libvirt pool" value = libvirt_volume.disk.path}After apply, with the default values of the variables (without
terraform.tfvars, presented right after), you read these outputs again with
terraform output (tested on Debian 13, Terraform 1.17.2, libvirt provider
0.9.9):
terraform output vm_name # → "lab-vm"terraform output disk_path # → "/var/lib/libvirt/images/lab-vm.qcow2"The name comes from the default of vm_name, and the disk is stored in the
libvirt default pool, hence the path under /var/lib/libvirt/images/.
terraform.tfvars
Concrete values for the variables. This file overrides the default values
declared in variables.tf.
vm_name = "my-web-server"memory_mb = 1024vcpu_count = 2This convenience has a downside on a shared repository: a file that is
loaded automatically lives at the root of the project, and goes into Git on the
first git add ., secrets included.
The structure of the tested project
Once terraform init and terraform apply have run, the directory contains
three more items, which Terraform generates itself:
Répertoiremy-project/
- terraform.tf
- providers.tf
- main.tf
- variables.tf
- outputs.tf
- terraform.tfvars
Répertoire.terraform/ (generated by terraform init, do not commit)
- …
- .terraform.lock.hcl (provider lock file, commit it)
- terraform.tfstate (local state, do not commit)
The .terraform.lock.hcl file
Generated automatically by terraform init, this file records the exact
versions of the installed providers and their hash (a fingerprint used to
verify the download).
provider "registry.terraform.io/dmacvicar/libvirt" { version = "0.9.9" constraints = "~> 0.9.0" hashes = [ "h1:...", ]}The constraints line copies what you wrote in terraform.tf, the
version line what Terraform actually installed: here 0.9.9, which
satisfies ~> 0.9.0.
This file must be committed to the Git repository: at the next
terraform init, every team member (and the CI) then gets exactly the same
provider versions.
The recommended .gitignore
Three items stay out of Git: the cache .terraform/, which
terraform init can rebuild, the state (the file where Terraform records
what it created, with values in clear text) and the values files, which may
hold secrets.
# Terraform.terraform/terraform.tfstateterraform.tfstate.backup*.tfvarstfplan*.tfplanterraform.tfstate.backup is the copy of the state before the last change:
Terraform creates it on its own, and it contains the same clear-text values as
the state. tfplan and *.tfplan cover the plans saved with
terraform plan -out=tfplan: a saved plan contains the configuration values in
clear text, passwords included, and must not go into Git.
*.tfvars is excluded to avoid pushing secrets. If your .tfvars files hold
no sensitive data, remove this line, or version an example file
(terraform.tfvars.example) without the real values.
.terraform.lock.hcl is not in the .gitignore, it must be versioned.
Structure for a larger project
As a project grows, you can split it by resource type:
Répertoireinfra-kvm/
- terraform.tf
- providers.tf (provider configurations, including aliases)
- network.tf (libvirt_network)
- compute.tf (libvirt_domain + libvirt_volume)
- variables.tf
- outputs.tf
- locals.tf (computed values, locals block)
- terraform.tfvars
An alias is a second configuration of the same provider, for example to talk to a second libvirt daemon.
There is no absolute rule on file names; what matters is that the structure is predictable and documented for the whole team. This page stops at the organization of a project's files; splitting into several directories and environments is covered in the guide organize a Terraform repository (in French).
Put it into practice
File names have no functional effect (apart from override files): Terraform reads all the .tf files as a single document. You split a monolith into six files by theme, and the validation compares two plans in JSON format to establish that nothing changed. You then observe which source of values wins over the others.
Key points
terraform.tf: version constraints for Terraform and the providers, the name the official style guide uses.providers.tf: provider configuration.main.tf: resources, data sources.variables.tf: declarations of the input parameters.outputs.tf: values exposed after apply.terraform.tfvars(and*.auto.tfvars): variable values (no secrets)..terraform.lock.hcl: always commit it..terraform/andterraform.tfstate: keep them out of Git.