هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
ماذا يفعل
Terraform reads .tf files, compares them with the state (what it built before) and the real world, and makes a plan: what to create, change or destroy. apply runs the plan through providers (azurerm, aws, kubernetes, github, …). The same code builds dev, test and prod. OpenTofu (tofu) is the open-source fork with the same commands.
The state file holds every resource ID and often secrets; keep it in a remote backend (Azure Storage, S3, HCP Terraform) with locking — never in Git.
طريقة الكتابة
terraform [-chdir=DIR] COMMAND [OPTIONS]
terraform init && terraform plan -out tfplan && terraform apply tfplanالأجزاء الرئيسية
Main commands
init- Download providers and modules, set up the backend.
fmt[-recursive]- Format the code.
validate- Check syntax and types.
plan[-out FILE]- Show what will change.
apply[FILE]- Make the changes.
destroy- Delete everything in this configuration.
output[NAME]- Show output values.
statelist|show|mv|rm- Read or change the state.
importADDRID- Bring an existing resource under Terraform.
workspacenew|select|list- Several states for one configuration.
providerslock- Write provider hashes for more platforms.
console- Try expressions.
graph- Dependency graph (DOT format).
force-unlockID- Remove a stuck state lock (careful).
test- Run
.tftest.hcltests.
الخيارات 10
الأكثر استخدامًا
Common flags
لا يوجد شيء يطابق هذا الفلتر.
أمثلة 11
$ مستخدم عادي# بصلاحية root (أو مع sudo)
1The daily loop
terraform init terraform fmt -recursive terraform validate terraform plan -out tfplan terraform apply tfplan2A small Azure configuration
cat main.tf terraform { required_version = ">= 1.9" required_providers { azurerm = { source = "hashicorp/azurerm", version = "~> 4.0" } } backend "azurerm" { resource_group_name = "rg-tfstate" storage_account_name = "sts0statfstate" container_name = "tfstate" key = "lab.tfstate" } } provider "azurerm" { features {} } resource "azurerm_resource_group" "lab" { name = "rg-lab-weu" location = "westeurope" tags = { owner = "s0sta", env = "lab" } }3Use variables per environment
cat prod.tfvars location = "westeurope" vm_size = "Standard_B2s" terraform plan -var-file=prod.tfvars -out tfplan4Read outputs
terraform output terraform output -raw vm_public_ip5Look into the state
terraform state list terraform state show azurerm_resource_group.lab6Rename a resource without destroying it
cat moved.tf moved { from = azurerm_resource_group.lab to = azurerm_resource_group.main } terraform plan7Import an existing resource
cat import.tf import { to = azurerm_resource_group.old id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-old" } terraform plan -generate-config-out=generated.tf8Find drift in CI
terraform plan -detailed-exitcode -lock=false echo $? 20 = no changes, 1 = error, 2 = changes (drift or new code).
9Use a module
cat network.tf module "vnet" { source = "Azure/avm-res-network-virtualnetwork/azurerm" version = "0.7.1" name = "vnet-lab" resource_group_name = azurerm_resource_group.lab.name location = "westeurope" address_space = ["10.20.0.0/16"] }10Scan the code for security problems
trivy config . checkov -d .11Clean up a lab
terraform destroy -var-file=lab.tfvars
لا يوجد شيء يطابق هذا الفلتر.
انتبه
- Never commit
*.tfstate,.terraform/or*.tfvarswith secrets; add them to.gitignore. - Commit
.terraform.lock.hcl— it pins provider versions and hashes. - Review every plan: a changed name can mean "destroy and create" (data loss).
- Mark secret variables and outputs
sensitive = true(they are still in the state in clear text).
الملفات
*.tf- The configuration.
terraform.tfstate- Local state (use a remote backend instead).
.terraform.lock.hcl- Provider versions and hashes (commit it).
.terraform/- Downloaded providers and modules (do not commit).
~/.terraformrc- CLI settings (plugin cache, credentials).
نصائح للحفظ
- In CI:
planon every PR (post it as a comment),applythe saved plan only after merge and approval. - Use OIDC (workload identity federation) for CI logins to Azure — no client secrets.
رموز الخروج
0 | Success (no changes with -detailed-exitcode). |
1 | Error. |
2 | Plan has changes (only with -detailed-exitcode). |