DevOpsLesson
DevOpsLesson

Free, comprehensive DevOps tutorials and learning roadmaps. Master Docker, Kubernetes, CI/CD, and more.

Stay Updated

Get notified about new tutorials and features.

Tutorials

  • What is DevOps?
  • Docker Tutorial
  • Terraform Tutorial
  • CI/CD Pipeline
  • All Tutorials

Roadmaps

  • DevOps Engineer
  • Cloud Engineer
  • SRE Path
  • All Roadmaps

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 DevOpsLesson. All rights reserved.

DOCKERKUBERNETESTERRAFORMAWSCI/CDLINUXGITDEVOPS ROADMAPCLOUD ROADMAPSRE ROADMAPGIT CHEATSHEETDOCKER CHEATSHEETK8S CHEATSHEETTF CHEATSHEETLINUX CHEATSHEETDOCKERFILE LINTERYAML VALIDATORCRON PARSERREGEX TESTER

Terraform Tutorial

Introduction to Terraform
Installing Terraform
The Terraform Core Workflow
Terraform Variables and Outputs
Terraform State and Backends
Terraform Modules
Terraform Data Sources
Terraform Count and for_each
Terraform Expressions and Functions
Terraform Dynamic Blocks
Terraform Workspaces
Terraform with AWS
Terraform Provisioners
Terraform CI/CD Pipeline
Terraform Security

Terraform Count and for_each

PreviousPrev
Next

Get a quick overview of Terraform count and for_each so you can choose the right repetition pattern before learning each meta-argument in detail.

As Terraform configurations grow, you quickly reach a common problem: you need to create multiple similar resources without copy-pasting the same block again and again. Terraform solves that with two important meta-arguments: count and for_each.

Both let you declare one resource block and expand it into multiple instances, but they are best at different kinds of repetition.

Meta-argumentUse case
countSimple numeric repetition
for_eachMap/set-based iteration with named instances

Use count when your main question is, "How many copies do I need?" It works well for patterns like creating three identical EC2 instances or optionally creating one resource when a boolean is true.

Use for_each when your instances have stable identities, such as environment names, bucket names, or per-team settings. Because each instance is keyed by name rather than list position, for_each is usually safer when the collection may change over time.

A practical rule is:

  • choose count for simple, index-based repetition
  • choose for_each for named items and long-lived infrastructure

Another way to think about the difference is that count answers a quantity question, while for_each answers an identity question. If your design starts with "I need three of these," count is often the cleanest tool. If your design starts with "I need one for dev, one for staging, and one for prod," for_each usually maps better to the real infrastructure.

In the next lessons, you will learn both patterns in depth, including syntax, references, tradeoffs, and common pitfalls.

  • Learn Terraform count
  • Learn Terraform for_each
PreviousPrev
Next

Continue Learning

Using Terraform Modules

Learn how to use Terraform modules from local paths and the Terraform Registry, pass inputs, pin versions, run terraform init, and consume module outputs.

12 min read·Medium

Writing Terraform Modules

Learn how to write reusable Terraform modules, structure them with main.tf, variables.tf, and outputs.tf, and publish or share modules across projects.

14 min read·Medium

Terraform Data Sources

Learn how Terraform data sources read existing infrastructure, how data blocks work, how outputs are referenced, and when to use data sources instead of resources.

12 min read·Easy

Explore Related Topics

AW

AWS Tutorials

Provision AWS infrastructure with Terraform

CI

CI/CD Tutorials

Run terraform plan/apply in CI pipelines

Try the Tool

YAML Validator

Validate Terraform and Kubernetes YAML configs before applying.

On This Page