Live
โ€ขBlack Hat USADark Readingโ€ขBlack Hat AsiaAI Businessโ€ขBuilding a Zero-Downtime AI Content Generator with Gemini 2.5 Flash ๐Ÿš€Dev.to AIโ€ขHow I Built a Full SaaS Product Using Next.js and TypeScriptDev.to AIโ€ขYour AI Is Not Thinking. It's Multiplying Numbers. Let Me Show You Exactly How.Dev.to AIโ€ขSecure AWS Certified Data Engineer Associate Exam Structure and Key ConceptsDev.to AIโ€ขFree MCP Server: Real-Time Crypto Data for Claude Code and CursorDev.to AIโ€ขI Am an AI Agent. Here Is My Entire Business Stack.Dev.to AIโ€ขA Reasoning Log: What Happens When Integration Fails HonestlyDEV Communityโ€ขI Scanned 50 Open-Source MCP Servers. Here Is What I Found.DEV Communityโ€ขLG holds AI hackathon to cultivate next generation of tech talent - The Korea TimesGoogle News: LLMโ€ขHow to Create Your Own AI Coding AgentDEV Communityโ€ขPractical Implementation of Power BI Report Embedding in Modern Website(Step-by-Step Guide)DEV Communityโ€ขArtificial Intelligence Versus Human Stupidity - CounterPunch.orgGoogle News: AIโ€ขBlack Hat USADark Readingโ€ขBlack Hat AsiaAI Businessโ€ขBuilding a Zero-Downtime AI Content Generator with Gemini 2.5 Flash ๐Ÿš€Dev.to AIโ€ขHow I Built a Full SaaS Product Using Next.js and TypeScriptDev.to AIโ€ขYour AI Is Not Thinking. It's Multiplying Numbers. Let Me Show You Exactly How.Dev.to AIโ€ขSecure AWS Certified Data Engineer Associate Exam Structure and Key ConceptsDev.to AIโ€ขFree MCP Server: Real-Time Crypto Data for Claude Code and CursorDev.to AIโ€ขI Am an AI Agent. Here Is My Entire Business Stack.Dev.to AIโ€ขA Reasoning Log: What Happens When Integration Fails HonestlyDEV Communityโ€ขI Scanned 50 Open-Source MCP Servers. Here Is What I Found.DEV Communityโ€ขLG holds AI hackathon to cultivate next generation of tech talent - The Korea TimesGoogle News: LLMโ€ขHow to Create Your Own AI Coding AgentDEV Communityโ€ขPractical Implementation of Power BI Report Embedding in Modern Website(Step-by-Step Guide)DEV Communityโ€ขArtificial Intelligence Versus Human Stupidity - CounterPunch.orgGoogle News: AI
AI NEWS HUBbyEIGENVECTOREigenvector

LAB: Terraform Dependencies (Implicit vs Explicit)

DEV Communityby Aisalkyn AidarovaApril 3, 20263 min read1 views
Source Quiz

๐Ÿ“ 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" }

๐Ÿ“ Project Structure

terraform-dependency-lab/ โ”‚ โ”œโ”€โ”€ main.tf โ”œโ”€โ”€ variables.tf โ”œโ”€โ”€ terraform.tfvars โ”œโ”€โ”€ outputs.tf โ””โ”€โ”€ providers.tf

Enter fullscreen mode

Exit fullscreen mode

๐Ÿ”น 1. providers.tf

terraform {  required_version = ">= 1.5.0"

required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } }

provider "aws" { region = var.aws_region }`

Enter fullscreen mode

Exit fullscreen mode

๐Ÿ”น 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) }`

Enter fullscreen mode

Exit fullscreen mode

๐Ÿ”น 3. terraform.tfvars

aws_region = "us-east-2" project_name = "dep-lab" instance_type = "t2.micro"

common_tags = { Owner = "Student" Lab = "Dependencies" }`

Enter fullscreen mode

Exit fullscreen mode

๐Ÿ”น 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" }) }`

Enter fullscreen mode

Exit fullscreen mode

๐Ÿ‘‰ Explanation:

  • aws_subnet depends on aws_vpc automatically

  • No depends_on needed

๐Ÿ”ธ 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 }`

Enter fullscreen mode

Exit fullscreen mode

๐Ÿ”ธ 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" }) }`

Enter fullscreen mode

Exit fullscreen mode

๐Ÿ”ธ Data Source (Dynamic AMI)

data "aws_ami" "amazon_linux" {  most_recent = true

owners = ["amazon"]

filter { name = "name" values = ["al2023-ami--x86_64"] } }`

Enter fullscreen mode

Exit fullscreen mode

๐Ÿ”น 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 }`

Enter fullscreen mode

Exit fullscreen mode

๐Ÿ”น 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 }`

Enter fullscreen mode

Exit fullscreen mode

๐Ÿ”น ๐Ÿš€ How to Run (Step-by-Step)

cd terraform-dependency-lab

terraform init terraform plan terraform apply`

Enter fullscreen mode

Exit fullscreen mode

โœ… Implicit Dependency

  • VPC โ†’ Subnet โ†’ EC2 created in order

  • No depends_on used

โœ… Parallel Execution

  • Security group may create in parallel with subnet

โœ… Explicit Dependency

  • null_resource runs only after EC2

Show graph:

terraform graph | dot -Tpng > graph.png

Enter fullscreen mode

Exit fullscreen mode

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_on when dependency is hidden

Was this article helpful?

Sign in to highlight and annotate this article

AI
Ask AI about this article
Powered by Eigenvector ยท full article context loaded
Ready

Conversation starters

Ask anything about this articleโ€ฆ

Daily AI Digest

Get the top 5 AI stories delivered to your inbox every morning.

More about

version

Knowledge Map

Knowledge Map
TopicsEntitiesSource
LAB: Terrafโ€ฆversionDEV Communiโ€ฆ

Connected Articles โ€” Knowledge Graph

This article is connected to other articles through shared AI topics and tags.

Knowledge Graph100 articles ยท 207 connections
Scroll to zoom ยท drag to pan ยท click to open

Discussion

Sign in to join the discussion

No comments yet โ€” be the first to share your thoughts!

More in Releases