|
| 1 | +# Terraform |
| 2 | + |
| 3 | +> **Terraform** is an infrastructure as code tool that lets you build, change, and version infrastructure safely and efficiently |
| 4 | +
|
| 5 | +Resources |
| 6 | + |
| 7 | +* [Terraform in Action](https://www.manning.com/books/terraform-in-action) |
| 8 | +* [Documentation](https://developer.hashicorp.com/terraform) |
| 9 | +* [Terraform Registry](https://registry.terraform.io) |
| 10 | + |
| 11 | +Setup |
| 12 | +```sh |
| 13 | +brew tap hashicorp/tap |
| 14 | +brew install hashicorp/tap/terraform |
| 15 | + |
| 16 | +terraform version |
| 17 | +``` |
| 18 | + |
| 19 | +Terraform's core building blocks: |
| 20 | + |
| 21 | +* `terraform` Terraform's own settings (required version, providers, backend/state config) |
| 22 | +* `provider` tells Terraform which cloud/API to talk to and how to authenticate |
| 23 | +* `resource` something Terraform creates/updates/deletes e.g. VM, subnet, bucket |
| 24 | +* `data` something Terraform reads only (already exists) |
| 25 | +* `variable` input values for your config (like function arguments) |
| 26 | +* `locals` internal named values/calculations to avoid repeating logic |
| 27 | +* `output` values Terraform prints after apply (and exposes to parent modules) |
| 28 | +* `module` reusable Terraform package/folder you call from another config |
| 29 | + |
| 30 | +Examples |
| 31 | +```sh |
| 32 | +# formatting/linting |
| 33 | +terraform fmt -check |
| 34 | + |
| 35 | +# initializing a configuration directory downloads and installs the providers defined in the configuration |
| 36 | +cd terraform/docker-example |
| 37 | +terraform init |
| 38 | + |
| 39 | +# validatiom |
| 40 | +terraform validate |
| 41 | +# dry run |
| 42 | +terraform plan |
| 43 | + |
| 44 | +# with rancher desktop |
| 45 | +terraform plan -var="docker_host=unix:///${HOME}/.rd/docker.sock" |
| 46 | +# apply with "TF_VAR_<name>" convention |
| 47 | +TF_VAR_docker_host="unix:///${HOME}/.rd/docker.sock" terraform apply -auto-approve |
| 48 | + |
| 49 | +# verify state |
| 50 | +terraform show |
| 51 | +terraform state list |
| 52 | + |
| 53 | +# http://localhost:8080 |
| 54 | +terraform output -json |
| 55 | + |
| 56 | +# controlled/appoved deployments (binary format) |
| 57 | +terraform plan -out=plan.out |
| 58 | +terraform show -json plan.out |
| 59 | +terraform apply plan.out |
| 60 | +``` |
0 commit comments