0
0
Terraformcloud~10 mins

Immutable infrastructure concept in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Immutable infrastructure concept
Write new config
Create new infrastructure
Switch traffic to new infra
Destroy old infrastructure
End
Immutable infrastructure means creating new resources for changes instead of modifying existing ones, then switching traffic and removing old resources.
Execution Sample
Terraform
resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
  lifecycle {
    create_before_destroy = true
  }
}

# Change instance_type to t2.small
This Terraform code creates an AWS instance. Changing instance_type triggers creation of a new instance instead of modifying the old one.
Process Table
StepActionTerraform Plan ResultInfrastructure State
1Initial applyCreate aws_instance.web with t2.microOne instance running t2.micro
2Change instance_type to t2.smallPlan to create new aws_instance.web and destroy old oneOld instance running t2.micro, new instance pending
3Apply changesNew instance created, traffic switchedOne instance running t2.small, old instance terminated
4Final stateNo changesOne instance running t2.small
💡 No further changes, infrastructure matches desired state
Status Tracker
VariableStartAfter ChangeAfter ApplyFinal
instance_typet2.microt2.smallt2.smallt2.small
instance_idi-abc123i-abc123 (old), i-def456 (new)i-def456i-def456
Key Moments - 2 Insights
Why does Terraform create a new instance instead of updating the existing one?
Terraform treats changes to instance_type as requiring replacement, so it plans to create a new instance and destroy the old one (see execution_table step 2).
What happens to the old instance during the update?
The old instance stays running until the new one is created and traffic is switched, then Terraform destroys the old instance (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what does Terraform plan to do?
AUpdate the existing instance in place
BDo nothing
CCreate a new instance and destroy the old one
DDestroy the instance without replacement
💡 Hint
See execution_table row 2 under 'Terraform Plan Result'
According to variable_tracker, what is the instance_type after applying changes?
At2.micro
Bt2.small
Ct2.medium
Dunchanged
💡 Hint
Check variable_tracker row for instance_type after apply
When does the old instance get terminated?
AAfter the new instance is created and traffic switched
BAt the same time as creating the new instance
CBefore creating the new instance
DIt never gets terminated
💡 Hint
See execution_table step 3 for infrastructure state changes
Concept Snapshot
Immutable infrastructure means no in-place changes.
Change triggers new resource creation.
Switch traffic to new resource.
Destroy old resource after switch.
Ensures stable, repeatable deployments.
Full Transcript
Immutable infrastructure is a way to manage cloud resources where you never change existing resources directly. Instead, when you want to update something, you create a new resource with the new settings. Then you switch your traffic or usage to the new resource. Finally, you remove the old resource. This approach avoids unexpected changes and makes deployments safer and easier to track. For example, in Terraform, changing the instance type of a server causes Terraform to plan creating a new server and destroying the old one. The old server stays running until the new one is ready, then traffic switches, and the old server is removed. This process ensures your infrastructure is always in a known good state.