https://developer.hashicorp.com/terraform/docs
Declarative infrastructure. You describe the resources you want, Terraform diffs that against recorded state and makes the minimum set of API calls to converge.
Where this differs from Ansible: Ansible converges the inside of a machine that already exists, running tasks in order. Terraform creates and destroys the resources themselves — DNS records, VMs, cloud objects — and works out the order from the dependency graph. They compose: Terraform makes the host, Ansible configures it.
Via the Ansible role already declared in ../ansible/requirements.yml:
- hosts: all
roles:
- diodonfrost.terraformOr directly from the HashiCorp repo — see https://developer.hashicorp.com/terraform/install
# Fedora / RHEL
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager addrepo --from-repofile=https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
sudo dnf install terraform
terraform versionterraform init # download providers, set up the backend. Re-run when either changes.
terraform fmt # canonical formatting
terraform validate # syntax and type checking, no API calls
terraform plan # what would change. Read this every time.
terraform apply # make it so
terraform destroy # tear it downplan is the whole point. It is the dry run that tells you whether a change is
additive or whether Terraform intends to delete and recreate something.
terraform {
required_version = ">= 1.6"
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}
provider "cloudflare" {
api_token = var.cloudflare_api_token
}
variable "cloudflare_api_token" {
type = string
sensitive = true # keeps it out of CLI output
}
variable "zone_id" {
type = string
}
resource "cloudflare_record" "docker1" {
zone_id = var.zone_id
name = "docker1"
content = "10.0.0.11"
type = "A"
proxied = false
}
output "docker1_fqdn" {
value = cloudflare_record.docker1.hostname
}Files ending .tf in a directory are one configuration — the split across files is
for your benefit, not Terraform's. The conventional layout is main.tf,
variables.tf, outputs.tf, versions.tf.
Never put secrets in a .tf file. Options, roughly in order of preference:
export TF_VAR_cloudflare_api_token='...' # environment
terraform apply -var-file=secret.tfvars # file, gitignored
terraform apply -var='zone_id=abc123' # inline, ends up in shell historyA terraform.tfvars in the working directory is loaded automatically, which is
convenient and exactly why it needs to be gitignored.
Terraform records what it created in terraform.tfstate.
- It contains secrets. Resource attributes are stored in the clear, including generated passwords and keys. Treat the state file as sensitive.
- Never hand-edit it. Use
terraform statesubcommands. - Never commit it when more than one machine or person will run Terraform — two local states diverge silently and then fight.
terraform state list # every resource Terraform knows about
terraform state show <addr> # one resource in full
terraform import <addr> <id> # adopt something created by hand
terraform state rm <addr> # forget it without destroying itimport is the one to reach for when something already exists — it brings a
hand-made resource under management instead of having Terraform try to create a
duplicate and fail.
For anything beyond a single workstation, move state off local disk. Terraform supports an HTTP backend, which Forgejo and GitLab both implement:
terraform {
backend "http" {
address = "https://git.example.com/api/packages/OWNER/terraform/state/NAME"
lock_address = "https://git.example.com/api/packages/OWNER/terraform/state/NAME/lock"
unlock_address = "https://git.example.com/api/packages/OWNER/terraform/state/NAME/lock"
lock_method = "POST"
unlock_method = "DELETE"
}
}Credentials go in terraform init -backend-config=... or the environment, not in
the block.
planshowing a resource being destroyed and recreated usually means you changed an attribute that the provider cannot update in place. Check whether the provider documents that field as forcing replacement before you apply.- A provider version with
~>pins the minor, not the patch.initrecords exact versions in.terraform.lock.hcl— commit that file. terraform destroyrespects no confirmation beyond one prompt. There is no undo.