Bird
Raised Fist0
Terraformcloud~10 mins

Module composition patterns in Terraform - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a module source.

Terraform
module "network" {
  source = [1]
}
Drag options to blanks, or click blank then click option'
A"aws_vpc"
B"./modules/network"
C"terraform.io/modules"
D"network_module"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a module name instead of a path for source.
Omitting quotes around the source path.
2fill in blank
medium

Complete the code to pass a variable to a module.

Terraform
module "compute" {
  source = "./modules/compute"
  instance_type = [1]
}
Drag options to blanks, or click blank then click option'
A"t2.micro"
Bt2.micro
Cinstance_type
Dvar.instance_type
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around string values.
Using variable names without the var. prefix.
3fill in blank
hard

Fix the error in the module call to correctly reference an output.

Terraform
output "subnet_id" {
  value = module.network.[1]
}
Drag options to blanks, or click blank then click option'
Asubnet-id
Bsubnet
CsubnetId
Dsubnet_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using camelCase or hyphens instead of underscores.
Referencing outputs that do not exist in the module.
4fill in blank
hard

Fill both blanks to create a module block with a variable and output reference.

Terraform
module "storage" {
  source = [1]
  bucket_name = [2]
}

output "bucket" {
  value = module.storage.bucket_name
}
Drag options to blanks, or click blank then click option'
A"./modules/storage"
B"my-bucket"
Cvar.bucket_name
D"bucket_name"
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable references without defining them.
Not quoting string values.
5fill in blank
hard

Fill all three blanks to define a module with variables and use an output in another resource.

Terraform
module "db" {
  source = [1]
  db_name = [2]
  db_user = [3]
}

resource "aws_db_instance" "main" {
  identifier = module.db.db_name
  username = module.db.db_user
  engine = "mysql"
  instance_class = "db.t3.micro"
}
Drag options to blanks, or click blank then click option'
A"./modules/database"
B"mydatabase"
C"admin"
D"database_module"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect variable names.
Not quoting string values.

Practice

(1/5)
1. What is the main purpose of using module composition patterns in Terraform?
easy
A. To combine multiple modules by passing outputs as inputs for better organization
B. To write all resources in a single large file
C. To avoid using variables in Terraform configurations
D. To deploy resources without any dependencies

Solution

  1. Step 1: Understand module composition

    Module composition means connecting modules by using outputs from one as inputs to another.
  2. Step 2: Recognize benefits

    This approach keeps infrastructure code organized, reusable, and easier to manage.
  3. Final Answer:

    To combine multiple modules by passing outputs as inputs for better organization -> Option A
  4. Quick Check:

    Module composition = combining modules with outputs as inputs [OK]
Hint: Think of modules as building blocks connected by outputs [OK]
Common Mistakes:
  • Thinking modules should be combined by copying code
  • Believing variables are avoided in module composition
  • Assuming modules work independently without connections
2. Which of the following is the correct syntax to pass an output from one module as an input to another in Terraform?
easy
A. output.moduleA.output_name
B. module.moduleA.output_name
C. var.moduleA.output_name
D. resource.moduleA.output_name

Solution

  1. Step 1: Recall Terraform module output reference syntax

    Outputs from a module are accessed using module.<module_name>.<output_name>.
  2. Step 2: Compare options

    Only module.moduleA.output_name matches the correct syntax for accessing module outputs.
  3. Final Answer:

    module.moduleA.output_name -> Option B
  4. Quick Check:

    Module output reference = module.module_name.output_name [OK]
Hint: Outputs are accessed via module.<name>.<output> [OK]
Common Mistakes:
  • Using var or resource instead of module to access outputs
  • Confusing output blocks with module references
  • Using incorrect keywords like output.moduleA
3. Given the following Terraform code snippet, what will be the value of moduleB's input instance_id?
module "moduleA" {
  source = "./moduleA"
}

module "moduleB" {
  source      = "./moduleB"
  instance_id = module.moduleA.instance_id
}
medium
A. The value of the output named instance_id from moduleA
B. A syntax error because instance_id is not defined
C. The string literal 'module.moduleA.instance_id'
D. Null because moduleB does not have instance_id output

