DEV Community

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

Posted on

2 2

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!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay