---
tags:
- devops
- l1
- flashcard-deck
- terraform-deep-dive
---
<!-- wiki:breadcrumb:start -->
[Portal](../../../../library/portal/index.md) | **Level:** [L1: Foundations](../../../../library/portal/levels.md) | **Topics:** [Terraform Deep Dive](../../../../library/portal/topics.md) | **Domain:** DevOps & Tooling
<!-- wiki:breadcrumb:end -->

id	category	difficulty	tags	question	answer	source_path
terraform-modules/0051643c15ad	terraform-modules	medium	terraform, input-output, composition	Aside from .tfvars files or CLI arguments, how can you inject dependencies from other modules?	The built-in terraform way would be to use `remote-state` to lookup the outputs from other modules.\n It is also common in the community to use a tool called `terragrunt` to explicitly inject variables between modules.\n\nRemember: "Module = reusable Terraform package." It has inputs (variables), outputs, and resources.	projects/knowledge/interview/terraform/069-aside-from-tfvars-files-or-cli-arguments-how-can-y.txt
terraform-modules/14210042b804	terraform-modules	medium	terraform, source, versioning, registry	How do you use a Terraform module in your configuration?	"The general syntax is:\n\n```hcl\nmodule ""<MODULE_NAME>"" {\n  source  = ""<MODULE_SOURCE>""\n  version = "">= 1.2.0, < 2.0.0""\n\n  # module inputs\n}\n```\n\nThe critical part is the source which you use to tell Terraform where the module can be found. When the module comes from the registry, you can pin the `version`; for local paths omit that attribute."	projects/knowledge/interview/terraform/144-how-to-use-a-module.txt
terraform-modules/17acd997ede6	terraform-modules	hard	terraform, source, versioning, composition	Discuss the use of Terraform modules for code reusability and composability.	"Terraform modules are reusable and composable components that encapsulate infrastructure configurations. They allow users to define and version a set of resources, making it easy to share, reuse, and maintain infrastructure code. Modules can represent higher-level abstractions or specific functionalities, enhancing code organization. For example, a ""VPC module"" could encapsulate networking configurations. Modules can be composed to create complex infrastructure by referencing them in other Terraform configurations, promoting modularity, maintainability, and consistency across projects."	projects/knowledge/interview/terraform/223-discuss-the-use-of-terraform-modules-for-code-reus.txt
terraform-modules/2e1494732fa4	terraform-modules	hard	terraform, composition, best-practices, source	You noticed there are relative paths in some of your modules and you would like to change that. What can you do and why is that a problem in the first place?	Relative paths usually work fine in your own environment as you are familiar with the layout and paths used, but when sharing a module and making it reusable, you may bump into issues as it runs on different environments where the relative paths may no longer be relevant.\n\nA better approach would be to use `path reference` like one of the following:\n\n- `path.module`: the path of the module where the expression is used\n- `path.cwd`: the path of the current working directory\n- `path.root`: the path of the root module	projects/knowledge/interview/terraform/143-you-noticed-there-are-relative-paths-in-some-of-yo.txt
terraform-modules/4496657c5ca4	terraform-modules	medium	terraform, composition, source	You are trying to use templatefile as part of a module and you use a relative path to load a file but sometimes it fails, especially when others try to reuse the module. How can you deal with that?	Switch relative paths with what is known as path references. These are fixes: paths like module root path, module expression file path, etc.\n\nRemember: "Module sources: local (./), registry, GitHub, S3, GCS." Pin versions for registry modules.	projects/knowledge/interview/terraform/126-you-are-trying-to-use-templatefile-as-part-of-a-mo.txt
terraform-modules/46b5752ee6bd	terraform-modules	medium	terraform, source, composition	How to use a module with for_each to create networks per availability zone?	"```hcl\nmodule ""subnet"" {\n  source   = ""../modules/subnet""\n  for_each = toset(var.azs)\n\n  name = ""subnet-${each.key}""\n  az   = each.value\n}\n```\n\n`for_each` keeps addresses stable and lets you add or remove AZs without re-creating the entire module."	projects/knowledge/interview/terraform/150-how-to-use-a-module-with-foreach-to-create-network.txt
terraform-modules/48f08a5e03f0	terraform-modules	medium	terraform, source, versioning	What should be done every time you modify the source parameter of a module?	Run `terraform init -upgrade` (or `terraform get -update` on older versions) so Terraform downloads the new version of the module and updates the `.terraform/modules` directory.\n\nRemember: "Module outputs expose inner values to the caller." Access with module.NAME.OUTPUT.	projects/knowledge/interview/terraform/146-what-should-be-done-every-time-you-modify-the-sour.txt
terraform-modules/4c8f12a5bb73	terraform-modules	easy	terraform, source, composition	What is the difference between Terraform modules and resources?	In Terraform, resources represent individual infrastructure components (e.g., virtual machines, networks), while modules are a way to encapsulate and reuse groups of resources. Modules allow you to organize and abstract portions of your Terraform configuration for better maintainability and reusability. Resources are the building blocks, and modules are the higher-level constructs that promote modularization and shareability of infrastructure code.	projects/knowledge/interview/terraform/206-what-is-the-difference-between-terraform-modules-a.txt
terraform-modules/508f6445e181	terraform-modules	medium	terraform, source, versioning, composition	"Discuss the importance of ""Terraform Modules"" in promoting code reuse and maintainability."	Terraform Modules are crucial for:\n* Code Reusability: Encapsulating and reusing configurations for different components.\n* Maintainability: Simplifying maintenance by isolating functionality within modules.\n* Abstraction: Providing an abstraction layer for hiding implementation details.\n* Consistency: Ensuring consistency in resource configurations across environments.\n* Collaboration: Facilitating collaboration by sharing and versioning modular code.	projects/knowledge/interview/terraform/264-discuss-the-importance-of-terraform-modules-in-pro.txt
terraform-modules/5ba9a511779b	terraform-modules	medium	terraform, source, versioning, registry	How do you consume a versioned module from a VCS or the Terraform Registry?	"```hcl\nmodule ""vpc"" {\n source = ""git::https://github.com/org/infra-modules.git//vpc?ref=v1.2.3""\n cidr = ""10.0.0.0/16""\n azs = [""us-east-1a"", ""us-east-1b""]\n}\n```\n\n- For registry modules, use `version` constraints (for example `version = "">= 1.2.0, < 2.0.0""`). \n- Always pin module versions to keep plans reproducible and review upstream releases before upgrading. \n- Prefer tags or immutable SHAs for VCS sources to guarantee repeatable builds."	projects/knowledge/interview/terraform/135-how-do-you-consume-a-versioned-module-from-a-vcs-o.txt
terraform-modules/6155bda7d6f6	terraform-modules	easy	terraform, source, versioning, composition	What is a recommended layout for a reusable module?	```text\nmodules/vpc/\n main.tf\n variables.tf\n outputs.tf\n versions.tf\n README.md\n examples/\n basic/\n main.tf\n```\n\n- `main.tf` defines resources, data sources, and locals. \n- `variables.tf` contains typed inputs with descriptions and validations. \n- `outputs.tf` exposes the minimal set of outputs downstream stacks need. \n- `versions.tf` pins compatible Terraform and provider versions. \n- `examples/` hosts runnable samples for documentation and testing, usually referenced by CI.	projects/knowledge/interview/terraform/134-what-is-a-recommended-layout-for-a-reusable-module.txt
terraform-modules/688a51666a62	terraform-modules	medium	terraform, source, versioning, input-output	How do modules handle inputs, outputs, locals and validation effectively?	- Define typed variables with defaults and `validation` blocks to guard against invalid CIDRs, AZ counts, or empty strings. \n- Use `locals` to transform data or derive names, and combine with `for_each`/`count` for deterministic resource creation. \n- Document outputs in `outputs.tf`, mark sensitive ones, and keep outputs minimal to reduce coupling. \n- Surface module metadata (like version or tags) through outputs for downstream modules or automation.	projects/knowledge/interview/terraform/136-how-do-modules-handle-inputs-outputs-locals-and-va.txt
terraform-modules/77fcda4e39e6	terraform-modules	hard	terraform, versioning, composition, input-output	Which module anti-patterns should you avoid?	"- Embedding provider blocks inside the module which makes reuse across accounts harder. \n- Accepting overly generic `map(any)` inputs that hide required structure instead of typed objects. \n- Outputting secrets or credentials without `sensitive = true` or secret stores. \n- Creating ""god"" root modules that mix networking, compute, and application concerns instead of composing smaller modules. \n- Copying and pasting modules without versioning, documentation, or automated tests."	projects/knowledge/interview/terraform/137-which-module-anti-patterns-should-you-avoid.txt
terraform-modules/8aa63a82c5a0	terraform-modules	hard	terraform, versioning, composition	What are the considerations for versioning Terraform modules, and how do you approach it?	Considerations for versioning Terraform modules include:\n* Semantic Versioning: Follow SemVer for clear versioning conventions.\n* Module Stability: Version modules based on stability and backward compatibility.\n* Release Notes: Provide release notes to communicate changes between versions.\n* Dependency Management: Clearly define dependencies and version constraints.\n* Module Tagging: Tag Git or other VCS repositories with version information.\n* Changelog Maintenance: Keep a well-maintained changelog for transparency.	projects/knowledge/interview/terraform/270-what-are-the-considerations-for-versioning-terrafo.txt
terraform-modules/973e02496a52	terraform-modules	medium	terraform, source, input-output, composition	There is a module to create a compute instance. How would you use the module to create three separate instances?	"starting with Terraform 0.13, the `count` meta-argument can be used with modules. So you could use something like this:\n\n```\nmodule ""instances"" {\n  source = ""/some/module/path""\n\n  count = 3\n}\n```\n\nYou can also use it in outputs vars: `value = module.instances[*]`"\n\nRemember: "Keep modules small and focused." One module per concern: networking, compute, database.	projects/knowledge/interview/terraform/149-there-is-a-module-to-create-a-compute-instance-how.txt
terraform-modules/97b010c28398	terraform-modules	medium	terraform, source, versioning, registry	How do you manage and version control Terraform modules?	Managing and version controlling Terraform modules involves organizing them as separate directories or repositories. Best practices include:\n* Repository per Module: Create a Git repository for each module.\n* Semantic Versioning: Follow semantic versioning for module releases.\n* Use Version Tags: Tag module releases for version control.\n* Module Registry: Utilize Terraform Registry or private module registries for discoverability.\n* Module Documentation: Include documentation with examples and usage guidelines.\n* Dependencies: Declare module dependencies clearly in the module documentation.	projects/knowledge/interview/terraform/235-how-do-you-manage-and-version-control-terraform-mo.txt
terraform-modules/a20dd9121c57	terraform-modules	medium	terraform, modules, syntax	How do you use a module in Terraform?	"By calling it in your configuration with a module block, specifying the source (path, registry, or git URL) and passing input variables. For example:\n\n```hcl\nmodule ""vpc"" {\n  source = ""terraform-aws-modules/vpc/aws""\n  version = ""3.5.0""\n  cidr = ""10.0.0.0/16""\n  # ... other inputs\n}\n```"	projects/knowledge/interview/terraform/281-use-module.txt
terraform-modules/c20e4c4c2610	terraform-modules	hard	terraform, composition	"Discuss the use of ""Terraform Modules"" for managing complex infrastructure deployments."	Terraform modules are reusable and shareable components that encapsulate infrastructure configurations. They are beneficial for managing complex deployments by:\n* Abstraction: Providing a level of abstraction for infrastructure components.\n* Reusability: Enabling reuse across different projects and environments.\n* Consistency: Ensuring consistent configurations across deployments.\n* Maintainability: Simplifying updates and changes to infrastructure.\n* Scalability: Facilitating the scaling of deployments in a modular fashion.	projects/knowledge/interview/terraform/242-discuss-the-use-of-terraform-modules-for-managing-.txt
terraform-modules/c97a29bfd2be	terraform-modules	easy	terraform, input-output	How to access module output variables?	The general syntax is `module.<MODULE_NAME>.<OUTPUT_VAR_NAME>`\n\nRemember: "Publishing to the Terraform Registry requires: GitHub repo, semantic version tags, and standard module structure."	projects/knowledge/interview/terraform/147-how-to-access-module-output-variables.txt
terraform-modules/ca9731cbfabf	terraform-modules	medium	terraform, source, versioning, composition	When should you use a module instead of inline resources?	- You need to reuse infrastructure patterns across environments, teams, or regions without copy-paste. \n- You want to codify best practices (naming, tagging, security controls) behind a stable interface. \n- You need to version and promote infrastructure changes using semantic releases and CI pipelines. \n- You want to limit blast radius by updating one module and rolling it out gradually.	projects/knowledge/interview/terraform/133-when-should-you-use-a-module-instead-of-inline-res.txt
terraform-modules/d79454eb90b8	terraform-modules	medium	terraform, source, versioning, composition	What are Terraform modules and how do they promote reuse?	"[Terraform.io](https://www.terraform.io/language/modules/develop): ""A module is a container for multiple resources that are used together. Modules can be used to create lightweight abstractions, so that you can describe your infrastructure in terms of its architecture, rather than directly in terms of physical objects.""\n\nIn addition, modules are great for creating reusable Terraform code that can be shared and used not only between different repositories but even within the same repo, between different environments (like staging and production). Well-designed modules also expose a small, opinionated surface and hide implementation details."	projects/knowledge/interview/terraform/131-explain-modules.txt
terraform-modules/df4831a57024	terraform-modules	medium	terraform, source, composition	Assuming you have multiple regions configured and you would like to use a module in one of them. How to achieve that?	"```\nmodule ""some_module"" {\n  source = ""...""\n\n  providers = {\n    aws = aws.some_region\n  }\n\n  ...\n}\n```"\n\nGotcha: Nested module calls create deep dependency chains. Keep nesting to 2 levels max for readability.	projects/knowledge/interview/terraform/157-assuming-you-have-multiple-regions-configured-and-.txt
terraform-modules/e0e4b78f5806	terraform-modules	easy	modules, reusability, organization	What are Terraform modules and how do you use them?	"Modules are reusable, self-contained packages of Terraform configurations.\nThey're the primary way to organize and reuse infrastructure code.\n\nStructure:\n```\nmodule/\n├── main.tf      # Resources\n├── variables.tf # Input variables\n├── outputs.tf   # Output values\n└── README.md    # Documentation\n```\n\nUsing a module:\n```hcl\nmodule ""vpc"" {\n  source  = ""./modules/vpc""  # Local path\n  # Or: source = ""terraform-aws-modules/vpc/aws""  # Registry\n\n  vpc_cidr    = ""10.0.0.0/16""\n  environment = ""production""\n}"	projects/knowledge/interview/terraform/003-modules.txt
terraform-modules/e428ccaf4ead	terraform-modules	easy	terraform, source	You would like to load and render a file from module directory. How to do that?	`templatefile("${path.module}/user-data.sh", { name = var.name })` loads and renders a template from the module directory. `${path.module}` resolves to the filesystem path of the module where the expression is defined, not the calling module. This ensures the template path works regardless of where the module is called from. Variables are passed as the second argument map.	projects/knowledge/interview/terraform/148-you-would-like-to-load-and-render-a-file-from-modu.txt
terraform-modules/e4cbbcef02f0	terraform-modules	easy	terraform, source	True or False? Module source can be only local path	"False. It can be a Git URL, HTTP URL, ... for example:\n\n```\nmodule ""some_module"" {\n\n  source = ""github.com/foo/modules/bar?ref=v0.1""\n}\n```"\n\nRemember: "Module versioning: always pin versions in production." Without version pinning, terraform init may pull breaking changes.	projects/knowledge/interview/terraform/141-true-or-false-module-source-can-be-only-local-path.txt
terraform-modules/f26e05043e2b	terraform-modules	medium	terraform, source	When creating a module, do you prefer to use inline blocks, separate resources or both? why?	No right or wrong here. \nPersonally, I prefer to use only separate resources in modules as it makes modules more flexible. So if a resource includes inline blocks, that may limit you at some point.	projects/knowledge/interview/terraform/140-when-creating-a-module-do-you-prefer-to-use-inline.txt
terraform-modules/f3523a919050	terraform-modules	medium	terraform, source, registry	Where can you obtain Terraform modules?	Terraform modules can be found at the [Terraform registry](https://registry.terraform.io/browse/modules)\n\nRemember: "Modules don't inherit provider configuration." Pass providers explicitly or let them use the default.	projects/knowledge/interview/terraform/142-where-can-you-obtain-terraform-modules.txt
terraform-modules/f5a3a1c55b38	terraform-modules	hard	terraform, versioning, composition, input-output	How can you manage dependencies between Terraform modules in a complex infrastructure setup?	Managing module dependencies involves:\n* Input Variables: Passing output variables of one module as input variables to another.\n* Explicit Dependency Declaration: Clearly defining dependencies using Terraform syntax.\n* Module Composition: Composing modules hierarchically to reflect dependencies.\n* Terraform Remote State: Using remote state files to share outputs between modules.\n* Versioned Modules: Ensuring that module versions are specified to maintain stability.	projects/knowledge/interview/terraform/265-how-can-you-manage-dependencies-between-terraform-.txt

<!-- wiki:related:start -->
---

## Wiki Navigation

### Related Content

- [Runbook: Terraform Drift Detection Response](../../../../library/runbooks/cloud-terraform/drift-detection.md) (Runbook, L2) — Terraform Deep Dive
- [Terraform Deep Dive](../../../../library/topics/terraform-deep-dive/index.md) (Topic Pack, L2) — Terraform Deep Dive
- Terraform Providers Flashcards *(CLI)* (flashcard_deck, L1) — Terraform Deep Dive
- Terraform State Flashcards *(CLI)* (flashcard_deck, L1) — Terraform Deep Dive
- Terraform Workflow Flashcards *(CLI)* (flashcard_deck, L1) — Terraform Deep Dive

<!-- wiki:related:end -->
