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
Recall & Review
beginner
What is dependency inversion in Terraform modules?
Dependency inversion means designing modules so that higher-level modules do not depend on lower-level modules directly. Instead, they depend on abstractions or inputs, allowing flexibility and easier changes.
Click to reveal answer
beginner
Why use dependency inversion with Terraform modules?
It helps keep modules reusable and independent. Changes in one module won't break others because dependencies are managed through inputs and outputs, not hard-coded references.
Click to reveal answer
intermediate
How do Terraform modules communicate dependencies without tight coupling?
Modules communicate using input variables and output values. One module outputs data, and another module receives it as input, avoiding direct internal references.
Click to reveal answer
intermediate
Example: How to invert dependency between a network module and an instance module?
The network module outputs subnet IDs. The instance module takes subnet IDs as input variables. The root module connects them by passing outputs to inputs, not by the instance module calling the network module directly.
Click to reveal answer
advanced
What is a benefit of dependency inversion when scaling Terraform projects?
It allows teams to work on modules independently, reduces errors from tight coupling, and makes it easier to replace or upgrade modules without affecting the whole infrastructure.
Click to reveal answer
What does dependency inversion in Terraform modules primarily promote?
ALoose coupling between modules
BDirect module calls inside other modules
CHard-coded resource IDs
DIgnoring module outputs
✗ Incorrect
Dependency inversion promotes loose coupling by using inputs and outputs instead of direct references.
How do Terraform modules share data without tight dependency?
AUsing output values and input variables
BBy importing each other's code
CBy hardcoding resource names
DBy running scripts inside modules
✗ Incorrect
Modules share data through outputs and inputs, which keeps them independent.
Which Terraform component connects module outputs to inputs?
ABackend
BProvider
CResource block
DRoot module
✗ Incorrect
The root module orchestrates passing outputs from one module as inputs to another.
What is a risk of not using dependency inversion in Terraform modules?
AModules automatically update
BModules run faster
CModules become tightly coupled and hard to maintain
DModules use fewer variables
✗ Incorrect
Without dependency inversion, modules depend directly on each other, making maintenance difficult.
Which practice aligns with dependency inversion in Terraform?
AHardcoding subnet IDs inside instance module
BPassing subnet IDs as input variables to instance module
CCalling network module inside instance module
DIgnoring module outputs
✗ Incorrect
Passing subnet IDs as inputs keeps modules independent and flexible.
Explain how dependency inversion improves Terraform module design.
Think about how modules avoid direct references and rely on data passed in.
You got /4 concepts.
Describe a simple example of dependency inversion between two Terraform modules.
Consider network and instance modules sharing subnet IDs.
You got /4 concepts.
Practice
(1/5)
1. What does dependency inversion mean in Terraform modules?
easy
A. Modules cannot accept variables
B. Modules depend on inputs instead of creating resources themselves
C. Modules always create all resources internally
D. Modules must be written in the root configuration
Solution
Step 1: Understand module dependency principle
Dependency inversion means modules should not create resources directly but rely on inputs.
Step 2: Identify correct description
Modules depend on inputs instead of creating resources themselves correctly states modules depend on inputs, making them flexible and reusable.
Final Answer:
Modules depend on inputs instead of creating resources themselves -> Option B
Quick Check:
Dependency inversion = Modules use inputs [OK]
Hint: Modules get resource info via inputs, not by creating resources [OK]
Common Mistakes:
Thinking modules must create all resources internally
Assuming modules cannot accept variables
Believing modules must be in root config
2. Which of the following is the correct way to pass a resource ID to a module in Terraform?
easy
A. module "example" { resource_id = aws_instance.example.id }
B. module "example" { input_id = aws_instance.example.id }
C. module "example" { instance_id = var.instance_id }
D. module "example" { instance_id = aws_instance.example.id }
Solution
Step 1: Identify correct variable passing syntax
Modules accept variables by name; the value can be a resource attribute like aws_instance.example.id.
Step 2: Check option correctness
module "example" { instance_id = aws_instance.example.id } correctly passes instance_id with the resource ID aws_instance.example.id.
Final Answer:
module "example" { instance_id = aws_instance.example.id } -> Option D
Quick Check:
Pass resource ID as variable = module "example" { instance_id = aws_instance.example.id } [OK]
Hint: Use variable name = resource.attribute to pass IDs [OK]
Common Mistakes:
Using undefined variable names like resource_id or input_id
Inside the module, the variable is declared as variable "subnet" { type = string }. What error will occur?
medium
A. Error: Unknown variable 'subnet_id' in module
B. Error: Variable 'subnet' not provided
C. No error, variable names can differ
D. Error: aws_subnet.app.id is invalid
Solution
Step 1: Compare variable name and input argument
The module expects a variable named 'subnet' but the input is 'subnet_id'.
Step 2: Understand Terraform variable matching
Terraform matches input arguments to variable names exactly. 'subnet_id' does not match any variable, causing an unsupported argument error.
Final Answer:
Error: Unknown variable 'subnet_id' in module -> Option A
Quick Check:
Variable name mismatch causes unknown variable error [OK]
Hint: Variable names must match exactly between module and call [OK]
Common Mistakes:
Assuming variable names can differ
Confusing variable name with resource attribute name
Ignoring error messages about missing variables
5. You want to create a reusable module for an AWS security group that attaches to any VPC. Which approach follows dependency inversion best?
hard
A. Module accepts a VPC ID as input and creates security group in that VPC
B. Module hardcodes a VPC ID inside the module code
C. Module requires the user to create security group outside and passes its ID
D. Module creates its own VPC and security group inside
Solution
Step 1: Understand dependency inversion for modules
Modules should not create dependent resources like VPCs but accept them as inputs.
Step 2: Evaluate options for best practice
Module accepts a VPC ID as input and creates security group in that VPC accepts VPC ID as input and creates the security group inside that VPC, following dependency inversion.
Final Answer:
Module accepts a VPC ID as input and creates security group in that VPC -> Option A
Quick Check:
Pass dependencies as inputs for flexibility [OK]
Hint: Pass VPC ID as input; module creates resources inside it [OK]
Common Mistakes:
Hardcoding resource IDs inside modules
Modules creating dependent resources themselves
Requiring users to create resources outside without module help