Skip to content

002 · Domain 3: Infrastructure Lifecycle and Operations · 22% of the exam

Task 3.5: Manage infrastructure lifecycle

Changing what Terraform manages without surprises: -replace, removed blocks with and without destroy, moved blocks, lifecycle rules, and destroying a single object safely.

Study it

  • Lifecycle changes: replace, removed, moved and destroy

    Lesson coming

  • Recovering from a bad change: state versions and backups

    Partly covered by: Disaster Recovery: RPO & RTO

Sample questions

Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.

Question 1 · choose 1

A queue is no longer needed and must be deleted from the cloud. Nothing else may change, and the queue must not come back on later applies. Which procedure meets both conditions?

  1. ADelete the resource block and every reference to it, using terraform validate to find them, then apply
  2. BRun terraform destroy -target=aws_sqs_queue.jobs once and leave the configuration files exactly as they are
  3. CReplace the resource block with a removed block that sets destroy = false, then apply
  4. DRun terraform state rm aws_sqs_queue.jobs, then delete the resource block
Show the answer and why
  • ADelete the resource block and every reference to it, using terraform validate to find them, then apply

    Correct

    Removing the block makes Terraform destroy the object on apply, and validate reports leftover references to the deleted resource.

  • BRun terraform destroy -target=aws_sqs_queue.jobs once and leave the configuration files exactly as they are

    Incorrect

    destroy does not change the configuration, so the next apply plans to create the queue again.

  • CReplace the resource block with a removed block that sets destroy = false, then apply

    Incorrect

    destroy = false only forgets the object; the queue keeps running in the cloud.

  • DRun terraform state rm aws_sqs_queue.jobs, then delete the resource block

    Incorrect

    state rm forgets the queue without destroying it, so it stays in the cloud, unmanaged.

To destroy one object permanently, take it out of the configuration and let the next plan and apply destroy it. Commands that act only on state or only for one run leave either the object or the configuration behind.

Question 2 · choose 2

aws_s3_bucket.logs uses for_each over a map with the keys "app", "audit" and "legacy". Another team takes over the "legacy" bucket; Terraform must stop managing it without destroying it and keep managing the other two. Which two statements are correct? (Choose TWO.)

  1. AA removed block for aws_s3_bucket.logs with destroy = false forgets only the keys that left the map
  2. BDropping "legacy" from the map is enough; Terraform keeps a bucket whose key disappears
  3. CA removed block cannot name aws_s3_bucket.logs["legacy"]; removed does not accept instance keys
  4. DA moved block from aws_s3_bucket.logs["legacy"] to a data source hands it over without destroying it
  5. Eterraform state rm 'aws_s3_bucket.logs["legacy"]' forgets that one bucket, after which its key can be dropped
Show the answer and why
  • AA removed block for aws_s3_bucket.logs with destroy = false forgets only the keys that left the map

    Incorrect

    A removed block applies to the resource as a whole and replaces its resource block, so it would stop managing all three buckets.

  • BDropping "legacy" from the map is enough; Terraform keeps a bucket whose key disappears

    Incorrect

    An instance whose key disappears from for_each is planned for destruction.

  • CA removed block cannot name aws_s3_bucket.logs["legacy"]; removed does not accept instance keys

    Correct

    The from address of a removed block cannot include an instance key for a resource that has several instances.

  • DA moved block from aws_s3_bucket.logs["legacy"] to a data source hands it over without destroying it

    Incorrect

    moved cannot turn a managed resource into a data resource.

  • Eterraform state rm 'aws_s3_bucket.logs["legacy"]' forgets that one bucket, after which its key can be dropped

    Correct

    state rm accepts a single instance address, removes only that binding, and leaves the remote bucket in place.

removed blocks work on whole resources or modules, so a single instance is handled with terraform state rm before its key is removed from the map, ideally after a -dry-run to confirm the address.

Question 3 · choose 1

