Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Terraform modules pass data through explicit inputs and outputs: a caller supplies named arguments to a child module, and the child exposes selected values for the caller to use. The caller can then pass an output to another module or a resource. A module’s source is separate: it tells Terraform where to find the module’s code, not what runtime data to pass.

How data flows between Terraform modules

A module’s variables and outputs form its interface with the configuration that calls it. The parent supplies inputs in a module block; the child returns only the values it explicitly exposes with output blocks. The parent connects modules by referring to one module’s output in another module’s input.

module "network" {
  source = "./modules/network"

  base_cidr_block = "10.0.0.0/8"
}

module "app" {
  source = "./modules/app"

  subnet_ids = module.network.subnet_ids
}

For this configuration to work, the network module must declare a base_cidr_block input and a subnet_ids output. The app module must declare a subnet_ids input. The parent configuration makes the connection between them.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Inputs: values the caller supplies

A child module declares its input variables with variable blocks. The calling configuration supplies values as arguments with matching names inside the child’s module block. The child can use those variables in resources, data sources, and expressions.

If a variable has a default, the caller may omit it. If it has no default, the caller must provide a value before Terraform can generate a plan. Types and validation can clarify what values a module expects, but the basic flow is caller argument to child variable.

Outputs: values the child exposes

A child module uses output blocks to make selected values available to its caller. The caller references one with module.<label>.<output-name>. For example, module.network.subnet_ids means the subnet_ids output from the module instance whose label in the caller is network.

The label is the local name assigned in the caller’s module block; it need not match the module’s registry name. Outputs commonly expose values such as IDs, names, or endpoints that another module or resource needs. A resource attribute inside a child is not automatically available outside it: the module author must expose it with an output.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What the module source controls

The source argument identifies where Terraform obtains a module’s configuration files. It does not pass a value into the module. Supported source kinds include local directories, registries, and version-control repositories.

  • Local module: A path such as ./modules/network points to a directory in the local source tree.
  • Registry module: A registry source can use a version constraint to select an allowed release.
  • Version-control module: A Git source can use ref to select a branch, tag, or commit.

The version argument applies to registry modules; a local module uses the version of the caller’s local repository. Terraform must know the source during initialization. Current module references allow constant expressions using input variables and local values; a variable used in source must declare const = true. A source cannot be selected from arbitrary runtime values.

After changing a source or a registry module version, run terraform init so Terraform can install the module code. For an already-installed module, terraform init -upgrade updates it to the newest version allowed by its configured constraint.

Compose modules through a common caller

A useful default is to place related modules under a common parent and connect them by passing outputs into inputs. HashiCorp calls this flat style “module composition”: multiple building-block modules are assembled to produce a larger system. A relatively flat module tree makes the connections visible to the caller and generally avoids the reuse and comprehension difficulties of excessive nesting.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When a module argument refers to an upstream module output or resource, Terraform can infer the dependency from that reference and order operations accordingly. Use depends_on when a dependency exists but is not expressed through an argument reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When values cross Terraform configurations

Ordinary child-module outputs are consumed within their calling module hierarchy using references such as module.network.subnet_ids. A separate Terraform configuration can instead read root-module outputs using terraform_remote_state. That is cross-configuration state access, not the usual way to connect sibling modules within one configuration.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.