Which THREE of the following are components of the 'source' string when using the Terraform Registry?
The provider specifies the target cloud (e.g., aws).
Why this answer
The registry source string format is 'namespace/name/provider'.
33 questions · Terraform Modules · All types, answers revealed
Which THREE of the following are components of the 'source' string when using the Terraform Registry?
The provider specifies the target cloud (e.g., aws).
Why this answer
The registry source string format is 'namespace/name/provider'.
What is the recommended approach for managing a module that has grown too large?
Modularization of the module itself is the correct design pattern.
Why this answer
Refactoring into smaller, single-purpose sub-modules improves maintainability and readability.
Where do module outputs appear after a 'terraform apply'?
Outputs are part of the state and printed upon successful application.
Why this answer
Outputs are displayed in the CLI output and stored in the state file.
Which argument in a module block allows you to provide a custom name to the module instance?
The identifier after 'module' serves as the instance name for referencing.
Why this answer
The module block itself uses the label to name the instance, but the 'source' argument defines the location.
Which TWO of the following are true regarding variable validation in modules?
The condition must evaluate to true for the input to be valid.
Why this answer
Validation blocks and type constraints are the primary mechanisms for ensuring valid inputs.
You are building a module that needs to be consumed by multiple teams. How should you structure your module to ensure that the root module can provide values for variable definitions without hardcoding them in the module itself?
Variables defined in the module's variables.tf file can be populated by the root module.
Why this answer
Using input variables allows the module to be generic and reusable by accepting values defined in the calling configuration.
Which directory structure is considered standard for a Terraform module?
This is the established convention for module organization.
Why this answer
A standard module should contain at least main.tf, variables.tf, and outputs.tf.
When refactoring a large infrastructure configuration, you move a resource into a child module. How do you handle the existing state to prevent Terraform from destroying and recreating the resource?
This command updates the state file to reflect the new hierarchy without destroying infrastructure.
Why this answer
The 'terraform state mv' command is required to map the old address to the new module-relative address.
What is the primary benefit of using a module registry compared to a local file path for module distribution?
Registries facilitate versioned releases and standardized consumption patterns.
Why this answer
Registries provide versioning, documentation generation, and centralized discovery for teams.
Which THREE actions are best practices when publishing a module to a private registry?
SemVer allows consumers to pin to safe versions.
Why this answer
Best practices include versioning, documentation, and specific naming conventions.
Which THREE of the following are valid sources for a Terraform module?
The Registry is the primary source for published modules.
Why this answer
Terraform supports modules from local paths, the Terraform Registry, and version control providers like Git.
Which THREE tasks are performed by 'terraform init -upgrade'?
It fetches the latest provider versions.
Why this answer
The '-upgrade' flag forces the re-download of modules and providers to the latest versions within constraints.
What is the purpose of the 'terraform init' command when working with modules?
Module initialization is a core function of the init command.
Why this answer
'terraform init' downloads and installs the modules specified in the configuration into the '.terraform/modules' directory.
Which THREE of these are benefits of using modules in Terraform?
Modules ensure consistent resource setup.
Why this answer
Modules improve code reuse, organization, and standardization.
Which THREE items should be included in a well-documented Terraform module?
Output documentation clarifies what data is exposed.
Why this answer
Documentation should cover how to use the module, its inputs, and its outputs.
If a module has a required variable but you do not provide it in the calling configuration, what happens?
Missing required variables cause immediate validation errors.
Why this answer
Terraform will stop and prompt the user for the missing value during the plan or apply phase if it is not provided via command line or tfvars.
Which TWO of the following are valid ways to pass a provider configuration to a child module?
Explicit mapping is needed for non-default provider instances.
Why this answer
Providers are either inherited from the root or passed explicitly using the 'providers' map in the module block.
If you have a module that creates an S3 bucket, and you want the bucket name to be prefixed by a value provided by the caller, how do you achieve this?
Interpolating the input variable into the bucket name is the standard approach.
Why this answer
Concatenate the variable inside the resource block using string interpolation.
Your team requires a custom module to be versioned strictly to prevent breaking changes during deployments. What is the recommended way to reference a module from a private Git repository?
The 'ref' query parameter in the source URL specifies the version, tag, or branch.
Why this answer
Using the 'version' argument in the source block allows pinning to specific tags or branches, ensuring stability.
How can you protect a module's inputs from receiving incorrect types of data?
Validation blocks provide a way to enforce custom business logic on inputs.
Why this answer
Defining the 'type' constraint in the variable block ensures Terraform validates the input before applying changes.
You need to output a complex object from a child module to be used by the root module. What is the best practice for defining the output?
This is the standard pattern for passing information from a module back to its caller.
Why this answer
Using explicit output blocks in the child module makes the data available for access via 'module.<module_name>.<output_name>'.
You need to share a module across your entire organization. Which registry type should you use?
Private registries are designed for secure, internal module distribution.
Why this answer
Terraform Cloud or Terraform Enterprise private module registries are the standard for internal organization-wide sharing.
You have a nested module structure. How can you pass a variable from the root module through an intermediate module to a leaf module?
Explicit declaration and assignment are required at every level of the hierarchy.
Why this answer
Each intermediate module must define a variable and then pass it down using the 'source' or 'module' call block.
You are designing a module that should conditionally create resources based on a boolean input. What feature should you use?
Setting 'count = var.enabled ? 1 : 0' is the standard pattern for conditional resource creation.
Why this answer
The 'count' meta-argument is commonly used to toggle resource creation based on a boolean value.
You are using a module that depends on a specific provider version. Where should this constraint be defined?
This guarantees that the module's requirements are met during execution.
Why this answer
The 'required_providers' block within the 'terraform' block of the module is the correct location to pin versions.
You want to use a module that is hosted in a private GitHub repository. Which authentication method should Terraform use to access it?
Terraform leverages the underlying git command, so existing authenticated git configurations work.
Why this answer
Terraform uses the credentials configured in the local environment (e.g., ~/.git-credentials or SSH keys) to authenticate with private repositories.
Which TWO of the following are true about the '.terraform' directory?
Downloaded modules are stored here.
Why this answer
The '.terraform' directory is used locally by Terraform to store module files and provider plugins.
When should you use the 'for_each' meta-argument within a module?
'for_each' is specifically designed for creating multiple module instances.
Why this answer
'for_each' is used to create multiple instances of a module based on a map or set of strings.
What command do you run to view the documentation for a module that has been published to a registry?
Registry documentation is provided via the web interface.
Why this answer
The registry automatically renders documentation in the UI; no CLI command is required to 'view' the documentation directly.
Which TWO meta-arguments can be used within a module block?
'count' is a valid meta-argument for modules.
Why this answer
'count' and 'for_each' are the two meta-arguments available for module blocks.
Why would you use a 'locals' block within a module instead of passing everything through variables?
Locals clean up repetitive code and keep the module interface simple.
Why this answer
'locals' are ideal for transforming inputs, combining data, or creating complex naming conventions that the user shouldn't have to define.
What is the impact of using 'sensitive = true' in a module output?
This is the main purpose of the 'sensitive' flag.
Why this answer
It marks the output as sensitive, preventing it from being printed to the console during 'terraform apply' or 'plan'.
When a module is updated, how do you ensure that all environments using this module get the update?
The '-upgrade' flag is required to override the existing locked version.
Why this answer
You must update the version constraint (if pinned) and run 'terraform init -upgrade' to fetch the new version.
Ready to test yourself?
Try a timed practice session using only Terraform Modules questions.