Troubleshooting ERROR: stack module not found

First, really enjoying Terraspace. I’ve used Terraform for a few years, Terragrunt for a year in production, and I’m now using Terraspace for a personal project (and it’s fantastic). That all said, the error…

While making some revisions an AWS Launch Configuration that pulls the Key Name from a Key resource via the output helper, I ran into the following:

> terraspace all plan
Building one stack to build all stacks
Building .terraspace-cache/us-east-1/dev/stacks/my_thing
Downloading tfstate files for dependencies defined in tfvars...
ERROR: stack module not found
> 

And that was it. No context, description, or file reference, just “ERROR: stack module not found”. I also didn’t see any logs to indicate where the issue may be. As this was a few dozen lines across a half dozen files, it wasn’t obvious what the issue was. Poking around, I found that my output helper reference was malformed. I rewrote the reference and everything was good in the world again.

Enhancement: Is it possible to get more context around this error when it shows up? Thanks!

Interesting, thanks for the report. When you get a chance, wondering what the malformed output code looked like.

Dug into it a little bit and here’s an example project: https://github.com/tongueroo/infra-malform-output/tree/febd031b88d8208baae9bf59107ff8985da1e0e2

The code has an intentional error:

app/stacks/a1/tfvars/base.tfvars

length1 = <= output("b1.length") %>

It should be <%= not <=

Debugging Session

First run terraspace all up

$ terraspace all up
Building one stack to build all stacks
Building .terraspace-cache/us-west-2/dev/stacks/b1
Built in .terraspace-cache/us-west-2/dev/stacks/b1
Will run:
    terraspace up a1 # batch 1
    terraspace up b1 # batch 1
Are you sure? (y/N) y
Batch Run 1:
Running: terraspace up a1 Logs: log/up/a1.log
Running: terraspace up b1 Logs: log/up/b1.log
terraspace up a1:  Error: Invalid expression
terraspace up b1:  No changes. Infrastructure is up-to-date.
terraspace up b1:  Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Error running: terraspace up a1. Check logs and fix the error.
$

Then run terraspace logs up a1

$ terraspace logs up a1
Showing: log/up/a1.log
Current directory: .terraspace-cache/us-west-2/dev/stacks/a1
=> terraform plan -input=false -out /tmp/terraspace/plans/a1-9893a57ae00b26226a1af20b7f77ccec.plan

Error: Invalid expression

  on 1-base.auto.tfvars line 1:
   1: length1 = <= output("b1.length") %>

Expected the start of an expression, but found an invalid expression token.

Error running command: terraform plan -input=false -out /tmp/terraspace/plans/a1-9893a57ae00b26226a1af20b7f77ccec.plan
$

Curious to see the errors you were seeing though. No sweat either way. Thanks.

Hi Tung!

Thanks for the follow up, I’m sorry I didn’t provide the source to it. The output helper reference, using your example, would’ve been:

length1 = <%= output("module.b1.length") %>

Which is where the original failure message of ERROR: stack module not found came from. The error message made sense once I tracked it down, but because of my typo, it was challenging to track down.

Interesting, so updated the example with a stack that does not exist. https://github.com/tongueroo/infra-malform-output/tree/4a3fd61f28c190bde843be9f75cdd5b0878b205f

The code has an intentional error:

app/stacks/a1/tfvars/base.tfvars

length1 = <= output("makebelievestack.length") %>

The makebelievestack does not exist. Only the a1 and b1 stacks exist.

$ terraspace list
app/stacks/a1
app/stacks/b1
$

Debugging Session

$ terraspace all up -y
Building one stack to build all stacks
Building .terraspace-cache/us-west-2/dev/stacks/b1
WARN: The makebelievestack stack does not exist
Here's the line in app/stacks/a1/tfvars/base.tfvars with the error:

1 length1 = <%= output("makebelievestack.length") %>
ERROR: stack makebelievestack not found
$

The terraspace all provides some debugging info in output, at least in the example that have put together. Wondering if you’re seeing that. If not, wondering if there’s an edge case. :face_with_monocle:

I see! So it’s when the output doesn’t exist. Repo: https://github.com/tongueroo/infra-malform-output/tree/3463f35e968c026089c715020a004bbcce095a92

The code has an intentional error:

app/stacks/a1/tfvars/base.tfvars

length1 = <= output("b1.makebelieveoutput") %>

The makebelieveoutput does not exist. Only the pet and length outputs exist.

$ cat app/stacks/b1/outputs.tf
output "pet" {
  description = "pet"
  value       = random_pet.this.id
}
output "length" {
  description = "length"
  value       = 2
}
$

Debugging Session

$ terraspace all up -y
Building one stack to build all stacks
Building .terraspace-cache/us-west-2/dev/stacks/b1
Downloading tfstate files for dependencies defined in tfvars...
Built in .terraspace-cache/us-west-2/dev/stacks/b1
Running:
    terraspace up b1 # batch 1
    terraspace up a1 # batch 2
Batch Run 1:
Running: terraspace up b1 Logs: log/up/b1.log
terraspace up b1:  No changes. Infrastructure is up-to-date.
terraspace up b1:  Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Batch Run 2:
Running: terraspace up a1 Logs: log/up/a1.log
terraspace up a1:  Error: Invalid value for input variable
Error running: terraspace up a1. Check logs and fix the error.
$ cat .terraspace-cache/us-west-2/dev/stacks/a1/1-base.auto.tfvars
length1 = "(Output makebelieveoutput was not found for the a1 tfvars file. Either b1 stack has not been deployed yet or it does not have this output: makebelieveoutput)"
$

It’s a little bit trickier to surface a better debugging error message for this type of error. Mainly because the outputs are lazily calculated later in the process rather than where the original caller line context is available. May have to save the original caller line in memory and track it. Think the complexity is worth it for a better developer experience. Unsure when will get to this one, though. Will consider PRs. Of course, no sweat either way. Thanks for the report. It’s helpful.