Skip to main content
DevOps Insightsguides & answers

Terraform · troubleshooting

Error: Invalid value for input variable

A value given to a Terraform variable does not match the variable's type, or fails one of its validation rules. The message says which variable, where the value came from, and what was expected.

Reproduced on Terraform 1.14.0, . Where the text says “reproduced”, the behaviour was run on that version, not taken from documentation.

First published

New to this? Read what is actually happening first, then come back to the steps.

On this page · 8 sections

Before you start

  • The full error, which names the variable and where its value came from.

Run this first

step 1 of 1
terraform plan -input=false

Look for The variable name, the source of its value and either the required type or the validation rule's location

Runs the variable checks without prompting for missing values.

Work out which cause you have

A few questions to narrow the list. Every answer ends in a command that confirms or rules the cause out — this cannot see your cluster, so nothing here is a certainty until you have checked.

Narrow it down

0 answered · nothing is sent anywhere

Which heading does the error have?

Cause space

3 of 3 still possible

  • The value has the wrong typeCommon
  • The value fails a validation ruleCommon
  • A value comes from a source you forgotOccasional

Nothing ruled out yet. Answer the question above and the branches your answer eliminates will strike through here.

Or diagnose it manually

In this order. The first command usually contains the whole answer.

  1. Step 1

    terraform plan -input=false

    Runs the variable checks without prompting for missing values.

    Look for The variable name, the source of its value and either the required type or the validation rule's location

Every cause, and how to fix it

Ordered by how often each one turns out to be the answer.

The value has the wrong type

Common

A word where a number is expected, a string where a list is expected, or a value whose quoting changed it into something else.

Confirm

terraform plan -input=false

Which variable, where the value was set (-var, a file and line, or TF_VAR_), and the type required

Fix

  • Pass a value of the declared type: -var replicas=3, or replicas = 3 (no quotes) in a .tfvars file.
  • For lists, maps and objects, use Terraform syntax: -var 'tags={team="platform"}' or -var 'zones=["a","b"]'; the same syntax works in TF_VAR_ variables.

The value fails a validation rule

Common

The variable's validation block rejected it. The message shows the value Terraform received and the rule's own error_message.

Confirm

grep -n -A4 'validation {' *.tf

The condition at the line the error names, and what values it allows

Fix

  • Pass a value the rule accepts, or, if the rule is wrong, fix the condition and its error_message together.

A value comes from a source you forgot

Occasional

A TF_VAR_ variable in the shell or CI settings, or an auto-loaded terraform.tfvars or *.auto.tfvars file, can set a value you did not expect.

Confirm

env | grep '^TF_VAR_'; find . -maxdepth 1 -name '*.tfvars'

A source for the variable named in the error, other than the one you meant to use

Fix

  • Remove or correct the stray value. The error message names the source it came from, which is the place to look first.

Understanding it properly

Skip this if you are mid-incident — the working part of the page is above. Worth reading afterwards, because understanding the mechanism is what stops the next one.

In plain terms

A Terraform variable can declare its type (number, string, list, map) and rules the value must pass. When you set it on the command line, in a .tfvars file or through a TF_VAR_ environment variable, Terraform checks the value against both before planning anything. This error is that check failing.

Words used on this page

Type constraint
The type = line in a variable block: number, string, bool, list(...), map(...) or object({...}).
Validation rule
A validation block inside a variable, with a condition and an error_message shown when the condition is false.
TF_VAR_name
An environment variable Terraform reads as the value of variable name.
What is actually happening

Reproduced on Terraform 1.14.0. A number variable given "three" through -var, through a .tfvars file and through TF_VAR_replicas failed each time with "Error: Invalid value for input variable", and each message named its source: "set using -var=\"replicas=...\"", the line in the .tfvars file, or "set using the TF_VAR_replicas environment variable", followed by "a number is required".

A value that has the right type but fails a validation block gets a slightly different heading, "Error: Invalid value for variable", shows the value, your error_message and "This was checked by the validation rule at main.tf:7,3-13".

One case does not produce this error at all: -var 'tags=team' for a map(string) variable failed with Variables not allowed, because Terraform read team as an expression. The map has to be written in Terraform syntax: -var 'tags={team="platform"}'.

How to tell this is your problem
WhereWhat you see
terraform planError: Invalid value for input variable … a number is required.
terraform plan, validationError: Invalid value for variable … This was checked by the validation rule at main.tf:7,3-13.

How to know it is actually fixed

  • terraform plan gets past variable checking and shows a plan.

Stopping it happening again

  • Give variables precise types and validation rules with clear error_message text, so a bad value fails at plan time with an explanation.
  • Keep TF_VAR_ settings for CI in one documented place, so a forgotten value does not override the one you pass.

Sources

Behaviour described here is drawn from official documentation. Where a figure could not be confirmed on an official page it is attributed in the text rather than stated as canonical.

Did this get you to an answer?

No text box on purpose — please do not paste production logs anywhere

Something wrong or out of date? Report it