Solution

  1. Step 1: Identify module output usage

    The input instance_id for moduleB is assigned from module.moduleA.instance_id, which means it uses the output named instance_id from moduleA.
  2. Step 2: Understand value passing

    This passes the actual value of moduleA's output to moduleB's input, not a string or error.
  3. Final Answer:

    The value of the output named instance_id from moduleA -> Option A
  4. Quick Check:

    moduleB.instance_id = moduleA.instance_id output value [OK]
Hint: module.<name>.<output> passes actual output value, not string [OK]
Common Mistakes:
  • Thinking it passes a string literal instead of value
  • Assuming syntax error due to missing variable declaration
  • Confusing inputs and outputs between modules
4. You have two modules: moduleA outputs vpc_id, and moduleB requires vpc_id as input. The following code causes an error:
module "moduleA" {
  source = "./moduleA"
}

module "moduleB" {
  source = "./moduleB"
  vpc_id = var.vpc_id
}
What is the most likely cause of the error?
medium
A. moduleB source path is incorrect
B. moduleA does not have an output named vpc_id
C. moduleB is using var.vpc_id instead of module.moduleA.vpc_id as input
D. Terraform does not support passing outputs between modules

Solution

  1. Step 1: Check input assignment in moduleB

    moduleB expects vpc_id input, but it is assigned from var.vpc_id, which is a variable, not the output from moduleA.
  2. Step 2: Identify correct way to pass output

    The correct way is to assign vpc_id = module.moduleA.vpc_id to pass the output from moduleA.
  3. Final Answer:

    moduleB is using var.vpc_id instead of module.moduleA.vpc_id as input -> Option C
  4. Quick Check:

    Pass outputs via module.moduleA.output, not var [OK]
Hint: Use module.<name>.<output> to pass outputs, not var.<name> [OK]
Common Mistakes:
  • Assuming var.vpc_id automatically links to moduleA output
  • Ignoring the need to explicitly connect modules
  • Thinking Terraform cannot pass outputs between modules
5. You want to create a composed Terraform setup where moduleC depends on outputs from both moduleA and moduleB. Which pattern correctly composes these modules to pass outputs as inputs to moduleC?
// Option A
module "moduleC" {
  source = "./moduleC"
  inputA = module.moduleA.outputA
  inputB = module.moduleB.outputB
}

// Option B
module "moduleC" {
  source = "./moduleC"
  inputA = var.outputA
  inputB = var.outputB
}

// Option C
module "moduleC" {
  source = "./moduleC"
  inputA = module.moduleB.outputB
  inputB = module.moduleA.outputA
}

// Option D
module "moduleC" {
  source = "./moduleC"
  inputA = module.moduleA.outputB
  inputB = module.moduleB.outputA
}
hard
A. Mix outputs incorrectly between modules
B. Use variables instead of module outputs for inputs
C. Swap inputs but still use correct module outputs
D. Pass outputs correctly matching input names from moduleA and moduleB

Solution

  1. Step 1: Match inputs to correct module outputs

    Pass outputs correctly matching input names from moduleA and moduleB passes inputA from moduleA.outputA and inputB from moduleB.outputB, matching names correctly.
  2. Step 2: Evaluate other options

    Use variables instead of module outputs for inputs uses variables instead of outputs, which breaks composition. Swap inputs but still use correct module outputs swaps the assignments, mismatching inputA (expects moduleA.outputA) with moduleB.outputB and inputB with moduleA.outputA. Mix outputs incorrectly between modules mixes outputs incorrectly, likely causing errors.
  3. Final Answer:

    Pass outputs correctly matching input names from moduleA and moduleB -> Option D
  4. Quick Check:

    Correct output-to-input mapping = Pass outputs correctly matching input names from moduleA and moduleB [OK]
Hint: Match input names to correct module outputs carefully [OK]
Common Mistakes:
  • Swapping outputs between modules without checking input expectations
  • Using variables instead of module outputs
  • Mixing output names incorrectly causing runtime errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard