Terraform Use Module Output as Input for Another Module: Wiring, Type Errors and the for_each Trap (Terraform 1.17)
TL;DR
TL;DR
- Two steps: declare
output "instance_id"inside module A, then passmodule.ec2_module.instance_idas an argument of module B in the root module. Modules never talk to each other directly; the root module is the wire.- That one reference is also the dependency: Terraform creates A first and B second. You do not need
depends_on(Terraform docs reserve it for hidden dependencies).- The three errors you will meet:
Unsupported attribute(output not declared),Missing required argument(B needs the input), andstring required, but have tuple(wrong type, found byplan, not byvalidate).- Do not use an apply-time output as a
for_eachkey: it fails withInvalid for_each argument.- Only reading the output of a child inside the same root works like this. Not your case? See reading a module output and sensitive outputs or how Terraform modules work.
I wrote the first version of this post in 2023 with an EC2 example I never ran. This is the rewrite: a small lab you can reproduce offline with two local modules and the null and local providers, so there is no cloud account involved. Everything below ran with Terraform 1.17.1 (null 3.3.2, local 2.9.1) on 2026-11-02. If you are new to the output block itself, start with the output values post; the full list of posts is in the Terraform index.
How the wiring works
A module is a folder of .tf files. The calling module sees only what the child declares as outputs. The module block reference says that when the child exposes output values you reference them with module.<label>.<output>, and the outputs page says the same: a child module output is available to the parent as module.<CHILD_MODULE_NAME>.<OUTPUT_NAME>. On the receiving side, the module declares an ordinary input variable, and the root module sets it like any other argument.
| Piece | Where it lives | Syntax |
|---|---|---|
| Producer output | Inside module A | output "instance_id" { value = ... } |
| Reference | Root module, in the module "b" block | module.ec2_module.instance_id |
| Consumer input | Inside module B | variable "another_module_instance_id" { type = string } |
| Wrong: A talks to B | Inside module A | not possible, A cannot see module B |
| Wrong: reach a resource in A | Root module | module.ec2_module.null_resource.server does not exist |
Why does this matter beyond tidiness? The reference is how you keep modules reusable. Module B never knows module A exists; it only asks for a string. Tomorrow you can pass it an ID from a different module, a data source or a literal.
The lab
Module A, modules/ec2_module/main.tf, stands in for the EC2 module of the original post. A null_resource has an id that is generated at apply time, exactly like a cloud instance ID, which is the property that matters for this topic:
1variable "name" { type = string }
2
3resource "null_resource" "server" {
4 triggers = { name = var.name }
5}
6
7output "instance_id" {
8 value = null_resource.server.id
9}
10
11output "private_ips" {
12 value = ["10.0.1.10", "10.0.1.11"]
13}
Module B, modules/compute/main.tf, takes one string and writes a file with it:
1variable "another_module_instance_id" { type = string }
2
3resource "local_file" "attach" {
4 filename = "${path.root}/attached.txt"
5 content = "attached to ${var.another_module_instance_id}\n"
6}
7
8output "attached_file" { value = local_file.attach.filename }
And the root main.tf is the wire:
1module "ec2_module" {
2 source = "./modules/ec2_module"
3 name = "web-1"
4}
5
6module "compute" {
7 source = "./modules/compute"
8 another_module_instance_id = module.ec2_module.instance_id
9}
10
11output "attached" { value = module.compute.attached_file }
With a real cloud module the producer would output aws_instance.example.id and the pattern is identical. I ran the offline version only, so check the resource arguments against your provider's documentation.
Plan and apply
On 2026-11-02 with Terraform 1.17.1 the plan reported Plan: 2 to add, 0 to change, 0 to destroy. and showed the consumer's content as unknown, because the ID does not exist yet:
1 # module.compute.local_file.attach will be created
2 + resource "local_file" "attach" {
3 + content = (known after apply)
4 + directory_permission = "0777"
5 + file_permission = "0777"
6 + filename = "./attached.txt"
The apply then created the producer first and the consumer second, in that order:
1$ terraform apply -auto-approve
2module.ec2_module.null_resource.server: Creating...
3module.ec2_module.null_resource.server: Creation complete after 0s [id=6881768477812657768]
4module.compute.local_file.attach: Creating...
5module.compute.local_file.attach: Creation complete after 0s [id=9fb7d32e916cb4acdfd7ab6e803dec45ed47c48c]
6Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
7
8Outputs:
9
10attached = "./attached.txt"
11$ cat attached.txt
12attached to 6881768477812657768
The ID printed by the producer is the one that ended up in the consumer's file. terraform graph shows the one edge that the reference created, and nothing else:
1 "module.compute.local_file.attach" -> "module.ec2_module.null_resource.server";
That arrow reads "compute depends on ec2_module". I wrote no depends_on. The depends_on reference says to use it for hidden dependencies Terraform cannot infer, where a resource relies on another's behavior without reading its data. Here the data is read, so the reference is enough.
What broke when I ran this
I deliberately broke the wiring three ways, because these are the messages you will search for.
1. The output name does not exist. I changed the reference to module.ec2_module.instance. terraform validate on 2026-11-02 printed:
1Error: Unsupported attribute
2
3 on main.tf line 8, in module "compute":
4 8: another_module_instance_id = module.ec2_module.instance
5 ├────────────────
6 │ module.ec2_module is object with 2 attributes
7
8This object does not have an attribute named "instance".
"2 attributes" is the number of outputs module A declares (instance_id and private_ips). If you expected 3, you forgot to add an output in the child.
2. The consumer's input is not set. I deleted the another_module_instance_id line from the module "compute" block:
1Error: Missing required argument
2
3 on main.tf line 6, in module "compute":
4 6: module "compute" {
5
6The argument "another_module_instance_id" is required, but no definition was
7found.
A variable without a default is required, as the input variables page describes. That is the right behavior: the module tells you exactly what it needs.
3. The type is wrong, and validate does not catch it. I pointed the input at module.ec2_module.private_ips, a list, while module B declares type = string. The surprise: terraform validate printed Success! The configuration is valid. Only terraform plan failed:
1Error: Invalid value for input variable
2
3 on main.tf line 8, in module "compute":
4 8: another_module_instance_id = module.ec2_module.private_ips
5
6The given value is not suitable for
7module.compute.var.another_module_instance_id declared at
8modules/compute/main.tf:1,1-38: string required, but have tuple.
So in CI, run terraform plan, not only validate. Note the word "tuple": a list written literally in the child is a tuple until a type constraint converts it. To pass one element, use module.ec2_module.private_ips[0].
4. Using the output as a for_each key. I added a second consumer, for_each = toset([module.ec2_module.instance_id]), to a fresh state. validate was happy again; plan failed:
1Error: Invalid for_each argument
2
3 on main.tf line 15, in module "extra":
4 15: for_each = toset([module.ec2_module.instance_id])
5 ├────────────────
6 │ module.ec2_module.instance_id is a string, known only after apply
7
8The "for_each" set includes values derived from resource attributes that
9cannot be determined until apply, and so Terraform cannot determine the full
10set of keys that will identify the instances of this resource.
The for_each documentation says the values must be known before Terraform performs any remote resource operations, and that a unique ID generated by the remote API will result in an error. The fix is to key the loop on something you write statically (a name) and only pass the ID as the value, as the error message itself suggests. When I first ran this against a state where the ID already existed, the plan did not complain at all, so this error can hide until the first clean environment, which is why I test it from an empty state.
Changing the producer changes the consumer
I changed name from web-1 to web-2 in the root module. The triggers map of a null_resource forces replacement, and since the consumer holds the ID, it is replaced as well:
1 # module.compute.local_file.attach must be replaced
2-/+ resource "local_file" "attach" {
3 # module.ec2_module.null_resource.server must be replaced
4-/+ resource "null_resource" "server" {
5 ~ triggers = { # forces replacement
6Plan: 2 to add, 0 to change, 2 to destroy.
This is the cost of wiring by output: a replaced producer produces a new ID, and everything that consumed it is rebuilt or updated. With a real EC2 instance that can mean downtime for everything attached to the old ID, so read the plan summary line before you approve. For the counts of what Terraform will do, the Plan: line is the number to look at; here it said 2 to add and 2 to destroy for a single changed argument.
What it costs
Nothing, and almost no time. With the providers already installed, on 2026-11-02 a validate of the two-module lab took 0.35 s, a plan 0.41 s, a fresh apply 0.57 s and a destroy 0.65 s. After the apply the state file held 2 resources in 2,371 bytes. Every command finished in under 1 s, and both resources reported Creation complete after 0s. These are placeholder resources, so do not extrapolate to a real cloud stack; the point is that the module boundary and the output add no measurable overhead.
Checklist
| Symptom | Cause | Fix |
|---|---|---|
Unsupported attribute ... object with N attributes | Output missing or misspelled in the child | Declare or rename the output |
Missing required argument | Consumer variable has no default and is not set | Pass module.a.output in the module block |
string required, but have tuple | List output given to a string variable | Index it, [0], or change the variable type |
Invalid for_each argument | Key derived from an apply-time value | Use static keys, pass the ID as the map value |
| Consumer rebuilt on every change upstream | It depends on a replaced producer | Read the plan; split stable and volatile modules |
| Need a value from a different configuration | Outputs belong to one configuration | Read root outputs with remote state |
You can evaluate any of these references without applying by using the terraform console, and the errors above share their cause with Value for undeclared variable. If you loop over modules, for and for_each explains the rules in depth.
My recommendation
Keep module inputs small and typed: one string for an ID, not a whole object, so the consumer works with any producer. Give every output a description, name outputs for their meaning (instance_id), and run terraform plan in CI so type errors and unknown-key errors show up before apply. Do not add depends_on between modules out of habit; every unneeded one hides the real graph and can force extra replacements.
Read More - Terragrunt -
Did this save you a debugging session? Stay in the loop -
- Subscribe on YouTube - every post here has a screen-recorded companion video.
- Code on GitHub - the repositories, Terraform and scripts behind these posts.