Automation
terraform
Makes infrastructure match .tf files: plans the creates, updates and destroys, then applies them through providers.
Terraform reads the .tf files in a folder, written in HCL: each resource block describes one thing that
should exist, and a provider is the plugin that talks to the API managing it. init installs the providers. plan
reads every managed resource back, compares the files with the state (terraform.tfstate, its record of what it
made) and lists what an apply would create (+), update (~) or destroy (-). apply does it. With nothing changed, a
second plan says No changes.; a resource changed by hand shows up as an update back to the file.
terraform command usage
terraform [-chdir=DIR] init|validate|plan|apply|state list|destroy [OPTIONS]
Options
-
-chdir=DIR - work in that folder, as if run from it
-
init - install the providers the files need
-
validate - check the files' syntax and references, without touching any API
-
plan - read the resources back and print what an apply would change
-
apply -auto-approve - make the changes without asking for yes
-
plan -detailed-exitcode - exit 0 with no changes, 2 with changes, 1 on an error
-
state list - list the resources in the state
terraform examples
-
terraform -chdir=/root/westvale plan - Shows what an apply would create, change or destroy, changing nothing.
-
terraform -chdir=/root/westvale apply -auto-approve - Makes the devices match the files and records the result in the state.
-
terraform -chdir=/root/westvale plan -detailed-exitcode - Exits 2 when anything differs from the files: a drift check for a nightly job.
Pitfalls
- A plan only covers what's in the files and the state: something added by hand at another name stays invisible.
- Deleting a managed object outside Terraform doesn't remove it: the file still asks for it, so the next apply makes it again.