Bird
Raised Fist0
Terraformcloud~10 mins

What is Infrastructure as Code in Terraform - Visual Explanation

Choose your learning style10 modes available

Start learning this pattern below

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
Process Flow - What is Infrastructure as Code
Write code to define infrastructure
↓
Run code with Terraform
↓
Terraform reads code
↓
Terraform creates/updates resources
↓
Infrastructure is ready and managed by code
↓
Change code -> Run Terraform again -> Update infrastructure
Infrastructure as Code means writing code to create and manage cloud resources automatically. Terraform reads this code and makes the real infrastructure match it.
Execution Sample
Terraform
resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-unique-bucket-123"
  acl    = "private"
}
This code tells Terraform to create a private S3 bucket named 'my-unique-bucket-123'.
Process Table
StepActionTerraform ReadsResult
1Start Terraform applyReads resource block for aws_s3_bucket.my_bucketPlans to create S3 bucket named 'my-unique-bucket-123'
2Create resourceSends request to AWS to create bucketAWS creates the bucket successfully
3Verify creationChecks bucket existsBucket is confirmed created
4FinishNo more changesInfrastructure matches code
💡 Terraform finishes because infrastructure matches the code definition.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
aws_s3_bucket.my_bucketundefinedplannedcreatedverifiedready
Key Moments - 3 Insights
Why do we write code instead of clicking in the cloud console?
Writing code lets us repeat, track, and share infrastructure setup easily. Execution table step 1 shows Terraform reading code to plan creation.
What happens if we change the code after creation?
Terraform compares current infrastructure with code and updates resources accordingly, as shown in the concept flow last step.
Does Terraform create resources immediately when we write code?
No, Terraform only creates resources when we run 'terraform apply', as shown in execution table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does Terraform do at step 2?
AVerifies the bucket exists
BReads the code to plan changes
CSends request to AWS to create the bucket
DFinishes execution
💡 Hint
Check the 'Action' and 'Result' columns in step 2 of the execution table.
According to the variable tracker, what is the state of aws_s3_bucket.my_bucket after step 3?
Aundefined
Bverified
Cplanned
Dcreated
💡 Hint
Look at the 'After Step 3' column for aws_s3_bucket.my_bucket in the variable tracker.
If we change the bucket name in the code and run Terraform again, what will happen?
ATerraform will plan to create a new bucket and possibly delete the old one
BTerraform will ignore the change
CTerraform will update the existing bucket to the new name
DTerraform will crash
💡 Hint
Refer to the concept flow last step about changing code and running Terraform again.
Concept Snapshot
Infrastructure as Code means writing code to define cloud resources.
Terraform reads this code and creates or updates resources automatically.
This makes infrastructure repeatable, trackable, and easy to manage.
Changes in code lead to updates in real infrastructure when Terraform runs.
No manual clicks needed; code is the source of truth.
Full Transcript
Infrastructure as Code is a way to manage cloud resources by writing code instead of clicking in a console. You write code that describes what you want, like creating a storage bucket. Then you run Terraform, which reads your code and makes the cloud match it. Terraform plans what to do, creates or updates resources, and checks they exist. If you change the code later, running Terraform again updates the infrastructure to match. This method helps keep infrastructure consistent, easy to share, and repeatable.

Practice

(1/5)
1. What does Infrastructure as Code mean in simple terms?
easy
A. Using physical servers instead of cloud
B. Manually clicking buttons in the cloud console
C. Writing text files to create and manage cloud resources
D. Writing code only for applications, not infrastructure

Solution

  1. Step 1: Understand the concept of Infrastructure as Code

    It means using code or text files to define and manage infrastructure instead of manual steps.
  2. Step 2: Compare options with this definition

    Only Writing text files to create and manage cloud resources describes writing text files to manage cloud resources, matching the concept.
  3. Final Answer:

    Writing text files to create and manage cloud resources -> Option C
  4. Quick Check:

    Infrastructure as Code = writing text files [OK]
Hint: Think: code controls infrastructure, not manual clicks [OK]
Common Mistakes:
  • Confusing manual setup with code-based setup
  • Thinking Infrastructure as Code means only coding apps
  • Assuming physical servers are Infrastructure as Code
