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!

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

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

Okay