Terraform Debug Printing


If you have ever had a value in Terraform that you didn’t quite know the shape of and get an error such as:

This object does not have an attribute named "primary-ip".

You were probably frustrated to find out that Terraform doesn’t have a print() function.

Bugs have been closed with no resolution for this basic debugging essential. So I set out to find an “exploit” in Terraform that would let us see the value of a variable. My first thought, and what ended up being quite successful, was to find a function that would show us the value on error.

After only a couple of tries I found that the file() function will print paths that it can not open. Unfortunately this doesn’t help if your value isn’t a string, so I paired it with yamlencode() to turn (most) values into a string.

And there we have it. Wrap file(yamlencode(...)) around the value you want to see and hope that the file doesn’t exist.

locals {
	foo = file(yamlencode({a=1, b=2}))
Invalid value for "path" parameter: a: 1
b: 2

It isn’t super pretty but it works. Let me know if you find something that works for a wider range of values, for my purposes yamlencode() was sufficient.