Bird
Raised Fist0
Terraformcloud~20 mins

Test file structure in Terraform - Practice Problems & Coding Challenges

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
🎖️
Terraform File Structure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Architecture
intermediate
2:00remaining
Identify the correct Terraform file structure for a multi-environment setup

You want to organize Terraform files for two environments: dev and prod. Which file structure below correctly separates environment-specific configurations while sharing common resources?

Aroot/main.tf, root/variables.tf, root/dev/main.tf, root/prod/main.tf
Broot/main.tf, root/variables.tf, root/environments/dev.tf, root/environments/prod.tf
Croot/dev/main.tf, root/dev/variables.tf, root/prod/main.tf, root/prod/variables.tf
Droot/main.tf, root/dev.tf, root/prod.tf, root/variables.tf
Attempts:
2 left
💡 Hint

Think about how to keep environment files separate and complete with their own variables.

Configuration
intermediate
1:30remaining
Determine which Terraform file will load variables for the production environment

Given this file structure:

root/
  main.tf
  variables.tf
  prod/
    main.tf
    variables.tf
  dev/
    main.tf
    variables.tf

When running Terraform in the prod folder, which variables.tf file is loaded?

ABoth root/variables.tf and prod/variables.tf
Bprod/variables.tf
Cdev/variables.tf
Droot/variables.tf
Attempts:
2 left
💡 Hint

Terraform loads variables from the current working directory.

service_behavior
advanced
1:30remaining
What happens if Terraform files are duplicated in root and environment folders?

You have main.tf files in both the root folder and the dev folder. You run Terraform commands inside the dev folder. What will Terraform do?

ATerraform only uses the main.tf in the current <code>dev</code> folder
BTerraform merges both main.tf files automatically
CTerraform throws an error about duplicate main.tf files
DTerraform uses the root main.tf and ignores the dev folder
Attempts:
2 left
💡 Hint

Terraform works with files in the current directory only.

security
advanced
1:30remaining
Which file should NOT contain sensitive data in a Terraform project?

In a Terraform project with multiple files, which file is the least appropriate place to store sensitive information like passwords or API keys?

Amain.tf
Bterraform.tfvars
Cvariables.tf
Dsecret.auto.tfvars
Attempts:
2 left
💡 Hint

Think about which files are meant for configuration vs. variable values.

Best Practice
expert
2:30remaining
Choose the best Terraform file structure for reusable modules and environment configs

You want to create reusable Terraform modules and separate environment configurations. Which file structure below follows best practices?

A
root/
  main.tf
  variables.tf
  dev_main.tf
  prod_main.tf
B
root/
  network_module.tf
  compute_module.tf
  dev.tf
  prod.tf
C
root/
  modules/
    main.tf
  dev/
    main.tf
  prod/
    main.tf
D
root/
  modules/
    network/
      main.tf
    compute/
      main.tf
  envs/
    dev/
      main.tf
    prod/
      main.tf
Attempts:
2 left
💡 Hint

Think about separating reusable code and environment-specific configs clearly.

Practice

(1/5)
1. What is the main reason to keep Terraform test files separate from production code files?
easy
A. Because Terraform does not support test files inside the main folder
B. To avoid mixing test code with production code and keep the project organized
C. To reduce the size of the Terraform state file
D. Because test files must be written in a different language

Solution

  1. Step 1: Understand file organization purpose

    Separating test files helps keep the project clean and easier to manage by not mixing test and production code.
  2. Step 2: Recognize Terraform best practices

    Terraform encourages clear separation to avoid confusion and accidental deployment of test code.
  3. Final Answer:

    To avoid mixing test code with production code and keep the project organized -> Option B
  4. Quick Check:

    Separate test files = organized project [OK]
Hint: Keep test files separate to avoid confusion [OK]
Common Mistakes:
  • Thinking test files must be in main folder
  • Believing test files affect state size
  • Assuming test files require different language
2. Which of the following is the correct folder structure for organizing Terraform files including tests?
easy
A. /main.tf, /variables.tf, /outputs.tf, /test/main_test.tf
B. /main.tf, /variables.tf, /outputs.tf, /modules/tests/test_main.tf
C. /main.tf, /variables.tf, /outputs.tf, /tests/test_main.tf
D. /main.tf, /variables.tf, /outputs.tf, /tests/main_test.tf

