Ravindra BagaleCourses & study guides मराठी

Ravi Sir's Cards › DevOps

Chapter 36: Terraform count for_each dynamic lifecycle and depends_on: Cards

मराठी cards · Open the chapter

Revision cards

count: N copies of the same thing. Writing the same block 3 times is copying. count makes N copies from one block.
Revision card 1 of 5: count: N copies of the same thing · Download PNG · Open card page

Lesson: 36.1 count — N copies of the same thing

for_each: named copies that survive edits. With count, removing an item from the middle of a list shifts every index, and Terraform may recreate resources. for_each uses names instead.
Revision card 2 of 5: for_each: named copies that survive edits · Download PNG · Open card page

Lesson: 36.2 for_each — named copies that survive edits

dynamic blocks: repeat a nested block from data. Some resources have nested blocks, like ingress rules. Writing them by hand for each port is repetitive.
Revision card 3 of 5: dynamic blocks: repeat a nested block from data · Download PNG · Open card page

Lesson: 36.3 dynamic blocks — repeat a nested block from data

lifecycle: control how changes happen. Some changes force Terraform to destroy and recreate a resource. For a database or a live server that can mean an outage or lost data.
Revision card 4 of 5: lifecycle: control how changes happen · Download PNG · Open card page

Lesson: 36.4 lifecycle — control how changes happen

depends_on: when Terraform cannot see the link. Terraform orders work by references. If there is a hidden link, it may create things in the wrong order.
Revision card 5 of 5: depends_on: when Terraform cannot see the link · Download PNG · Open card page

Lesson: 36.5 depends_on — when Terraform cannot see the link

Interview cards

When do you use count? count creates a number of identical copies of a resource, addressed as name[0], name[1]. It is also used as an on/off switch with count = var.enabled ? 1 : 0.
Interview card 1 of 5: When do you use count? · Download PNG · Open card page

Lesson: 36.1 count — N copies of the same thing

Why is for_each often safer than count? With count, resources are tracked by position. Remove the 2nd item and the 3rd moves to index 1, so Terraform changes or recreates it. With for_each, each resource is tracked by a key, so only the removed one is destroyed.
Interview card 2 of 5: Why is for_each often safer than count? · Download PNG · Open card page

Lesson: 36.2 for_each — named copies that survive edits

What is a dynamic block, and when should you avoid it? A dynamic block generates repeated nested blocks (like ingress inside a security group) from a list or map. It does not create resources, only nested blocks inside one resource.
Interview card 3 of 5: What is a dynamic block, and when should you avoid it? · Download PNG · Open card page

Lesson: 36.3 dynamic blocks — repeat a nested block from data

How do you stop Terraform from deleting a production database? Add lifecycle { prevent_destroy = true } to the resource. Any plan that would destroy it fails with an error. Also turn on the service’s own protection, like RDS deletion_protection.
Interview card 4 of 5: How do you stop Terraform from deleting a production database? · Download PNG · Open card page

Lesson: 36.4 lifecycle — control how changes happen

When do you need depends_on in Terraform? Terraform orders resources by references. depends_on is needed only for a hidden dependency: one resource needs another to exist, but does not use any of its values.
Interview card 5 of 5: When do you need depends_on in Terraform? · Download PNG · Open card page

Lesson: 36.5 depends_on — when Terraform cannot see the link

Comparison cards

count vs for_each. Both make many copies from one resource block.count: copies by number, address name[0].for_each: copies by key, address name["payments"].
Comparison card 1 of 3: count vs for_each · Download PNG · Open card page

Lesson: Chapter 36: Terraform count for_each dynamic lifecycle and depends_on

create_before_destroy vs prevent_destroy. Both go in a lifecycle block.create_before_destroy: when a resource must be replaced, build the new one first.prevent_destroy: refuse any plan that would delete it.
Comparison card 2 of 3: create_before_destroy vs prevent_destroy · Download PNG · Open card page

Lesson: Chapter 36: Terraform count for_each dynamic lifecycle and depends_on

Implicit reference vs depends_on. Reference: one resource uses another’s value (aws_vpc.main.id), so Terraform orders them.depends_on: you list a dependency by hand.
Comparison card 3 of 3: Implicit reference vs depends_on · Download PNG · Open card page

Lesson: Chapter 36: Terraform count for_each dynamic lifecycle and depends_on