DEV Community

Cover image for Ease your .tf debugging using Terraform console
Leon Nunes
Leon Nunes

Posted on

Ease your .tf debugging using Terraform console

Terraform is fun, it gives you the ability to quickly write code to deploy your infrastructure, but I've always found debugging terraform loops to be a little bit hard, this could be because I like to see what the result will be before hand and sometimes that's just not possible.

So let's jump in!

To begin, go to your terraform code.

$ terraform console
Enter fullscreen mode Exit fullscreen mode

For starters we can view the data sent over from a data source

> data.terraform_remote_state.network.outputs.public_subnets
[
  "subnet-03d91cba453ff08b1",
  "subnet-07f430834d2138353",
  "subnet-096e710748bab5eff",
]
Enter fullscreen mode Exit fullscreen mode

Now if you want to create a loop out of something you can do

> [for a in data.terraform_remote_state.network.outputs.public_subnets:upper(a)]

[
  "SUBNET-04D91CBA353F108B1",
  "SUBNET-07F030831D2358353",
  "SUBNET-096E710758CAB7EFF",
]
Enter fullscreen mode Exit fullscreen mode

One can also create structures to check the output for example

> {"${module.vpc.vpc_id}":[{"public":"${module.vpc.public_subnets}","private":"${module.vpc.private_subnets}"}]}
{
  "vpc-03cf041fb057a54f7" = [
    {
      "private" = [
        "subnet-07fcca0e214a4a439",
        "subnet-056496be722ef56d2",
        "subnet-04755007e91fe1e20",
      ]
      "public" = [
        "subnet-04d92cba4432f08b1",
        "subnet-07f030534d2c58353",
        "subnet-096e710648cab7eff",
      ]
    },
  ]
}
Enter fullscreen mode Exit fullscreen mode

That's it, you can also loop over data.

Some things such as accessing outputs is still not supported.

If you liked this article I've also written one on structuring Terraform code.

For more information, read the docs and follow the guide here

I can also be found on Twitter, Linkedin.

Thank you for reading!

Top comments (0)