Problem
When you trigger a run using the HCP Terraform API, the run fails during the plan or apply phase with one of the following errors:
Scenario 1:
Error: Variables not allowedScenario 2:
Error: No value for required variable
Cause
These errors occur due to incorrectly formatted variables within the attributes.variables block of the JSON payload sent to the API. Common causes include improper syntax for string values or using an incorrect key for a variable.
Below is an example of a payload structure that can cause these issues if not formatted correctly.
{
"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 1: Correctly Format String Variables
The Variables not allowed error often occurs when a string variable in your Terraform configuration is not correctly represented in the JSON payload.
For a Terraform variable defined as a string, the value in the JSON payload must be a string that contains escaped quotes.
Terraform Configuration:
variable "region" {
type = string
}Correct Payload: Notice the \" characters which correctly pass a quoted string to Terraform.
{
"key": "region",
"value": "\"us-east-1\""
}Incorrect Payload: This payload will cause the error because the value us-east-1 is not a valid HCL string literal.
{
"key": "region",
"value": "us-east-1"
}Solution 2: Use Correct Variable Key Names
The No value for required variable error occurs when the key in the JSON payload does not match the name of the variable declared in your Terraform code.
Terraform Configuration:
variable "resource_group" {}Correct Payload: The key must exactly match the variable name, resource_group.
{
"key": "resource_group",
"value": "\"my-rg\""
}Outcome
After you correct the variable formatting in your API payload, the plan and apply runs will proceed without these errors.
Additional Information
You can provide complex variable types, such as maps or lists, in the JSON payload. All values must be expressed as a valid HCL literal, just as you would write them in a .tf file. For example, to pass a map variable named alb, you would format the payload as follows.
JSON Payload for a Map Variable:
{
"data": {
"attributes": {
"variables": [
{
"key": "alb",
"value": "{name=\"alb\"\ninternal=true\nsubnet_ids=[\"subnet-03637308c48757029\"]}"
}
]
}
}
}This payload is equivalent to the following HCL variable definition.
Equivalent HCL:
variable "alb" {
default = {
name = "alb"
internal = true
subnet_ids = ["subnet-03637308c48757029999911"]
}
}For more details, please refer to the official HCP Terraform API documentation for creating runs.