Introduction
The Terraform Enterprise supports a wide range of REST API. The parameters of the REST API are generally defined in JSON data structure. In Terraform Enterprise, it matches the variables configured on the workspaces to those variables defined in the Terraform CLI configuration files. Terraform CLI also supports input variables in various types including list, map, and object, more details of Terraform CLI input variables can be found in this document. As a result, the content of API payload may contain nested complex data structure when passing the values of input variables via the Workspace Variable API.
Use Case
When creating or updating variables of Terraform Enterprise workspaces with non-primitive types such as list, map, and object, using the Workspace Variable API from ServiceNow.
Problem
When sending a nested value of the input variables in complex structure, it requires special attention on the escape characters due to ServiceNow removes the escaping backslash in the process of attaching the variable to the REST API call prior to sending it out. Terraform also takes another escaping backslash out when it detects the variable is in HCL.
Solution
Prepare the input payload with an appropriate number of escaping backslash. With non-primitive data nested with string data type, it had to double escape the double quotes and it also had to surround the entire input value with single quotes. For example:
{
"data": {
"type": "vars",
"attributes": {
"key": "some_key",
"value": '[\\"sg-001\\",\\"sg-002\\"]',
"description": "some description",
"category": "terraform",
"hcl": true,
"sensitive": false
},
"relationships": {
"workspace": {
"data": {
"id": "ws-TFh1mDfjCkURfxsd",
"type": "workspaces"
}
}
}
}
}
Additional information
If you continue to experience the issues after following the guides, please contact HashiCorp Support to request for further assistance.