Bird
Raised Fist0
Terraformcloud~10 mins

Terraform file organization - Interactive Code Practice

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 the main Terraform configuration file.

Terraform
resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "[1]"
}
Drag options to blanks, or click blank then click option'
At2.micro
Bt3.nano
Ct3.micro
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid instance types like 't3.nano' which may not be supported.
Typos in instance type string.
2fill in blank
medium

Complete the code to declare a variable for the AWS region.

Terraform
variable "region" {
  type    = string
  default = "[1]"
}
Drag options to blanks, or click blank then click option'
Aap-south-1
Bus-east-1
Ceu-central-1
Dus-west-2
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid region codes.
Forgetting to put the region in quotes.
3fill in blank
hard

Fix the error in the provider block to specify the AWS region variable.

Terraform
provider "aws" {
  region = [1]
}
Drag options to blanks, or click blank then click option'
Aregion
B"region"
Cvar.region
Daws_region
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name without var. prefix.
Putting the variable name in quotes, making it a literal string.
4fill in blank
hard

Fill both blanks to create an output that shows the instance's public IP.

Terraform
output "instance_ip" {
  value = [1].[2]
}
Drag options to blanks, or click blank then click option'
Aaws_instance.example
Bpublic_ip
Cprivate_ip
Dinstance_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using private_ip instead of public_ip.
Referencing the wrong resource name.
5fill in blank
hard

Fill all three blanks to define a resource with a tag name and a count of 2.

Terraform
resource "aws_instance" "[1]" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  count         = [2]
  tags = {
    Name = "[3]"
  }
}
Drag options to blanks, or click blank then click option'
Aweb_server
B2
CMyServer
Ddb_server
Attempts:
3 left
💡 Hint
Common Mistakes
Using count as a string instead of a number.
Mismatching resource name and tag name.

Practice

(1/5)
1. What is the main purpose of organizing Terraform files into separate files like main.tf, variables.tf, and outputs.tf?
easy
A. To avoid using variables in the configuration
B. To reduce the number of resources created
C. To speed up Terraform apply operations
D. To make the code easier to read and maintain

Solution

  1. Step 1: Understand file separation purpose

    Separating files by purpose helps organize code logically, making it easier to find and update parts.
  2. Step 2: Recognize benefits of organization

    This organization improves readability and maintainability but does not affect resource count or speed directly.
  3. Final Answer:

    To make the code easier to read and maintain -> Option D
  4. Quick Check:

    File organization = easier maintenance [OK]
Hint: Think about how folders help organize papers [OK]
Common Mistakes:
  • Confusing file organization with resource optimization
  • Thinking it speeds up Terraform operations
  • Believing variables are avoided by file separation
2. Which of the following is the correct way to declare a variable in a separate variables.tf file?
easy
A. variable "instance_type" { default = "t2.micro" }
B. var instance_type = "t2.micro"
C. variable instance_type = "t2.micro"
D. var "instance_type" { default = "t2.micro" }

Solution

  1. Step 1: Recall Terraform variable syntax

    Terraform variables are declared with the keyword variable followed by the variable name in quotes and a block with attributes.
  2. Step 2: Match correct syntax

    variable "instance_type" { default = "t2.micro" } matches the correct syntax: variable "name" { default = "value" }. Other options use invalid syntax.
  3. Final Answer:

    variable "instance_type" { default = "t2.micro" } -> Option A
  4. Quick Check:

    Correct variable syntax = variable "instance_type" { default = "t2.micro" } [OK]
Hint: Variables use 'variable' keyword with quotes and braces [OK]
Common Mistakes:
  • Omitting quotes around variable name
  • Using 'var' instead of 'variable' keyword
  • Assigning variable like a normal programming variable
3. Given these files:
main.tf:
resource "aws_instance" "web" {
  ami           = var.ami_id
  instance_type = var.instance_type
}

variables.tf:
variable "ami_id" {}
variable "instance_type" {}

outputs.tf:
output "instance_id" {
  value = aws_instance.web.id
}

What will Terraform output after apply if the instance ID is i-1234567890abcdef0?
medium
A. "instance_id" = "i-1234567890abcdef0"
B. "instance_type" = "i-1234567890abcdef0"
C. "ami_id" = "i-1234567890abcdef0"
D. No output will be shown

Solution

  1. Step 1: Identify output block purpose

    The outputs.tf file defines an output named instance_id that returns the ID of the AWS instance resource.
  2. Step 2: Match output value with resource ID

    After apply, Terraform shows outputs with their names and values. The output will show instance_id with the instance's actual ID.
  3. Final Answer:

    "instance_id" = "i-1234567890abcdef0" -> Option A
  4. Quick Check:

    Output name matches resource ID [OK]
Hint: Outputs show resource values by their output names [OK]
Common Mistakes:
  • Confusing variable names with output names
  • Expecting variables to show as outputs automatically
  • Thinking no output is shown without explicit output block
4. You have a variables.tf file with:
variable "region" {
  default = "us-east-1"
}

But Terraform apply fails with an error about missing provider configuration. What is the likely cause?
medium
A. Variables cannot have default values
B. The variable region must be declared in main.tf
C. The provider block is missing or not configured
D. Terraform requires region to be set in outputs.tf

Solution

  1. Step 1: Understand provider role

    Terraform needs a provider block to know which cloud and region to use. Variables alone do not configure providers.
  2. Step 2: Identify missing provider configuration

    If the provider block is missing or lacks region setting, Terraform cannot connect to the cloud, causing errors.
  3. Final Answer:

    The provider block is missing or not configured -> Option C
  4. Quick Check:

    Provider block required for cloud access [OK]
Hint: Always check provider block for cloud setup [OK]
Common Mistakes:
  • Assuming variables configure providers automatically
  • Placing variables in wrong files to fix provider errors
  • Expecting outputs to fix provider configuration
5. You want to organize a Terraform project with multiple environments (dev, prod). Which file organization approach is best practice?
hard
A. Use one main.tf and switch environment by changing variable defaults
B. Create separate folders for each environment, each with its own main.tf, variables.tf, and outputs.tf
C. Put all environments in one folder using different variable names
D. Store environment configs only in terraform.tfvars without separate folders

Solution

  1. Step 1: Understand environment isolation

    Separate folders per environment keep configurations isolated, avoiding accidental resource overlap.
  2. Step 2: Recognize best practice for clarity and safety

    Each folder having its own files allows independent management and clear separation of dev and prod.
  3. Final Answer:

    Create separate folders for each environment, each with its own main.tf, variables.tf, and outputs.tf -> Option B
  4. Quick Check:

    Separate folders = safer multi-env management [OK]
Hint: Use folders to separate environments clearly [OK]
Common Mistakes:
  • Mixing environments in one folder causing conflicts
  • Relying only on variable changes without folder separation
  • Not isolating state files per environment