Meta-Arguments & Resource Lifecycle Control
Master Terraform meta-arguments: count, for_each, depends_on, provider alias, and lifecycle blocks (create_before_destroy, prevent_destroy, ignore_changes).
Meta-Arguments & Resource Lifecycle Control
Section titled “Meta-Arguments & Resource Lifecycle Control”Meta-arguments change how resources are evaluated, provisioned, and destroyed by Terraform’s Directed Acyclic Graph (DAG) engine.
1. count vs. for_each: The Definitive Guide
Section titled “1. count vs. for_each: The Definitive Guide”The Fatal Flaw of count
Section titled “The Fatal Flaw of count”Using count indexes resources by array integers ([0], [1], [2]). If an item in the middle of the list is deleted, all subsequent resources shift index, forcing Terraform to destroy and recreate live infrastructure!
# ❌ DANGEROUS: If "marketing" is removed, "finance" becomes index 1 and gets recreated!variable "departments" { default = ["engineering", "marketing", "finance"]}
resource "aws_iam_user" "dept" { count = length(var.departments) name = var.departments[count.index]}The Production Standard: for_each
Section titled “The Production Standard: for_each”for_each binds each resource to a persistent string key, guaranteeing that adding or deleting keys never affects adjacent resources:
# ✅ PRODUCTION SAFE: Each user is identified by unique key ("engineering", "marketing", etc.)resource "aws_iam_user" "dept" { for_each = toset(["engineering", "marketing", "finance"]) name = each.key}2. Explicit Dependencies: depends_on
Section titled “2. Explicit Dependencies: depends_on”Terraform automatically builds resource dependency trees from referenced attributes (e.g. subnet_id = aws_subnet.pub.id). However, when dependencies are indirect (such as an IAM role needing time to propagate before an EKS cluster or Lambda function uses it), use depends_on:
resource "aws_eks_cluster" "main" { name = "enterprise-eks-cluster" role_arn = aws_iam_role.cluster.arn
# Explicitly wait until AWS IAM policy attachment completes depends_on = [ aws_iam_role_policy_attachment.cluster_AmazonEKSClusterPolicy, aws_iam_role_policy_attachment.cluster_AmazonEKSVPCResourceController ]}3. The lifecycle Block: Controlling Provisioning Behavior
Section titled “3. The lifecycle Block: Controlling Provisioning Behavior”The lifecycle meta-argument allows fine-grained control over how changes are applied:
resource "aws_instance" "app_node" { ami = var.ami_id instance_type = "t3.large"
lifecycle { # 1. Zero-Downtime Rollout: Creates new node before terminating old one create_before_destroy = true
# 2. Safety Net: Prevents accidental deletion of mission-critical assets prevent_destroy = false
# 3. Drift Suppression: Ignores changes made by external autoscalers or security scanners ignore_changes = [ tags["LastSecurityAudit"], user_data ] }}4. Multi-Region & Multi-Account with provider Alias
Section titled “4. Multi-Region & Multi-Account with provider Alias”When provisioning cross-region resources (e.g., CloudFront ACM certificates in us-east-1 while hosting infrastructure in eu-west-1):
provider "aws" { alias = "us_east_1" region = "us-east-1"}
resource "aws_acm_certificate" "cdn_cert" { provider = aws.us_east_1 # Directs certificate creation to us-east-1 domain_name = "devsecopsguru.in" validation_method = "DNS"}