Introduction
Problem
Variables provided in the API call that generates a run are not honoured or being recognized.
Plan/Apply runs fail with errors:
Scenario 1 Error:
Scenario 2 Error:
Cause
- The cause of the errors is the content of the "attributes": { "variables":[] } block in the payload. Example payload below:
{
"data": {
"attributes": {
"variables": [
{
"key": "string_one",
"value": "\"ABCDE12345\""
},
{
"key": "length",
"value": "32"
},
{
"key": "alb",
"value": "{name=\"alb\"\ninternal=true\nsubnet_ids=[\"subnet-03637308c48757029\"]}"
},
{
"key": "list_strings",
"value": "[\"ABCDE123456\" , \"ABCDE123456\",\"ABCDE123456\"]"
},
{
"key": "region",
"value": "\"us-east-1\""
}
]
},
"type": "runs",
"relationships": {
"workspace": {
"data": {
"type": "workspaces",
"id": "ws-LLGHCr4SWy28wyGN"
}
},
"configuration-version": {
"data": {
"type": "configuration-versions",
"id": "cv-n4XQPBa2QnecZJ4G"
}
}
}
}
}
Solutions
-
Solution Scenario 1:
If you have in your code a terraform variable type string for example:variable "region" {
type = string
}
And you want to provide the value of this variable through the API call , then in the json payload the correct syntax should be (including escape character prior any quotes \") :
Incorrect payload will result in the error Variables not allowed
example of INCORRECT payload causing the error: - Solution Scenario 2:
if variable is declared as follows :
variable "resource_group" {
}
However the value is not passed because of a wrong key in the payload. The key should be the variable name declared in the terraform code so the CORRECT payload is:
Outcome
When correct variables are provided the plan/apply runs should not fail with the above errors.
Additional Information
-
You can have more complex type of variables provided in the json payload of the API call. All values must be expressed as an HCL literal in the same syntax you would use when writing Terraform code for example a variable
alb
type map below:
"data": {
"attributes": {
"variables": [
{ "key": "alb", "value": "{name=\"alb\"\ninternal=true\nsubnet_ids=[\"subnet-03637308c48757029\"]}"},
]
},
This payload is equivalent to writing the variable in HCL
.tf code as follows:
variable "alb" {
default = {
name = "alb"
internal = true
subnet_ids = ["subnet-03637308c48757029999911"]
}
}