DEV Community

Cover image for How to fix your automation broken by Terraform output formatting change in 0.14
Kevin LARQUEMIN for Stack Labs

Posted on

How to fix your automation broken by Terraform output formatting change in 0.14

If you move to Terraform 0.14 from previous version and you are a user of the terraform output feature in your CI. You probably will be surprised because working automation isn't anymore.

In Terraform 0.14, the formatting of terraform output is modified to match the formatting of terraform show. Like it's explained in the upgrade guide.

For example, to get the name of a kubernetes cluster in Gke

output "kubernetes_cluster_name" {
  value       = google_container_cluster.primary.name
  description = "GKE Cluster Name"
}
Enter fullscreen mode Exit fullscreen mode

In Terraform 0.13.5

Λ\: terraform output kubernetes_cluster_name
dev-cluster
Enter fullscreen mode Exit fullscreen mode

In Terraform 0.14.2

Λ\: terraform output kubernetes_cluster_name
"dev-cluster"
Enter fullscreen mode Exit fullscreen mode

Not a big difference, but quoted and unquoted string aren't the same value in automation.

For example, a shell script

CLUSTER_NAME=`terraform output kubernetes_cluster_name`
gcloud config configurations create ${CLUSTER_NAME}
gcloud auth activate-service-account ...
gcloud container clusters get-credentials ... ${CLUSTER_NAME}
Enter fullscreen mode Exit fullscreen mode

And of course, quoted string in an error message isn't as anormal as it is in this case ^^

ERROR: (gcloud.config.configurations.create) Invalid name ["dev-cluster"] for a configuration.  Except for special cases (NONE), configuration names start with a lower case letter and contain only lower case letters a-z, digits 0-9, and hyphens '-'.
Enter fullscreen mode Exit fullscreen mode

The upgrade guide give some information :

We consider the console output of Terraform human readable;
[...]
If you rely on terraform output in automation, please use terraform output -json

In my test, I can't see any difference just with -json.

Λ\: terraform output -json kubernetes_cluster_name
"dev-cluster"
Enter fullscreen mode Exit fullscreen mode

The answer to how to use it in automation and get unquoted value is more deeper in the documentation of the output command.

Λ\: terraform output -json kubernetes_cluster_name | jq -r .
dev-cluster
Enter fullscreen mode Exit fullscreen mode

And here we are!

Thanks jq to be there 😁

We can find some other alternative, like using tr -d '"'

terraform output -json kubernetes_cluster_name | tr -d '"'
dev-cluster
Enter fullscreen mode Exit fullscreen mode

But this will delete every " from the output, not only the surround one.

I will prefer using jq to digest a json input.

Top comments (0)