Bird
Raised Fist0
Terraformcloud~10 mins

Creation-time vs destruction-time in Terraform - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Process Flow - Creation-time vs destruction-time
Start Terraform Apply
↓
Creation-time: Create Resources
↓
Resources Active
↓
Start Terraform Destroy
↓
Destruction-time: Delete Resources
↓
Resources Removed
↓
End
Terraform first creates resources during apply, then deletes them during destroy, each with specific timing and actions.
Execution Sample
Terraform
resource "aws_s3_bucket" "example" {
  bucket = "my-example-bucket"
}

output "bucket_name" {
  value = aws_s3_bucket.example.bucket
}
Creates an S3 bucket at creation-time and outputs its name; bucket is deleted at destruction-time.
Process Table
StepActionResource StateTerraform PhaseResult
1terraform apply startsNo resourcesCreation-timePreparing to create resources
2Create aws_s3_bucket.exampleBucket createdCreation-timeBucket 'my-example-bucket' exists
3Output bucket_nameBucket existsCreation-timeOutputs bucket name 'my-example-bucket'
4terraform apply endsResources activeCreation-timeResources ready for use
5terraform destroy startsResources activeDestruction-timePreparing to delete resources
6Delete aws_s3_bucket.exampleBucket deletedDestruction-timeBucket removed from cloud
7terraform destroy endsNo resourcesDestruction-timeAll resources destroyed
💡 terraform destroy completed, all resources removed
Status Tracker
VariableStartAfter CreationAfter Destruction
aws_s3_bucket.examplenullexistsnull
bucket_name outputnull"my-example-bucket"null
Key Moments - 3 Insights
Why can't we access the bucket output after destruction?
Because at destruction-time (see step 7), the bucket resource no longer exists, so outputs referencing it become invalid.
What happens if creation fails at step 2?
Terraform stops and does not proceed to output or resource activation, so no resources are created or output values set.
Is the bucket available immediately after step 2?
Yes, after creation at step 2, the bucket exists and can be used or referenced in outputs as shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the resource state at step 4?
ANo resources
BBucket deleted
CResources active
DPreparing to delete resources
💡 Hint
Check the 'Resource State' column for step 4 in the execution_table.
At which step does Terraform start deleting resources?
AStep 3
BStep 5
CStep 2
DStep 7
💡 Hint
Look for 'terraform destroy starts' in the 'Action' column of the execution_table.
If the bucket creation failed at step 2, what would be the bucket_name output after step 3?
Anull
BError message
C"my-example-bucket"
DEmpty string
💡 Hint
Refer to variable_tracker and key_moments about creation failure effects.
Concept Snapshot
Terraform manages resources in two main phases:
- Creation-time: resources are created and become active.
- Destruction-time: resources are deleted and removed.
Outputs reflect resource state only when resources exist.
Destroy removes all created resources, cleaning the environment.
Full Transcript
Terraform works in two main phases: creation-time and destruction-time. During creation-time, Terraform creates resources like an S3 bucket and outputs their details. After creation, resources are active and usable. During destruction-time, Terraform deletes these resources, removing them from the cloud. Outputs depending on these resources become invalid after destruction. This flow ensures infrastructure is managed cleanly and predictably.

Practice

(1/5)
1. What does creation-time mean in Terraform?
easy
A. The moment when resources are first set up or created
B. The moment when resources are deleted or removed
C. The time when Terraform runs a plan without changes
D. The time when Terraform refreshes the state

Solution

  1. Step 1: Understand creation-time meaning

    Creation-time refers to when Terraform sets up resources for the first time.
  2. Step 2: Differentiate from destruction-time

    Destruction-time is when resources are removed, not created.
  3. Final Answer:

    The moment when resources are first set up or created -> Option A
  4. Quick Check:

    Creation-time = resource setup [OK]
Hint: Creation-time means resource setup, destruction-time means removal [OK]
Common Mistakes:
  • Confusing creation-time with destruction-time
  • Thinking creation-time is when resources are deleted
  • Mixing creation-time with Terraform plan execution
