Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Vaardigheden in dit hoofdstuk
- init, validate, plan, apply, destroy en fmt
Know the workflow
Write the code, preview the changes, apply them — in a team through pull requests.
| Step | Command | Does |
|---|---|---|
| Write | (editor) | Describe the wanted infrastructure |
| Init | terraform init | Download providers and modules, set up the backend |
| Validate | terraform validate | Check syntax and references |
| Plan | terraform plan | Show what would change |
| Apply | terraform apply | Make the changes |
| Destroy | terraform destroy | Delete everything in this configuration |
Initialise a working directory
init prepares the folder. Run it first, and again after you add providers, modules or change the backend.
terraform init
Initializing the backend...
Initializing provider plugins...
- Installing hashicorp/azurerm v4.20.0...
Terraform has been successfully initialized!
terraform init -upgrade
terraform init -reconfigure
terraform init -migrate-state
ls -a
.terraform .terraform.lock.hcl main.tf variables.tf| Option | Use |
|---|---|
-upgrade | Get newer provider/module versions within the constraints |
-reconfigure | Use the backend settings again, ignore the saved ones |
-migrate-state | Move existing state to a new backend |
-backend-config=FILE | Give backend settings from a file (partial configuration) |
Validate and format
validate finds errors without contacting the cloud. fmt rewrites files in the standard style.
terraform fmt
terraform fmt -recursive
terraform fmt -check -diff
terraform validate
Success! The configuration is valid.Op het werk
In CI, run terraform fmt -check and terraform validate on every pull request — they fail the pipeline when someone forgot.
Plan the changes
plan compares code, state and the real world, and lists every change: + create, ~ update in place, -/+ replace, - destroy.
terraform plan
# azurerm_resource_group.web will be created
+ resource "azurerm_resource_group" "web" {
+ location = "westeurope"
+ name = "rg-web-dev"
}
Plan: 1 to add, 0 to change, 0 to destroy.
terraform plan -out=tfplan
terraform plan -var-file=prod.tfvars
terraform plan -target=azurerm_resource_group.web
terraform plan -refresh-only
terraform plan -destroyLet op
Read every -/+ (replace) line. Replacing a database or a disk means the old one is deleted.
Apply the changes
apply runs a plan and asks for yes. Applying a saved plan file runs exactly that plan, without asking.
Destroy infrastructure
destroy deletes all resources in the state of this configuration.
Use the CLI helpers
A few more commands help every day.