Resource A sets create_before_destroy = true and depends on resource B, which does not set it. An engineer adds create_before_destroy = false to B. What happens?

  1. AB is now destroyed before its replacement is created
  2. BA loses its create_before_destroy behavior for every replacement
  3. CB prevents A from ever being replaced
  4. DIt cannot override the setting Terraform applies to B
Show the answer and why
  • AB is now destroyed before its replacement is created

    Incorrect

    The behavior propagates from A, and B cannot opt out.

  • BA loses its create_before_destroy behavior for every replacement

    Incorrect

    The setting on A is unchanged.

  • CB prevents A from ever being replaced

    Incorrect

    That describes prevent_destroy, not create_before_destroy.

  • DIt cannot override the setting Terraform applies to B

    Correct

    Terraform propagates create_before_destroy to dependencies and stores it; setting false on B would imply dependency cycles.

Plan replacements across a dependency chain as a whole, since the ordering of one resource affects its dependencies.

Question 4 · choose 1

A module renamed aws_instance.a to aws_instance.b in v2 and now renames it to aws_instance.c in v3. Some callers still run v1. How should v3 record the moves?

  1. ATwo chained moved blocks: a to b, and b to c
  2. BOne moved block from b to c, replacing the old one
  3. CA removed block for a, and a new resource c
  4. Dterraform state mv in each caller's pipeline
Show the answer and why
  • ATwo chained moved blocks: a to b, and b to c

    Correct

    HashiCorp recommends chaining moved blocks when the same object moves twice, documenting the full history for callers on any version.

  • BOne moved block from b to c, replacing the old one

    Incorrect

    Callers still on v1 would plan to destroy aws_instance.a.

  • CA removed block for a, and a new resource c

    Incorrect

    That destroys or forgets the object instead of moving it.

  • Dterraform state mv in each caller's pipeline

    Incorrect

    moved blocks let callers upgrade without manual state commands.

Keep historical moved blocks in published modules so that every caller can upgrade from any earlier version.

Question 5 · choose 1

A removed block for aws_db_instance.legacy sets lifecycle { destroy = false prevent_destroy = true }. What does Terraform do?

  1. AForgets the database and blocks any later destroy
  2. BDestroys the database, as prevent_destroy wins
  3. CRejects it; destroy is the only lifecycle argument allowed
  4. DIgnores the lifecycle block and keeps managing the database
Show the answer and why
  • AForgets the database and blocks any later destroy

    Incorrect

    prevent_destroy is not accepted in a removed block.

  • BDestroys the database, as prevent_destroy wins

    Incorrect

    The configuration is invalid rather than resolved in favor of either argument.

  • CRejects it; destroy is the only lifecycle argument allowed

    Correct

    In removed blocks, the only supported lifecycle argument is destroy.

  • DIgnores the lifecycle block and keeps managing the database

    Incorrect

    A removed block always stops management of the resource.

Use destroy = false to forget an object without destroying it, and nothing else in that lifecycle block.

Question 6 · choose 1

A team wants to replace aws_instance.web with a resource of a different type, keeping the same remote object, by writing a moved block between the two addresses. What does the refactoring documentation say?

  1. Amoved never works between types, as each type has its own schema
  2. BIt works only if the provider supports that type change
  3. CIt always works, and Terraform converts the attributes
  4. DIt works only for resources in the root module
Show the answer and why
  • Amoved never works between types, as each type has its own schema

    Incorrect

    Some providers do support moving objects between resource types.

  • BIt works only if the provider supports that type change

    Correct

    moved can always rename a resource, but changing the type depends on provider support.

  • CIt always works, and Terraform converts the attributes

    Incorrect

    Types have separate schemas and are not typically compatible.

  • DIt works only for resources in the root module

    Incorrect

    moved blocks also work inside and across modules.

Check the provider documentation before planning a cross-type move.

Practice domain 3 →Practice all domains →