متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 159IaC · Terraform

How to use Terraform variables per environment

Dev gets one small VM, production gets three big ones — with the same Terraform code. Variables with types and defaults, plus one .tfvars file per environment, make that possible. This how-to declares good variables, writes dev.tfvars and prod.tfvars, explains which value wins, and shows how to keep each environment's state separate (the part people forget).

  • 5 الدروس
  • 3 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

159.1

The quick answer

variables.tf
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"
}
envs/prod.tfvars
env            = "prod"
instance_count = 3
instance_type  = "m6i.large"
terraform init -backend-config=envs/prod.backend.hcl
terraform plan -var-file=envs/prod.tfvars -out=tfplan

الأوامر في هذا الدرس

159.2

Step by step

  1. Declare every input with a type, a description, a default where it makes sense, and validation for values that must be right.
  2. **One .tfvars file per environment** in Git (no secrets in them):
envs/dev.tfvars
env            = "dev"
instance_count = 1
  1. Which value wins (last wins):
OrderSource
1 (lowest)default in the variable block
2Environment variables TF_VAR_name
3terraform.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.

  1. Separate state per environment — the same code with another .tfvars but the same state would replace prod with dev. Give each environment its own backend key:
envs/prod.backend.hcl
bucket       = "acme-tfstate-111122223333"
key          = "shop/prod/terraform.tfstate"
region       = "eu-central-1"
use_lockfile = true
terraform init -reconfigure -backend-config=envs/dev.backend.hcl
terraform plan -var-file=envs/dev.tfvars
  1. Derived values go into locals, not into more variables:
locals.tf
locals {
  name_prefix = "shop-${var.env}"
  common_tags = { env = var.env, team = "shop", managed_by = "terraform" }
}
  1. In CI — one job per environment, each with its backend file and tfvars, and an approval before prod apply.

159.3

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.

159.4

Common problems

ProblemCauseFix
Plan wants to replace prod with dev valuesSame state for both environments.Separate backend keys; init -reconfigure when switching.
A value is not the one you expectA terraform.tfvars/*.auto.tfvars or TF_VAR_ overrides it.Check the precedence table; remove auto-loaded files.
No value for required variableNo default and not set.Add it to the tfvars file or pass -var.
Number/bool read as stringWrong type in the variable.Set type = number/bool.
Secrets in a tfvars file in GitConvenient, 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.

الأوامر في هذا الدرس

159.5

Go further

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.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل