iocalhost.nl // no place like ::1
references Infrastructure as Code Terraform

Terraform

Infrastructuur als code — workflow, state en variabelen.

Kernworkflow

terraform init          providers + backend initialiseren
terraform plan          voorgestelde wijzigingen tonen
terraform apply         toepassen (vraagt bevestiging)
terraform apply -auto-approve
terraform destroy       alles afbreken
terraform fmt           code formatteren
terraform validate      syntax controleren

State

terraform state list                resources in state
terraform state show <adres>        details
terraform state rm <adres>          uit state halen (niet vernietigen)
terraform import <adres> <id>       bestaande resource importeren
terraform refresh                   state bijwerken vanaf echte infra

Variabelen

variable "region" {
  type    = string
  default = "eu-west-1"
}
resource "aws_instance" "web" {
  ami           = var.ami
  instance_type = "t3.micro"
  tags = { Name = "web" }
}
output "ip" { value = aws_instance.web.public_ip }
terraform apply -var="region=eu-central-1"
terraform apply -var-file=prod.tfvars
# of via omgeving: export TF_VAR_region=eu-west-1

Targeting & workspaces

terraform plan -target=aws_instance.web
terraform workspace new staging
terraform workspace select prod
terraform output -json

Commit nooit terraform.tfstate of *.tfvars met secrets. Gebruik een remote backend (S3, GCS) met state-locking.