0
0
Terraformcloud~30 mins

Creation-time vs destruction-time in Terraform - Hands-On Comparison

Choose your learning style9 modes available
Creation-time vs destruction-time in Terraform
📖 Scenario: You are managing cloud resources using Terraform. You want to understand how to set values that are fixed when resources are created (creation-time) versus values that can be changed or removed when resources are destroyed (destruction-time).
🎯 Goal: Build a Terraform configuration that defines a resource with a creation-time attribute and a destruction-time attribute, showing how to set and update these values properly.
📋 What You'll Learn
Create a Terraform resource with a fixed creation-time attribute
Add a variable to configure a destruction-time attribute
Use Terraform expressions to assign these attributes correctly
Complete the resource configuration with lifecycle rules
💡 Why This Matters
🌍 Real World
Managing cloud infrastructure with Terraform requires understanding which resource attributes can be changed without recreating resources and which cannot. This helps avoid downtime and data loss.
💼 Career
Cloud engineers and DevOps professionals must write Terraform code that safely manages resource lifecycle, balancing fixed creation-time settings with flexible destruction-time updates.
Progress0 / 4 steps
1
Define a Terraform resource with a creation-time attribute
Create a Terraform resource called aws_s3_bucket named example_bucket with the attribute bucket set to the fixed string my-fixed-bucket-name.
Terraform
Need a hint?

The bucket name is fixed at creation time and cannot be changed later.

2
Add a variable for destruction-time attribute
Add a Terraform variable called enable_versioning of type bool with a default value false to control versioning on the bucket.
Terraform
Need a hint?

Variables allow you to configure attributes that can be changed without recreating the resource.

3
Use the variable to configure destruction-time attribute
Update the aws_s3_bucket.example_bucket resource to add a versioning block. Set enabled to the variable enable_versioning.
Terraform
Need a hint?

The versioning block can be changed after creation, so it is a destruction-time attribute.

4
Add lifecycle rule to prevent bucket recreation
Add a lifecycle block inside aws_s3_bucket.example_bucket with prevent_destroy = true to avoid accidental bucket deletion.
Terraform
Need a hint?

This lifecycle rule protects the bucket from being destroyed accidentally.