> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Introduction

> Guide to Terraform iteration and logic using count, for_each, for expressions and conditional expressions for scalable maintainable resources and predictable state management

In this lesson we'll explore how Terraform handles repetition and decision-making so your infrastructure code can scale without duplication. You'll learn when to use `count` and when to prefer `for_each`, how to reshape data with `for` expressions, and how to apply conditional expressions for environment-specific or optional logic.

* Understand `count` and its limits for predictable resource addresses.
* Master `for_each` for stable identities and safer state management.
* Compare `count` vs `for_each` to choose the right approach for long-term maintenance.
* Use `for` expressions to transform collections.
* Use conditional expressions to implement environment or optional logic.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/9l60TCpe5-axPZIp/images/Terraform-On-Azure/Iteration-and-Logic/Introduction/terraform-resource-management-agenda.jpg?fit=max&auto=format&n=9l60TCpe5-axPZIp&q=85&s=65247a865a7c0d6230ab7b89d1df9e52" alt="The image displays an agenda with five numbered points related to resource management and expressions in Terraform, each point having a different color." width="1920" height="1080" data-path="images/Terraform-On-Azure/Iteration-and-Logic/Introduction/terraform-resource-management-agenda.jpg" />
</Frame>

All of these patterns let your configurations adapt programmatically, reducing repetition and making modules easier to maintain and evolve.

<Callout icon="lightbulb" color="#1CB2FE">
  Use `count` when you need simple repetition of identical resources. Prefer `for_each` for collections where each instance needs a stable address or when keys/identities matter for the Terraform state.
</Callout>

## Quick overview: when to use each pattern

|                 Pattern | Best for                                                            | Example                                                                                    |
| ----------------------: | ------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
|                 `count` | Creating N identical resources with a numeric index                 | `count = 3`                                                                                |
|              `for_each` | Creating resources from a collection with stable keys (`set`/`map`) | `for_each = toset(["a","b"])` or `for_each = { "app1" = "10.0.1.0", "app2" = "10.0.2.0" }` |
|       `for` expressions | Transforming or mapping collections before use                      | `local.subnets = [for s in var.azs : "${s}-subnet"]`                                       |
| Conditional expressions | Environment-specific or optional behavior                           | `count = var.enabled ? 1 : 0`                                                              |

## What you'll learn in this lesson

1. `count` — how it creates multiple instances and where it becomes limiting.
2. `for_each` — why it provides stable resource identities and predictable state addresses.
3. Side-by-side comparison — how changing between patterns affects state and resource replacement.
4. `for` expressions — reshaping data for use in resources.
5. Conditional expressions — gating resources and settings by environment or flags.

<Callout icon="warning" color="#FF6B6B">
  Changing between `count` and `for_each` (or changing keys used by `for_each`) can force Terraform to recreate resources. Test changes in a safe environment and plan before apply to avoid unexpected replacements.
</Callout>

## Links and references

* [Terraform documentation: count](https://www.terraform.io/docs/language/meta-arguments/count.html)
* [Terraform documentation: for\_each](https://www.terraform.io/docs/language/meta-arguments/for_each.html)
* [Terraform documentation: for expressions](https://www.terraform.io/docs/language/expressions/for.html)
* [Terraform documentation: conditional expressions](https://www.terraform.io/docs/language/expressions/conditionals.html)

These topics will help you write Terraform configurations that are both concise and resilient as your infrastructure grows.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/terraform-on-azure/module/fb5019bb-df21-4583-818e-6dae40fde2ec/lesson/ee652b86-0e33-48f1-bea5-f99104adddd3" />
</CardGroup>
