Bird
Raised Fist0
Terraformcloud~20 mins

What is Infrastructure as Code in Terraform - Practice Questions & Exercises

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
Challenge - 5 Problems
🎖️
IaC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What does Infrastructure as Code (IaC) primarily enable?

Infrastructure as Code (IaC) is a key practice in cloud computing. What is the main benefit it provides?

AIt allows managing infrastructure using code to automate setup and changes.
BIt requires writing code only for application logic, not infrastructure.
CIt replaces all physical servers with virtual machines manually.
DIt is a tool for monitoring network traffic in real time.
Attempts:
2 left
💡 Hint

Think about how code can help with infrastructure tasks.

❓ Architecture
intermediate
2:00remaining
Which Terraform file defines the desired infrastructure state?

In Terraform, which file typically contains the code that describes the infrastructure you want to create?

Avariables.tf
Bmain.tf
Coutputs.tf
Dterraform.tfstate
Attempts:
2 left
💡 Hint

Look for the file where resources are declared.

❓ service_behavior
advanced
2:00remaining
What happens when you run 'terraform apply' with changed code?

You have updated your Terraform code to add a new server. What does terraform apply do?

AIt ignores the changes and keeps the infrastructure as before.
BIt deletes all existing servers and creates only the new one.
CIt only shows the changes but does not apply them.
DIt creates the new server and updates the infrastructure to match the code.
Attempts:
2 left
💡 Hint

Think about how Terraform syncs real infrastructure with code.

❓ security
advanced
2:00remaining
Which practice improves security when using Infrastructure as Code?

When managing infrastructure with code, which practice helps keep secrets safe?

AShare code files with secrets openly to all team members.
BWrite all passwords directly in the Terraform files for easy access.
CStore secrets in environment variables or secret managers, not in code files.
DUse plain text files in the repository to store sensitive data.
Attempts:
2 left
💡 Hint

Think about how to keep passwords and keys safe.

✅ Best Practice
expert
2:00remaining
What is a key benefit of using version control with Infrastructure as Code?

Why should you store your Infrastructure as Code files in a version control system like Git?

AIt tracks changes over time and allows safe collaboration and rollback.
BIt automatically applies infrastructure changes without review.
CIt prevents any changes to infrastructure code once committed.
DIt encrypts the infrastructure code to hide it from users.
Attempts:
2 left
💡 Hint

Think about teamwork and history of changes.

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