Using if statement in terraform


As immutable infrastructure I’m deleting my resources terraform resources using terraform destroy before creating new ones using apply.
However if I pass a variable like true or false. can the terraform script stop at destroy and exit else it should continue with apply? Any example would be much appreciated.

Thanks in advance.

If I understand correctly you want a way to prevent you from accidentally destroying your infrastructure?

I had the same need at my company and came up with a procedure using a variable that prevents “production” resources to be deleted:

Take a look at the following explanation (for Terraform 0.11 in AWS):

And a full example with source code for RDS and EC2 updated to Terraform 0.12 at:

Don’t know if there is a better approach.


Dear javier,

Really appreciate your response. Will explore it and let you know. Thank you very much.