0
0
Terraformcloud~5 mins

Root module concept in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform uses modules to organize infrastructure code. The root module is the main folder where you run Terraform commands. It controls what resources get created and how.
When you start a new Terraform project and want to define your infrastructure in one place.
When you want to organize your cloud resources like servers, networks, and storage together.
When you want to apply changes to your infrastructure by running Terraform commands in a single directory.
When you want to call other modules from your main configuration to keep code clean.
When you want to manage your infrastructure lifecycle with Terraform safely and predictably.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example_bucket" {
  bucket = "example-root-module-bucket-12345"
  acl    = "private"
}

terraform block sets the Terraform version requirement.

provider "aws" configures AWS as the cloud provider and sets the region.

resource "aws_s3_bucket" "example_bucket" defines an S3 bucket resource named example_bucket.

This file is the root module because it is the main Terraform configuration where commands are run.

Commands
Initializes the Terraform working directory. It downloads provider plugins and prepares the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. If you ever set or change modules or backend configuration, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
Shows what Terraform will do before applying changes. It helps you verify the planned infrastructure changes.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_s3_bucket.example_bucket will be created + resource "aws_s3_bucket" "example_bucket" { + acl = "private" + bucket = "example-root-module-bucket-12345" + force_destroy = false + id = (known after apply) + region = (known after apply) + tags = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. ─────────────────────────────────────────────────────────────────────────────
Applies the planned changes to create or update infrastructure without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Creating... aws_s3_bucket.example_bucket: Creation complete after 2s [id=example-root-module-bucket-12345] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips interactive approval before applying changes
Displays the current state of the infrastructure managed by Terraform in a human-readable format.
Terminal
terraform show
Expected OutputExpected
aws_s3_bucket.example_bucket: id = example-root-module-bucket-12345 arn = arn:aws:s3:::example-root-module-bucket-12345 bucket = example-root-module-bucket-12345 acl = private
Key Concept

If you remember nothing else from this pattern, remember: the root module is the main Terraform folder where you define and run your infrastructure code.

Common Mistakes
Running Terraform commands in a different folder than where main.tf is located
Terraform won't find the root module configuration and will fail or create empty state.
Always run Terraform commands in the directory containing your root module files like main.tf.
Not running 'terraform init' before other commands
Terraform needs to download providers and initialize the environment; without it, commands fail.
Run 'terraform init' once before planning or applying changes.
Summary
Create a main.tf file as the root module to define your infrastructure resources.
Run 'terraform init' to prepare the working directory and download providers.
Use 'terraform plan' to preview changes and 'terraform apply' to create resources.
The root module is the main entry point for Terraform commands and controls your infrastructure.