Chapter 36: Terraform count for_each dynamic lifecycle and depends_on
Chala mitrano, aaj meta-arguments. count ne copies, for_each ne naav asleli copies, dynamic ne repeat honare nested block, lifecycle ne badal kasa vhava te, aani depends_on ne lapleli dependency. He samajla ki tumcha code chota aani surakshit hoto.
What you will learn in this chapter
- count for N copies and on or off switches
- for_each for stable named copies
- dynamic blocks for repeated nested rules
- lifecycle: create_before_destroy, prevent_destroy, ignore_changes, preconditions
- depends_on for hidden dependencies
Concepts in this chapter
- 36.1count — N copies of the same thing
- 36.2for_each — named copies that survive edits
- 36.3dynamic blocks — repeat a nested block from data
- 36.4lifecycle — control how changes happen
- 36.5depends_on — when Terraform cannot see the link
The chapter recap is at the end of the last concept page.
Practice Tasks
Suppose we are on the DevOps team at Siemens, the industrial engineering company, working from Munich. Lukas Becker leads the team, and we look after the machine sensor dashboard. Costs are counted in EUR. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Siemens is a real company, but this project, the people, servers, IP addresses and numbers are made up for practice.
0 / 12
Easy S3 bucket resource
Why: Buckets in code are reviewed and never forgotten.
What to do: Write a resource aws_s3_bucket named logs with bucket = "siemens-logs" and tags { Team = "devops" }.
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Easy Input variable
Why: Variables make one module usable for many environments.
What to do: Write a variable "instance_type" with type = string and default = "t3.micro".
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Easy Output a value
Why: Outputs hand values to scripts and other modules.
What to do: Write output "web_ip" with value = aws_instance.web.public_ip.
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Easy Provider block
Why: The provider tells Terraform which cloud and region to talk to.
What to do: Write a provider "aws" block with region = "eu-central-1".
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Easy Name the practice
Why: IaC is the base of repeatable cloud setups.
What to do: Siemens describes its servers, networks and databases in text files kept in Git and creates them with a tool. What is this called (three-letter short form)?
- Write down the facts or numbers the task gives you.
- Work it out step by step.
- Type only the final answer (one word or number) in the box.
- Click Check answer.
Medium EC2 instance using a variable
Why: Referencing var.* avoids hard-coding sizes.
What to do: Write resource aws_instance "web" with ami = "ami-0abcdef1234567890", instance_type = var.instance_type and tags Name = "machine-sensor".
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Medium Plan with a variable value
Why: The same code builds dev and prod with different values.
What to do: Run a plan with the variable region set to eu-central-1 and using the file prod.tfvars.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Install several packages with a loop
Why: A loop keeps one task for many items.
What to do: Playbook for hosts all, become true. One apt task with name "{{ item }}" and state present, with loop over git, curl and htop.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Hard Create several copies with count
Why: count builds N identical resources from one block.
What to do: Write resource aws_instance "worker" with count = 4, ami = "ami-0abcdef1234567890", instance_type = "t3.small" and tags Name = "worker-${count.index}".
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Hard Security group for HTTPS
Why: Opening only 443 keeps the attack surface small.
What to do: Write resource aws_security_group "web" with name = "web-sg" and one ingress block: from_port = 443, to_port = 443, protocol = "tcp", cidr_blocks = ["0.0.0.0/0"].
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Hard Remote state in S3
Why: Shared remote state with locking stops two people breaking each other.
What to do: Write a terraform block with backend "s3": bucket = "siemens-tfstate", key = "prod/terraform.tfstate", region = "eu-central-1", dynamodb_table = "tf-lock".
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Hard Call a module
Why: Modules package a tested pattern, like a VPC, for reuse.
What to do: Write module "vpc" with source = "terraform-aws-modules/vpc/aws", version = "5.8.1", name = "siemens-vpc" and cidr = "10.0.0.0/16".
- Write the block type (resource, variable, output, module...) and its labels in quotes.
- Open { and write each argument as name = value.
- Put text values in double quotes; numbers and true/false without quotes.
- Close every block with } and click Check answer.
Ravi Sir's Cards: 13 revision, interview and comparison cards for this chapter · मराठी cards