Skip to content

IaC & CI/CD: Terraform

Terraform commands from the IaC & CI/CD folder.

Index
6 items

Terraform: Initialize and upgrade providers

Safe

Download modules/providers and upgrade within the version constraints.

terraform init -upgrade
What each part does
  1. terraform initset up the backend, download providers and modules
  2. -upgradepick the newest versions the constraints allow and update .terraform.lock.hcl

Also known as: tf init

Terraform: Plan and save to a file

Safe

Preview changes and save the exact plan that will be applied.

terraform plan -out=tfplan
What each part does
  1. terraform planshow what would change
  2. -out=tfplansave the plan so terraform apply tfplan does exactly that

Also known as: tf plan

Terraform: Apply a saved plan

Caution

Applies exactly what was reviewed in the saved plan. No re-plan surprises.

terraform apply tfplan
What each part does
  1. terraform applymake the changes
  2. tfplanthe saved plan file: applies exactly what was reviewed, with no new prompt

Also known as: tf apply

Terraform: Inspect state for a resource

Safe

All attributes Terraform has recorded for one resource.

terraform state show <resource-address>
What each part does
  1. terraform state showprint every attribute Terraform has stored for one resource
  2. <resource-address>e.g. aws_instance.web or module.vpc.aws_subnet.private[0] (see terraform state list)
Example
$ terraform state show 'module.vpc.aws_subnet.private[0]'

Also known as: tf state show · terraform state list

Terraform: Release a stuck state lock

Destructive

Removes a lock left behind by a crashed run. Make sure nothing else is running first.

terraform force-unlock <lock-id>
What each part does
  1. terraform force-unlockremove a state lock left by a crashed or killed run
  2. <lock-id>the ID shown in the Error acquiring the state lock message; make sure no one else is running

My note The lock ID is in the error message. See the gotcha on state locks.

Also known as: Error acquiring the state lock · state lock · ConditionalCheckFailedException · tf lock

Terraform: Enable debug logging

Safe

Write provider/API level debug logs to a file.

TF_LOG=DEBUG TF_LOG_PATH=./tf-debug.log terraform plan
What each part does
  1. TF_LOG=DEBUGlog level for this run: TRACE, DEBUG, INFO, WARN, ERROR
  2. TF_LOG_PATH=./tf-debug.logwrite the log to a file instead of stderr
  3. terraform planthe command to debug

Also known as: TF_LOG · tf debug

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…