Skip to content

Project Structure

When working with Terraform, it is important to understand how to structure your project for scalability, maintainability, and re-usability.

With the use of HCL configuration files, Terraform allows for easy management of complex infrastructures by breaking them down into smaller, more manageable modules.

The root module is the top-level module that contains all the other modules, resources, and data sources defined in a Terraform configuration. When Terraform is run, it loads the root module first and then processes all the child modules and resources within it.

The Terraform project structure consists of a set of directories and files that organise your infrastructure code, which we call the root module.

The root module is located in the directory where the Terraform configuration is stored, and it is identified by the presence of a main.tf file. This file contains the main Terraform code that defines resources and modules for the infrastructure being managed.

::alert[ `main.tf` is the community adopted standard, your configuration does not have to be in a file called `main.tf` and can be spread multiple `.tf` files.]

The root module can also include other supporting files such as variables, output definitions, provider configurations, and other resources that may be shared across multiple modules.

Here’s a breakdown of the key files and directories that you might see in a Terraform project:

  • main.tf This file defines the resources you want to create and configure.
  • variables.tf This file defines the input variables that can be passed to your resources.
  • outputs.tf: This file defines the output values that are returned by your resources.
  • terraform.tfvars: This file defines the variable values that should be used when running your Terraform code. It’s not recommended to store secrets in this file.
  • provider.tf This file defines the provider you want to use for your resources. For example, the AWS provider.
  • modules This directory contains reusable modules that you can use across different projects.

Example Terraform Project Structure [Example Terraform Project Structure]

In order to start creating your first terraform project, create the files: main.tf, variables.tf, version.tf.

Terminal window
touch main.tf variables.tf version.tf

In the version.tf file add details for which Terraform and AWS provider version you will be using.

version.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5"
}
}
required_version = ">= 1.4.0"
}

In the variables.tf file, add your first variable for naming your app (which we will use later).

variables.tf
variable "app_name" {
description = "Name of my app"
default = "Terraform App"
type = string
}

In the main.tf file, create a reference for the AWS Provider - giving it a region to deploy AWS Resources to.

main.tf
provider "aws" {
region = "us-east-1"
}

Remember this? A provider is an abstraction of the API/service provider such as GCP or Fastly. Providers typically require some sort of configuration data such as an API key or credential file.

If you are using a named AWS profile that isn’t the default one - you will want to tell Terraform which profile to use.

main.tf
provider "aws" {
region = "us-east-1"
profile = "name-of-profile"
}

Understanding the Terraform project structure and module system is essential for creating scalable, maintainable, and reusable infrastructure code. By organising your code into modules and following the file naming conventions, you can create powerful infrastructure-as-code solutions that are easy to manage and update.