2. Which of these is a correct Terraform resource block syntax?
easy
A. resource "aws_instance" "web" { ami = "ami-123" instance_type = "t2.micro" }
B. resource aws_instance web { ami = ami-123 instance_type = t2.micro }
C. resource "aws_instance" web { ami = "ami-123" instance_type = "t2.micro" }
D. resource "aws_instance" "web" ( ami = "ami-123", instance_type = "t2.micro" )

Solution

  1. Step 1: Recall Terraform resource block syntax

    It requires the keyword resource, resource type and name in quotes, then braces with key-value pairs.
  2. Step 2: Check each option for correct syntax

    resource "aws_instance" "web" { ami = "ami-123" instance_type = "t2.micro" } uses quotes for type and name, braces, and key-value pairs with equals and quotes correctly.
  3. Final Answer:

    resource "aws_instance" "web" { ami = "ami-123" instance_type = "t2.micro" } -> Option A
  4. Quick Check:

    Correct Terraform resource syntax = resource "aws_instance" "web" { ami = "ami-123" instance_type = "t2.micro" } [OK]
Hint: Look for quotes around type and name, and braces {} [OK]
Common Mistakes:
  • Missing quotes around resource type or name
  • Using parentheses instead of braces
  • Not quoting string values
3. Given this Terraform snippet:
resource "aws_s3_bucket" "bucket" {
  bucket = "my-bucket"
  acl    = "private"
}

What will Terraform do when applied?
medium
A. Create a public S3 bucket named 'my-bucket'
B. Throw a syntax error due to missing region
C. Delete the S3 bucket named 'my-bucket'
D. Create a private S3 bucket named 'my-bucket'

Solution

  1. Step 1: Analyze the resource block

    The resource defines an AWS S3 bucket named 'my-bucket' with ACL set to 'private'.
  2. Step 2: Understand the effect of ACL 'private'

    This means the bucket will be created with private access, not public.
  3. Final Answer:

    Create a private S3 bucket named 'my-bucket' -> Option D
  4. Quick Check:

    ACL 'private' means private bucket [OK]
Hint: ACL 'private' means bucket is not public [OK]
Common Mistakes:
  • Assuming default is public
  • Thinking missing region causes syntax error
  • Confusing create with delete actions
4. This Terraform code has an error:
resource "aws_instance" "web" {
  ami = "ami-123"
  instance_type = t2.micro
}

What is the error?
medium
A. Missing resource name
B. Missing quotes around instance_type value
C. Wrong resource type
D. Missing equals sign after ami

Solution

  1. Step 1: Check the instance_type line

    The value t2.micro is not in quotes, but it should be a string.
  2. Step 2: Confirm correct syntax for string values

    Terraform requires string values to be in quotes, so missing quotes cause an error.
  3. Final Answer:

    Missing quotes around instance_type value -> Option B
  4. Quick Check:

    String values need quotes in Terraform [OK]
Hint: String values always need quotes in Terraform [OK]
Common Mistakes:
  • Forgetting quotes around string values
  • Thinking resource name is missing
  • Assuming equals sign is missing
5. You want to create two identical AWS EC2 instances using Terraform without repeating code. Which feature helps you do this efficiently?
hard
A. Using a for_each or count loop in the resource block
B. Manually copying the resource block twice
C. Writing separate Terraform files for each instance
D. Using different resource types for each instance

Solution

  1. Step 1: Understand the goal to avoid repetition

    We want to create multiple similar resources without copying code.
  2. Step 2: Identify Terraform features for repetition

    Terraform supports 'count' and 'for_each' to create multiple instances from one block.
  3. Step 3: Evaluate options

    Using a for_each or count loop in the resource block uses these features, while others involve manual repetition or incorrect methods.
  4. Final Answer:

    Using a for_each or count loop in the resource block -> Option A
  5. Quick Check:

    Loops in Terraform = efficient resource creation [OK]
Hint: Use count or for_each to repeat resources without copy-paste [OK]
Common Mistakes:
  • Copy-pasting resource blocks manually
  • Creating separate files unnecessarily
  • Using different resource types for same resource