A variable or local in the backend block
CommonUsually an attempt to choose the state bucket, key or path per environment with var.env or a local.
Confirm
terraform init -input=false
The error pointing at a line inside terraform { backend ... }
Fix
- Remove the arguments that vary from the block and pass them at init: terraform init -backend-config="key=envs/prod.tfstate", or keep one file per environment, such as prod.tfbackend, and run terraform init -backend-config=prod.tfbackend.
- When you switch between those files in one working directory, add -reconfigure if each file points at its own existing state. If you are moving state that is already in use to a new location, use -migrate-state instead. Either way, check terraform state list before you apply.
- If the only difference is the environment's name, Terraform workspaces keep separate state under one backend configuration.
terraform init -reconfigure -backend-config=prod.tfbackend
prod.tfbackend holds lines such as key = "envs/prod.tfstate". Reproduced with the local backend's path argument on Terraform 1.14.0.