Terraform
1️⃣ Variables
Purpose:
Inputs to your Terraform configuration.
👉 Used by providers, resources, and modules
👉 Loaded first
2️⃣ Providers
Purpose:
Tell Terraform which cloud / service API to talk to and how to authenticate.
👉 Depend on variables
👉 Initialize during terraform init
3️⃣ Resources
Purpose:
Actual infrastructure objects (EC2, S3, Kafka topic, DB, etc.).
👉 Depend on providers
👉 Can depend on variables and other resources
4️⃣ Data Sources
Purpose:
Read-only existing infrastructure.
👉 Do not create anything
👉 Used by resources and modules
5️⃣ Modules
Purpose:
Reusable logical groups of Terraform code (like functions).
👉 Can contain:
-
variables
-
providers
-
resources
-
data sources
-
outputs
👉 Modules depend on inputs passed to them
Relationship (Big Picture)
Or more practically:
-
Variables feed everything
-
Providers enable API communication
-
Data sources read existing state
-
Resources create/update infrastructure
-
Modules organize and reuse all of the above
Execution Flow (What Terraform Does Internally)
1️⃣ terraform init
-
Downloads providers
-
Initializes backend
-
Loads modules
2️⃣ terraform plan
Terraform builds a dependency graph:
-
Loads variables
-
Configures providers
-
Reads data sources
-
Calculates resource dependencies
-
Shows what will change
👉 Execution order is determined by dependencies, not file order
3️⃣ terraform apply
Terraform executes in this order automatically:
-
Providers are initialized
-
Data sources are read
-
Resources are created/updated/destroyed
-
Module outputs are calculated
-
State file is updated
Example Execution Order
Execution:
-
Read variable
vpc_id -
Read existing VPC (data source)
-
Create subnet (resource)
Key Rules to Remember
✅ Terraform does NOT execute top to bottom
✅ It executes based on dependency graph
✅ Use references (resource.id) to control order
✅ Use depends_on only when absolutely necessary
One-Line Summary
Variables provide input → Providers enable APIs → Data sources read → Resources create → Modules organize everything
Comments
Post a Comment