Terraform: Force Destroy Resource when prevent_destroy is true

Tags: March 3, 2020 6:55 AM

How to Force Destroy Resource in Terraform

Terraform resource that having lifecycle prevent_destroy = true can not be destroyed. You need to manually edit the file inplace and change the value prevent_destroy to false manually each time you want to destroy the resource. Instead of having to edit manually and make git status dirty we can automate this using simple shell script.

Automate Force Destroy Resource in Terraform

The idea is simple.

  1. Search all *.tf files and look the value of prevent_default = true to prevent_default = false
  2. Run terraform destroy command
  3. Revert the changes back to prevent_default = true
Here is the implementation in Bash.
#!/bin/bash

$ find . -name '*.tf' -type f \
-exec perl -i -pe 's@prevent_destroy = true@prevent_destroy = false@g' {} \;

# Run terraform destroy
[ "$IS_PLAN" = "yes" ] && terraform plan -destroy || terraform destroy $@

# Revert the changes
$ find . -name '*.tf' -type f \
 -exec perl -i -pe 's@prevent_destroy = false@prevent_destroy = true@g' {} \;
Save the file with name e.g: terraform-force-destroy.sh. To issue terraform plan -destroy command use the following.
$ IS_PLAN=yes bash terraform-force-destroy.sh
To force destroy the resource use the following command.
$ bash terraform-force-destroy.sh -auto-approve
You can give normal terraform's arguments just like the original terraform destroy.

References for Terraform Force Destroy

Share on Facebook Twitter

1 comments:

ileanvalderrama said...

일반 갬블러를 VIP로 올리기 위해 여러 회유책(?)도 사용하게 되죠. 입국이 코로나 이전만큼 자유롭지는 않지만 온라인카지노 카지노를 즐기는 VIP 갬블러들에겐 자가격리 일수나 항공권 금액은 문제가 되지 않습니다. 하지만 백신 접종인구가 1000만명을 돌파하고 당국도 ‘여행안전권역’(트래블버블) 도입을 추진하는 지금이 ‘골든타임’이라는 생각도 들었습니다.

Post a Comment