Skip to content

Add plan to terraform destroy

Adds TF_PLAN_CACHE to the terraform destroy command, to prevent issues with projects that do not use auto.tfvars.

Currently, we run a plan using something like so:

gitlab-terraform plan -var-file=example.tfvars $(if [[ "$TF_DESTROY" == "true" ]]; then echo '-destroy'; fi)

after which, running an apply with simply gitlab-terraform apply works without issue.

However, when running gitlab-terraform destroy we get errors like the following:

│ Error: No value for required variable

│   on main.tf line 3:
│    3: variable "subnetwork" {type=string}

│ The root module input variable "subnetwork" is not set, and has no default
│ value. Use a -var or -var-file command line argument to provide a value for
│ this variable.

which seems to be because the current gitlab-terraform destroy command does not make use of TF_PLAN_CACHE, unlike gitlab-terraform apply, essentially ignoring any plan created by gitlab-terraform plan -destroy

This does not appear to occur on projects using auto.tfvars, as the issue is masked by the fact that terraform is ultimately recalculating an entire new plan as part of the destroy, and is able to do so without input from the user. We are seeing this issue because several of our projects can not leverage auto.tfvars, and so we'd need to manually pass them to the destroy in order for it to recreate a proper plan.

Merge request reports