Originally published byDev.to
π Project Structure
terraform-dependency-lab/
β
βββ main.tf
βββ variables.tf
βββ terraform.tfvars
βββ outputs.tf
βββ providers.tf
πΉ 1. providers.tf
terraform {
required_version = ">= 1.5.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.aws_region
}
πΉ 2. variables.tf (NO HARDCODING)
variable "aws_region" {
description = "AWS region"
type = string
}
variable "project_name" {
description = "Project name"
type = string
}
variable "instance_type" {
description = "EC2 instance type"
type = string
}
variable "common_tags" {
description = "Common tags"
type = map(string)
}
πΉ 3. terraform.tfvars
aws_region = "us-east-2"
project_name = "dep-lab"
instance_type = "t2.micro"
common_tags = {
Owner = "Student"
Lab = "Dependencies"
}
πΉ 4. main.tf
πΈ Part 1: Implicit Dependency
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = merge(var.common_tags, {
Name = "${var.project_name}-vpc"
})
}
resource "aws_subnet" "subnet" {
vpc_id = aws_vpc.main.id # β
IMPLICIT DEPENDENCY
cidr_block = "10.0.1.0/24"
tags = merge(var.common_tags, {
Name = "${var.project_name}-subnet"
})
}
π Explanation:
-
aws_subnetdepends onaws_vpcautomatically - No
depends_onneeded
πΈ Part 2: Explicit Dependency (Real Scenario)
resource "aws_security_group" "sg" {
name = "${var.project_name}-sg"
vpc_id = aws_vpc.main.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = var.common_tags
}
πΈ EC2 Instance
resource "aws_instance" "ec2" {
ami = data.aws_ami.amazon_linux.id
instance_type = var.instance_type
subnet_id = aws_subnet.subnet.id
vpc_security_group_ids = [aws_security_group.sg.id]
tags = merge(var.common_tags, {
Name = "${var.project_name}-ec2"
})
}
πΈ Data Source (Dynamic AMI)
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["al2023-ami-*-x86_64"]
}
}
πΉ 5. Explicit Dependency Example (FORCE ORDER)
β οΈ Simulate hidden dependency
resource "null_resource" "setup" {
provisioner "local-exec" {
command = "echo EC2 should be ready"
}
depends_on = [aws_instance.ec2] # β
EXPLICIT DEPENDENCY
}
πΉ 6. outputs.tf
output "vpc_id" {
value = aws_vpc.main.id
}
output "subnet_id" {
value = aws_subnet.subnet.id
}
output "ec2_id" {
value = aws_instance.ec2.id
}
πΉ π How to Run (Step-by-Step)
cd terraform-dependency-lab
terraform init
terraform plan
terraform apply
β Implicit Dependency
- VPC β Subnet β EC2 created in order
- No
depends_onused
β Parallel Execution
- Security group may create in parallel with subnet
β Explicit Dependency
-
null_resourceruns only after EC2
Show graph:
terraform graph | dot -Tpng > graph.png
Explain:
- Arrows = dependencies
- Graph = Terraform brain
πΉ π‘ Interview-Level Takeaways
- Terraform uses implicit dependencies via references
- Builds dependency graph (DAG)
- Executes parallel when possible
- Uses
depends_onwhen dependency is hidden
πΊπΈ
More news from United StatesUnited States
NORTH AMERICA
Related News

Qodo AI Review 2026: Is It the Best AI Testing Tool?
1d ago
How do you actually manage your content's SEO performance?
1d ago

I built a local-first Obsidian suite to safely feed my vault to AI π οΈπ
1d ago
America's CIA Recruited Iran's Nuclear Scientists - By Threatening To Kill Them
1d ago
Why NodeDB Might Be a Better Multi-Model Database
1d ago