Skip to content

OpenTofu

Usage

tofu apply -auto-approve

Debug:

OS_DEBUG=1 TF_LOG=DEBUG  apply

updating provider versions

find . -name versions.tf -exec sed -i 's/1.22.3/1.22.7/' {} \;

Import

Import a resource when a provider upgrade would force a resource recreation:

tf state show module.ricote.libvirt_pool.default  # note the `id`
tf state rm module.ricote.libvirt_pool.default
tofu import module.ricote.libvirt_pool.default 09f0dc94-5984-4330-b1fe-f150050643d1

Add provider versions to each submodule

git grep -l gitlabhq/gitlab | xargs -n 1 sed -i '/"gitlabhq\/gitlab"/a \      version = "16.3.0"'

Initialize all

find . -type d -name .terraform -exec tofu -chdir={}/.. init -upgrade \

or

for i in infrastructure infrastructure/* global environments/*/* kubernetes kubernetes/* ;\
  do echo $i; tofu init -upgrade $i; done

Remove all .terraform dirs

find . -type f -name .terraform.lock.hcl -exec rm {} \; && find . -type d -name .terraform -exec rm -rf {} \;

Tofu state

tf state list
tf state show 'google_compute_instance.legacy_vm["ofts105-3"]'

Style

Linting

  • pre-commit-opentofu
  • terraform validate isn't able to catch invalid resources like aws instance type, but tflint is i.e.

tflint

Debug mode:

TFLINT_LOG=debug tflint

Issues:

Vim integration

Additional tools

Terrascan

Detect compliance and security violations across Infrastructure as Code to mitigate risk before provisioning cloud native infrastructure.

Usage:

terrascan scan -i terraform .

Issues:

checkov

Install:

pipx install checkov

Scan directory:

checkov --directory .

terraform-docs

Usage:

terraform-docs markdown table .

Gitblab CI & terraform