Tools: LAB: Terraform Dependencies (Implicit vs Explicit) - Expert Insights

Tools: LAB: Terraform Dependencies (Implicit vs Explicit) - Expert Insights

📁 Project Structure

🔹 1. providers.tf

🔹 2. variables.tf (NO HARDCODING)

🔹 3. terraform.tfvars

🔹 4. main.tf

🔸 Part 1: Implicit Dependency

👉 Explanation:

🔸 Part 2: Explicit Dependency (Real Scenario)

🔸 EC2 Instance

🔹 5. Explicit Dependency Example (FORCE ORDER)

⚠️ Simulate hidden dependency

🔹 6. outputs.tf

🔹 🚀 How to Run (Step-by-Step)

✅ Implicit Dependency

✅ Parallel Execution

✅ Explicit Dependency

Show graph:

🔹 💡 Interview-Level Takeaways Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Code Block

Copy

terraform-dependency-lab/ │ ├── main.tf ├── variables.tf ├── terraform.tfvars ├── outputs.tf └── providers.tf terraform-dependency-lab/ │ ├── main.tf ├── variables.tf ├── terraform.tfvars ├── outputs.tf └── providers.tf terraform-dependency-lab/ │ ├── main.tf ├── variables.tf ├── terraform.tfvars ├── outputs.tf └── providers.tf terraform { required_version = ">= 1.5.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = var.aws_region } terraform { required_version = ">= 1.5.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = var.aws_region } terraform { required_version = ">= 1.5.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = var.aws_region } 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) } 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) } 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) } aws_region = "us-east-2" project_name = "dep-lab" instance_type = "t2.micro" common_tags = { Owner = "Student" Lab = "Dependencies" } aws_region = "us-east-2" project_name = "dep-lab" instance_type = "t2.micro" common_tags = { Owner = "Student" Lab = "Dependencies" } aws_region = "us-east-2" project_name = "dep-lab" instance_type = "t2.micro" common_tags = { Owner = "Student" Lab = "Dependencies" } 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" }) } 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" }) } 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" }) } 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 } 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 } 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 } 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" }) } 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" }) } 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 "aws_ami" "amazon_linux" { most_recent = true owners = ["amazon"] filter { name = "name" values = ["al2023-ami-*-x86_64"] } } data "aws_ami" "amazon_linux" { most_recent = true owners = ["amazon"] filter { name = "name" values = ["al2023-ami-*-x86_64"] } } data "aws_ami" "amazon_linux" { most_recent = true owners = ["amazon"] filter { name = "name" values = ["al2023-ami-*-x86_64"] } } resource "null_resource" "setup" { provisioner "local-exec" { command = "echo EC2 should be ready" } depends_on = [aws_instance.ec2] # ✅ EXPLICIT DEPENDENCY } resource "null_resource" "setup" { provisioner "local-exec" { command = "echo EC2 should be ready" } depends_on = [aws_instance.ec2] # ✅ EXPLICIT DEPENDENCY } resource "null_resource" "setup" { provisioner "local-exec" { command = "echo EC2 should be ready" } depends_on = [aws_instance.ec2] # ✅ EXPLICIT DEPENDENCY } output "vpc_id" { value = aws_vpc.main.id } output "subnet_id" { value = aws_subnet.subnet.id } output "ec2_id" { value = aws_instance.ec2.id } output "vpc_id" { value = aws_vpc.main.id } output "subnet_id" { value = aws_subnet.subnet.id } output "ec2_id" { value = aws_instance.ec2.id } output "vpc_id" { value = aws_vpc.main.id } output "subnet_id" { value = aws_subnet.subnet.id } output "ec2_id" { value = aws_instance.ec2.id } cd terraform-dependency-lab terraform init terraform plan terraform apply cd terraform-dependency-lab terraform init terraform plan terraform apply cd terraform-dependency-lab terraform init terraform plan terraform apply terraform graph | dot -Tpng > graph.png terraform graph | dot -Tpng > graph.png terraform graph | dot -Tpng > graph.png - aws_subnet depends on aws_vpc automatically - No depends_on needed - VPC → Subnet → EC2 created in order - No depends_on used - Security group may create in parallel with subnet - null_resource runs only after EC2 - Arrows = dependencies - Graph = Terraform brain - Terraform uses implicit dependencies via references - Builds dependency graph (DAG) - Executes parallel when possible - Uses depends_on when dependency is hidden