Terraform
Terraform is an infrastructure as code tool that lets you build, change, and version infrastructure safely and efficiently
Resources
Setup
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
terraform version
Terraform's core building blocks:
terraformTerraform's own settings (required version, providers, backend/state config)providertells Terraform which cloud/API to talk to and how to authenticateresourcesomething Terraform creates/updates/deletes e.g. VM, subnet, bucketdatasomething Terraform reads only (already exists)variableinput values for your config (like function arguments)localsinternal named values/calculations to avoid repeating logicoutputvalues Terraform prints after apply (and exposes to parent modules)modulereusable Terraform package/folder you call from another config
Docker example
# formatting/linting
terraform fmt -check
# initializing a configuration directory downloads and installs the providers defined in the configuration
cd terraform/docker-example
terraform init
# validatiom
terraform validate
# dry run
terraform plan
# with rancher desktop
terraform plan -var="docker_host=unix:///${HOME}/.rd/docker.sock"
# apply with "TF_VAR_<name>" convention
TF_VAR_docker_host="unix:///${HOME}/.rd/docker.sock" terraform apply -auto-approve
# cleanup
terraform destroy -var="docker_host=unix:///${HOME}/.rd/docker.sock"
# verify state
terraform show
terraform state list
# http://localhost:8080
terraform output -json
# controlled/appoved deployments (binary format)
terraform plan -out=plan.out
terraform show -json plan.out
terraform apply plan.out