How to fix your automation broken by Terraform output formatting change in 0.14
Kevin LARQUEMIN
Posted on December 16, 2020
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"
}
In Terraform 0.13.5
Λ\: terraform output kubernetes_cluster_name
dev-cluster
In Terraform 0.14.2
Λ\: terraform output kubernetes_cluster_name
"dev-cluster"
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}
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 '-'.
The upgrade guide give some information :
We consider the console output of Terraform human readable;
[...]
If you rely onterraform output
in automation, please useterraform output -json
In my test, I can't see any difference just with -json
.
Λ\: terraform output -json kubernetes_cluster_name
"dev-cluster"
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
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
But this will delete every "
from the output, not only the surround one.
I will prefer using jq
to digest a json input.
Posted on December 16, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024