Solution

  1. Step 1: Identify standard test folder naming

    Tests are usually placed in a separate folder named 'tests' at the root level for clarity.
  2. Step 2: Check file naming conventions

    Test files often have '_test' suffix to indicate their purpose, e.g., 'main_test.tf'.
  3. Final Answer:

    /main.tf, /variables.tf, /outputs.tf, /tests/main_test.tf -> Option D
  4. Quick Check:

    Tests folder with *_test.tf files = correct structure [OK]
Hint: Use 'tests' folder with *_test.tf files [OK]
Common Mistakes:
  • Placing tests inside modules folder
  • Naming test folder as 'test' instead of 'tests'
  • Not using _test suffix for test files
3. Given this Terraform project structure:
/main.tf
/variables.tf
/outputs.tf
/tests/test_main.tf

What will happen if you run terraform apply from the root directory?
medium
A. Terraform will apply infrastructure defined in main.tf and ignore test files
B. Terraform will apply both main.tf and test_main.tf causing errors
C. Terraform will only apply test_main.tf and ignore main.tf
D. Terraform will fail because test files are not allowed

Solution

  1. Step 1: Understand Terraform file loading behavior

    Terraform loads *.tf files in the current directory but ignores files in subfolders unless explicitly included.
  2. Step 2: Recognize test folder separation effect

    Files inside /tests are not loaded by default during terraform apply, so only main.tf and related files are applied.
  3. Final Answer:

    Terraform will apply infrastructure defined in main.tf and ignore test files -> Option A
  4. Quick Check:

    Terraform applies root *.tf files only [OK]
Hint: Terraform ignores test folder files by default [OK]
Common Mistakes:
  • Assuming terraform applies all .tf files recursively
  • Thinking test files run automatically
  • Believing terraform apply fails due to test files
4. You have this folder structure:
/main.tf
/variables.tf
/outputs.tf
/tests/test_main.tf

Running terraform apply gives an error about duplicate resource definitions. What is the likely cause?
medium
A. Outputs are missing in outputs.tf
B. Variables are defined twice in variables.tf
C. Terraform is loading test_main.tf and main.tf causing duplicate resources
D. Terraform state file is corrupted

Solution

  1. Step 1: Analyze error cause

    Duplicate resource errors usually mean Terraform sees the same resource defined more than once.
  2. Step 2: Check file loading

    If test files are accidentally loaded (e.g., by running terraform in /tests), resources duplicate with main.tf definitions.
  3. Final Answer:

    Terraform is loading test_main.tf and main.tf causing duplicate resources -> Option C
  4. Quick Check:

    Duplicate resources = multiple files loaded [OK]
Hint: Run terraform only in root, not in tests folder [OK]
Common Mistakes:
  • Blaming variables or outputs for duplicate resource error
  • Ignoring that test files can cause duplicates if loaded
  • Assuming state file corruption without checking files
5. You want to reuse infrastructure code in multiple projects and keep tests separate. Which folder structure best supports this using Terraform modules and tests?
hard
A. /modules/network/main.tf, /modules/network/variables.tf, /tests/network_test.tf, /main.tf
B. /network/main.tf, /network/variables.tf, /tests/network_test.tf, /main.tf
C. /modules/network/main.tf, /tests/network/main_test.tf, /main.tf
D. /modules/network/main.tf, /modules/network/tests/network_test.tf, /main.tf

Solution

  1. Step 1: Identify module folder best practice

    Modules should be inside a 'modules' folder with their own files for reuse.
  2. Step 2: Separate tests outside modules

    Tests should be in a top-level 'tests' folder to avoid mixing with reusable module code.
  3. Final Answer:

    /modules/network/main.tf, /modules/network/variables.tf, /tests/network_test.tf, /main.tf -> Option A
  4. Quick Check:

    Modules in 'modules', tests in 'tests' folder [OK]
Hint: Keep modules and tests in separate top-level folders [OK]
Common Mistakes:
  • Placing tests inside modules folder
  • Not using a modules folder for reusable code
  • Mixing test files with main project files