Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Immutable Infrastructure Concept with Terraform
📖 Scenario: You are working as a cloud engineer. Your team wants to use immutable infrastructure to deploy a simple web server. This means instead of changing the existing server, you will create a new server each time you deploy. This helps avoid errors and keeps the system stable.
🎯 Goal: Build a Terraform configuration that creates an AWS EC2 instance with immutable infrastructure principles. You will first define the instance data, then add a configuration variable, apply the main resource block, and finally complete the setup with a tag to identify the instance.
📋 What You'll Learn
Use Terraform to define AWS EC2 instance configuration
Create a variable for the AMI ID
Define the EC2 instance resource using the variable
Add a tag to the instance for identification
💡 Why This Matters
🌍 Real World
Immutable infrastructure helps teams deploy cloud servers safely by replacing servers instead of changing them. This reduces errors and downtime.
💼 Career
Cloud engineers and DevOps professionals use Terraform and immutable infrastructure to automate and improve cloud deployments.
Progress0 / 4 steps
1
Define the AWS provider and instance data
Write Terraform code to set the AWS provider with region us-east-1 and create a variable called ami_id with default value "ami-0c55b159cbfafe1f0".
Terraform
Hint
Use provider "aws" block to set the region. Use variable "ami_id" block with default to set the AMI ID.
2
Add instance type configuration
Add a variable called instance_type with default value "t2.micro" to specify the EC2 instance type.
Terraform
Hint
Use a variable block named instance_type with a default value.
3
Create the EC2 instance resource
Write a resource block named aws_instance with name web_server. Use var.ami_id for the AMI and var.instance_type for the instance type.
Terraform
Hint
Use resource "aws_instance" "web_server" block. Set ami and instance_type using variables.
4
Add a Name tag to the EC2 instance
Inside the aws_instance.web_server resource, add a tags block with Name = "ImmutableWebServer" to identify the instance.
Terraform
Hint
Inside the resource block, add tags = { Name = "ImmutableWebServer" } to label the instance.
Practice
(1/5)
1. What does the term immutable infrastructure mean in Terraform?
easy
A. Replacing resources instead of modifying them in place
B. Changing resources directly without replacement
C. Manually updating resources outside Terraform
D. Using mutable variables to configure resources
Solution
Step 1: Understand the definition of immutable infrastructure
Immutable infrastructure means you do not change existing resources but replace them entirely when updates are needed.
Step 2: Compare options with this definition
Only Replacing resources instead of modifying them in place describes replacing resources instead of modifying them, which matches the concept.
Final Answer:
Replacing resources instead of modifying them in place -> Option A
Quick Check:
Immutable infrastructure = Replace, not modify [OK]
Hint: Immutable means replace, not change existing resources [OK]
Common Mistakes:
Thinking mutable means immutable
Confusing manual updates with Terraform-managed changes
Assuming resources are changed in place
2. Which Terraform lifecycle argument helps implement immutable infrastructure by creating new resources before destroying old ones?
easy
A. ignore_changes
B. prevent_destroy
C. create_before_destroy
D. replace_triggered_by
Solution
Step 1: Identify lifecycle arguments related to resource replacement
Terraform lifecycle has arguments like create_before_destroy, prevent_destroy, ignore_changes, and replace_triggered_by.
Step 2: Match argument to immutable infrastructure behavior
Create_before_destroy ensures new resource is created before old one is destroyed, supporting immutable infrastructure.
Final Answer:
create_before_destroy -> Option C
Quick Check:
Lifecycle create_before_destroy = create new before delete old [OK]
Hint: Use create_before_destroy to replace safely [OK]
Common Mistakes:
Confusing prevent_destroy with create_before_destroy
Using ignore_changes which skips updates but doesn't replace
Misunderstanding replace_triggered_by purpose
3. Given this Terraform resource snippet with lifecycle:
5. You want to deploy a web server with immutable infrastructure using Terraform. Which combination of lifecycle settings and resource management best supports this goal?
hard
A. Use ignore_changes on all attributes to prevent updates
B. Use create_before_destroy = true and avoid manual changes outside Terraform
C. Use prevent_destroy = true to stop any resource replacement
D. Manually update resources and disable Terraform lifecycle rules
Solution
Step 1: Identify lifecycle settings that enable immutable infrastructure
Create_before_destroy = true ensures new resource is ready before old is removed, supporting immutable infrastructure.
Step 2: Consider resource management best practices
Avoid manual changes outside Terraform to keep infrastructure consistent and manageable.
Final Answer:
Use create_before_destroy = true and avoid manual changes outside Terraform -> Option B