2. Which Terraform lifecycle argument controls actions at destruction-time?
easy
A. create_before_destroy
B. depends_on
C. ignore_changes
D. prevent_destroy

Solution

  1. Step 1: Identify destruction-time control arguments

    The prevent_destroy argument stops Terraform from deleting a resource.
  2. Step 2: Compare other lifecycle arguments

    create_before_destroy affects creation and replacement order, not destruction prevention.
  3. Final Answer:

    prevent_destroy -> Option D
  4. Quick Check:

    Destruction-time control = prevent_destroy [OK]
Hint: Use prevent_destroy to block resource deletion at destruction-time [OK]
Common Mistakes:
  • Choosing create_before_destroy which affects creation order
  • Confusing ignore_changes with destruction control
  • Using depends_on which manages resource dependencies
3. Given this Terraform snippet:
resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  lifecycle {
    create_before_destroy = true
  }
}

What happens when you change instance_type from t2.micro to t2.small?
medium
A. Terraform destroys the old instance first, then creates the new one
B. Terraform updates the instance in place without destruction
C. Terraform creates the new instance first, then destroys the old one
D. Terraform throws an error due to lifecycle conflict

Solution

  1. Step 1: Understand create_before_destroy effect

    This setting makes Terraform create the new resource before destroying the old one.
  2. Step 2: Apply to instance_type change

    Changing instance_type forces replacement, so new instance is created first, then old destroyed.
  3. Final Answer:

    Terraform creates the new instance first, then destroys the old one -> Option C
  4. Quick Check:

    create_before_destroy = new before old [OK]
Hint: create_before_destroy means new resource before old is removed [OK]
Common Mistakes:
  • Assuming in-place update without replacement
  • Thinking old resource is destroyed before new is created
  • Believing lifecycle causes errors on changes
4. You added prevent_destroy = true to a resource but Terraform still deletes it on terraform destroy. What is the likely cause?
medium
A. You ran terraform destroy which ignores prevent_destroy
B. The prevent_destroy argument is misspelled or misplaced
C. The resource is referenced by another resource with depends_on
D. Terraform does not support prevent_destroy for that resource type

Solution

  1. Step 1: Check prevent_destroy usage

    It must be inside the lifecycle block and spelled correctly.
  2. Step 2: Understand terraform destroy behavior

    terraform destroy respects prevent_destroy and will error if deletion is blocked.
  3. Final Answer:

    The prevent_destroy argument is misspelled or misplaced -> Option B
  4. Quick Check:

    Misplaced prevent_destroy causes deletion despite protection [OK]
Hint: Ensure prevent_destroy is inside lifecycle block and spelled right [OK]
Common Mistakes:
  • Assuming terraform destroy ignores prevent_destroy
  • Placing prevent_destroy outside lifecycle block
  • Thinking depends_on affects prevent_destroy behavior
5. You want to replace a resource safely without downtime. Which lifecycle settings should you use to ensure the new resource is created before the old one is destroyed, and prevent accidental deletion?
hard
A. Set create_before_destroy = true and prevent_destroy = true
B. Set create_before_destroy = false and prevent_destroy = true
C. Set create_before_destroy = true and ignore_changes = [all]
D. Set prevent_destroy = false and ignore_changes = [all]

Solution

  1. Step 1: Use create_before_destroy for safe replacement

    This ensures the new resource is ready before the old one is removed, avoiding downtime.
  2. Step 2: Use prevent_destroy to block accidental deletion

    This stops Terraform from deleting the resource unless explicitly allowed, adding safety.
  3. Final Answer:

    Set create_before_destroy = true and prevent_destroy = true -> Option A
  4. Quick Check:

    Safe replacement = create_before_destroy + prevent_destroy [OK]
Hint: Combine create_before_destroy and prevent_destroy for safe updates [OK]
Common Mistakes:
  • Setting prevent_destroy without create_before_destroy causes downtime
  • Using ignore_changes to avoid destruction which doesn't prevent deletion
  • Disabling prevent_destroy risks accidental resource loss
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard