0
0
Terraformcloud~10 mins

Terraform file organization - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Terraform file organization
Start: Create main.tf
Define provider and resources
Create variables.tf
Declare input variables
Create outputs.tf
Declare outputs
Optional: Create terraform.tfvars
Assign variable values
Run terraform init & apply
Infrastructure deployed
End
Terraform files are organized by purpose: main.tf for resources, variables.tf for inputs, outputs.tf for outputs, and optional terraform.tfvars for variable values. This flow shows how these files work together to deploy infrastructure.
Execution Sample
Terraform
provider "aws" {
  region = var.region
}

resource "aws_s3_bucket" "bucket" {
  bucket = var.bucket_name
}
Defines AWS provider and an S3 bucket resource using variables for region and bucket name.
Process Table
StepFile ProcessedActionEffectNext Step
1main.tfRead provider and resource blocksProvider and resource defined with variable referencesProcess variables.tf
2variables.tfRead variable declarationsVariables region and bucket_name declaredProcess outputs.tf
3outputs.tfRead output declarationsOutputs defined for resource attributesCheck terraform.tfvars (optional)
4terraform.tfvarsRead variable valuesVariables assigned actual valuesRun terraform init
5terraform initInitialize working directoryPlugins downloaded, backend configuredRun terraform apply
6terraform applyApply configurationInfrastructure created with given variablesEnd
7-EndInfrastructure deployed successfully-
💡 All files processed and terraform commands run to deploy infrastructure.
Status Tracker
VariableDeclaredAssigned in terraform.tfvarsUsed in main.tfFinal Value
regionDeclared as string"us-east-1"Used in provider blockus-east-1
bucket_nameDeclared as string"my-unique-bucket"Used in resource bucket namemy-unique-bucket
Key Moments - 3 Insights
Why do we separate variables into variables.tf instead of putting them directly in main.tf?
Separating variables in variables.tf keeps main.tf focused on resources and makes it easier to manage inputs separately, as shown in execution_table steps 1 and 2.
What happens if terraform.tfvars is missing?
Terraform uses default variable values or prompts for input. Without terraform.tfvars (step 4), variables may be undefined causing errors or manual input.
Why do we have outputs.tf as a separate file?
Outputs.tf organizes outputs separately for clarity and reuse, as seen in step 3, making it easier to find what information Terraform will show after apply.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step are variable values assigned?
AStep 4
BStep 2
CStep 5
DStep 6
💡 Hint
Check the 'Action' column for 'Read variable values' in execution_table.
According to variable_tracker, what is the final value of 'bucket_name'?
Aus-east-1
BUndefined
Cmy-unique-bucket
Dbucket_name
💡 Hint
Look at the 'Final Value' column for 'bucket_name' in variable_tracker.
If we remove outputs.tf, what impact is shown in the execution flow?
ATerraform apply will fail at step 6
BOutputs will not be declared or shown after apply
CVariables will not be assigned
DProvider will not initialize
💡 Hint
Refer to execution_table step 3 about outputs.tf role.
Concept Snapshot
Terraform files are organized by role:
- main.tf: resources and provider
- variables.tf: input variable declarations
- outputs.tf: output declarations
- terraform.tfvars: variable values
This separation keeps configs clean and manageable.
Full Transcript
Terraform file organization involves splitting configuration into files by their purpose. The main.tf file contains provider and resource definitions, referencing variables declared in variables.tf. Outputs.tf declares outputs to show after deployment. Optionally, terraform.tfvars assigns values to variables. The process starts by reading main.tf, then variables.tf, outputs.tf, and terraform.tfvars if present. Terraform commands init and apply then deploy the infrastructure. Variables track from declaration to assignment and usage. This organization helps keep Terraform projects clear and easy to manage.