Bird
Raised Fist0
Terraformcloud~10 mins

Why patterns solve common problems in Terraform - Test Your Understanding

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a resource block in Terraform.

Terraform
resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "[1]"
}
Drag options to blanks, or click blank then click option'
At3.micro
Bm4.large
Ct2.micro
Dc5.xlarge
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing an instance type that does not exist or is too large for simple examples.
2fill in blank
medium

Complete the code to specify the provider in Terraform configuration.

Terraform
provider "aws" {
  region = "[1]"
}
Drag options to blanks, or click blank then click option'
Aap-southeast-2
Bus-west-1
Ceu-central-1
Dus-east-1
Attempts:
3 left
💡 Hint
Common Mistakes
Selecting a region that is less common or not supported in the example.
3fill in blank
hard

Fix the error in the Terraform variable declaration.

Terraform
variable "instance_count" {
  type    = [1]
  default = 1
}
Drag options to blanks, or click blank then click option'
A"string"
B"number"
Cnumber
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using quoted types like "number" or language-specific types like int instead of Terraform type keywords.
4fill in blank
hard

Fill both blanks to create a map variable with default values.

Terraform
variable "tags" {
  type    = [1]
  default = [2]
}
Drag options to blanks, or click blank then click option'
Amap(string)
Blist(string)
C{"Environment" = "dev", "Owner" = "team"}
D["dev", "team"]
Attempts:
3 left
💡 Hint
Common Mistakes
Using list type instead of map, or using list syntax for default values.
5fill in blank
hard

Fill all three blanks to output the instance ID from a resource.

Terraform
output "instance_id" {
  value = [1].[2].[3]
}
Drag options to blanks, or click blank then click option'
Aaws_instance
Bexample
Cid
Dinstance
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect resource names or attributes that do not exist.

Practice

(1/5)
1. Why do Terraform patterns help when building cloud infrastructure?
easy
A. They automatically fix errors in the code.
B. They make the cloud infrastructure run faster.
C. They save time and reduce mistakes by reusing code.
D. They replace the need for any manual setup.

Solution

  1. Step 1: Understand what patterns do

    Patterns are reusable ways to solve common problems, so they save time and reduce errors.
  2. Step 2: Compare options

    Only They save time and reduce mistakes by reusing code. correctly states that patterns save time and reduce mistakes by reusing code.
  3. Final Answer:

    They save time and reduce mistakes by reusing code. -> Option C
  4. Quick Check:

    Patterns save time and reduce mistakes = A [OK]
Hint: Patterns reuse code to save time and avoid errors [OK]
Common Mistakes:
  • Thinking patterns make infrastructure faster
  • Believing patterns fix code automatically
  • Assuming patterns remove all manual work
2. Which of the following is the correct way to use a module pattern in Terraform?
easy
A. module "example" { source = "./module_path" }
B. module example { source = "./module_path" }
C. module "example" (source = "./module_path")
D. module example: source = "./module_path"

Solution

  1. Step 1: Recall Terraform module syntax

    Terraform modules require the keyword module, a quoted name, and a block with source inside curly braces.
  2. Step 2: Check each option

    Only module "example" { source = "./module_path" } uses correct syntax with quotes and braces.
  3. Final Answer:

    module "example" { source = "./module_path" } -> Option A
  4. Quick Check:

    Correct module syntax uses quotes and braces = D [OK]
Hint: Modules need quotes around name and braces for block [OK]
Common Mistakes:
  • Omitting quotes around module name
  • Using parentheses instead of braces
  • Using colon instead of equals sign
3. Given this Terraform snippet using a module pattern:
module "web" {
  source = "./web_module"
  instance_count = 3
}

What will happen when you run terraform apply?
medium
A. Terraform creates only 1 instance ignoring instance_count.
B. Terraform deletes all existing instances.
C. Terraform throws a syntax error due to missing variable declaration.
D. Terraform creates 3 instances as defined in the module.

Solution

  1. Step 1: Understand module usage with variables

    The module is called with instance_count = 3, so it passes this value to the module.
  2. Step 2: Predict apply behavior

    Terraform will create 3 instances as the module uses instance_count to create resources.
  3. Final Answer:

    Terraform creates 3 instances as defined in the module. -> Option D
  4. Quick Check:

    Module variables control resource count = B [OK]
Hint: Module variables control resource creation count [OK]
Common Mistakes:
  • Assuming instance_count is ignored without variable block
  • Expecting syntax error without variable declaration in snippet
  • Thinking Terraform deletes resources on apply
4. You wrote this Terraform module call:
module "db" {
  source = "./db_module"
  size = 2
}

But Terraform shows an error: Unsupported argument. What is the likely cause?
medium
A. The module does not define a variable named size.
B. The source path is incorrect and missing files.
C. The module name must not be quoted.
D. Terraform requires count instead of size.

Solution

  1. Step 1: Understand Unsupported argument error

    This error means the module does not expect the argument provided.
  2. Step 2: Check argument name

    If the module does not define a variable named size, passing it causes the error.
  3. Final Answer:

    The module does not define a variable named size. -> Option A
  4. Quick Check:

    Unsupported argument means unknown variable = A [OK]
Hint: Check module variables match arguments passed [OK]
Common Mistakes:
  • Assuming source path causes Unsupported argument
  • Removing quotes from module name
  • Confusing variable names with Terraform reserved words
5. You want to reuse a common network setup in multiple Terraform projects. Which pattern best solves this problem?
hard
A. Write the network code only once and never update it.
B. Create a reusable module for the network and call it in each project.
C. Copy and paste the network code into every project manually.
D. Use a different cloud provider for each project.

Solution

  1. Step 1: Identify the best reuse method

    Reusable modules allow sharing common code across projects easily.
  2. Step 2: Evaluate options

    Copy-pasting causes errors and maintenance issues; writing once without updates is impractical; changing providers is unrelated.
  3. Final Answer:

    Create a reusable module for the network and call it in each project. -> Option B
  4. Quick Check:

    Reusable modules solve code reuse best = C [OK]
Hint: Use modules to share common infrastructure code [OK]
Common Mistakes:
  • Copy-pasting code instead of using modules
  • Ignoring updates by writing code only once
  • Confusing cloud providers with code reuse