هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
variable "env" {
type = string
validation {
condition = contains(["dev", "staging", "prod"], var.env)
error_message = "env must be dev, staging or prod."
}
}
variable "instance_count" {
type = number
default = 1
}
variable "instance_type" {
type = string
default = "t3.small"
}env = "prod"
instance_count = 3
instance_type = "m6i.large"Step by step
- Declare every input with a
type, adescription, adefaultwhere it makes sense, andvalidationfor values that must be right. - **One
.tfvarsfile per environment** in Git (no secrets in them):
env = "dev"
instance_count = 1- Which value wins (last wins):
| Order | Source |
|---|---|
| 1 (lowest) | default in the variable block |
| 2 | Environment variables TF_VAR_name |
| 3 | terraform.tfvars, then *.auto.tfvars (loaded automatically) |
| 4 (highest) | -var-file=… and -var name=value on the command line |
Avoid terraform.tfvars when you have several environments — it is loaded always, which surprises people.
- Separate state per environment — the same code with another
.tfvarsbut the same state would replace prod with dev. Give each environment its own backend key:
bucket = "acme-tfstate-111122223333"
key = "shop/prod/terraform.tfstate"
region = "eu-central-1"
use_lockfile = trueterraform init -reconfigure -backend-config=envs/dev.backend.hcl
terraform plan -var-file=envs/dev.tfvars- Derived values go into
locals, not into more variables:
locals {
name_prefix = "shop-${var.env}"
common_tags = { env = var.env, team = "shop", managed_by = "terraform" }
}- In CI — one job per environment, each with its backend file and tfvars, and an approval before prod apply.
Check that it worked
terraform plan -var-file=envs/dev.tfvars and …/prod.tfvars show different sizes and counts, each against its own state (terraform state list differs), and an invalid env value stops with your validation message.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Plan wants to replace prod with dev values | Same state for both environments. | Separate backend keys; init -reconfigure when switching. |
| A value is not the one you expect | A terraform.tfvars/*.auto.tfvars or TF_VAR_ overrides it. | Check the precedence table; remove auto-loaded files. |
No value for required variable | No default and not set. | Add it to the tfvars file or pass -var. |
| Number/bool read as string | Wrong type in the variable. | Set type = number/bool. |
| Secrets in a tfvars file in Git | Convenient, but leaked. | TF_VAR_ from CI secrets, or a secret store data source. |
| Forgot which backend is active | .terraform remembers the last init. | Use separate folders per env, or always init -reconfigure in scripts. |
Go further
- Command pages: terraform.
- Next how-tos: Separate environments in Terraform, Keep secrets out of Terraform, Store Terraform state in S3 with locking.
- Learn it: Terraform — Variables, Variable values, Locals, Remote state.
- Practise: Lab: a full configuration with variables, locals, outputs and a data source.
1Which wins: a default, TF_VAR_x, or -var-file?
-var-file (and -var) on the command line win over TF_VAR_ and defaults.
2Why does every environment need its own state?
With one shared state, applying dev values would change or destroy prod resources.