Usage of Terraform to deploy resources in cloud is very common now a days. And normally we use CI-CD pipeline to automate this deployment process based on our requirements. In our team we use Jenkins and Terraform. Let’s walk you through a Terraform issue.
One of my fellow colleagues faced an issue while running a Jenkins pipeline lately. Terraform Plan stage was failing to parse a variable which type was list and the error message was not leading us to any solution. Here I am going to walk you through the path we managed to resolve that issue.
In Terraform we have an input variable defined as –
and Jenkins file is written so that, user can provide the values as a comma separated string while deploying the resources. Then Jenkins is creating a list of sources from that comma separated string as –
and being passed in terraform plan like –
This is an example command with actual value that ran-
Everything seems fine until it failed with an error message-
The error message is very confusing. Clearly the error message doesn’t match with what we are seeing. There is something weird happening in the middle.
Then we were looking at the terrraform plan command more carefully. We were thinking couple of things-
environment was passed correctlysources list are also string(as value of environment was also not enclosed by quote and parsed successfully) and correctly separated by commaids.split(',') should do that right?
After thinking random things it’s time for Goggling. It was clear that Terraform is not able to parse the value as list of string. So, Tried some keywords like-
Finally found a Git issue that describes the problem and solution-
In Unix shells, quotes are interpreted by the shell to override the default behavior of splitting arguments with whitespace, and so the first “level” of quotes is consumed by the shell and thus isn’t passed to Terraform at all. The first example
terraform plan -var 'myvar=["zzzz"]'works because the'quotes prevent special interpretation of the inner"quotes and so they are passed by the shell into the command line Terraform sees.Bellow variations won’t work-
terraform plan -var 'myvar=['zzzz']'
terraform plan -var "myvar=['zzzz']"
terraform plan -var myvar=['zzzz']
In short this is a unix shell behavior that consumed the quotes around the source ids in the list and if we put single quote ' around the sources var like -
Now we update the terraform plan command-
and the input parser-
It resolved the issue. Then a successful terraform command looked like-
In conclusion, cloud resource deployment with Terraform and Jenkins is a standard procedure, but it has drawbacks of its own. When parsing a list variable in Terraform via a Jenkins pipeline, we ran into a problem where the Unix shell behavior was eating quotes around the list elements, which led to the failure of the Terraform Plan stage.
We found that enclosing the list elements in double quotes and the variable in single quotes fixed the problem after doing some research and consulting internet resources.
We were able to successfully implement the Terraform plan thanks to this solution, which guaranteed automated and seamless deployments. I appreciate your attention, and I hope this walkthrough aids in debugging similar problems in your pipelines for continuous integration and delivery.
Tadaaaa……….
Title Image credit – Jammin’ with Terraform, AWS & Jenkins