Home > Back-end >  Terraform Destroy does not work with Azure Devops Variables in terraform.tfvars
Terraform Destroy does not work with Azure Devops Variables in terraform.tfvars

Time:05-10

I have very simple pipeline, classic pipeline with Terraform Init, Plan and Apply, how ever deployment failed in middle, so I wanted to destroy all resources… (backend is remote sitting on Azure blob container so I enabled only Init and Destroy Task in pipeline, In Init parameters I have provided remote backend details, but when I run pipeline with destroy command it says " variable not allowed"

Actually in terraform.tfvars file I have used azure variable group variable substitution like below

enter image description here

and I have destory task like below

enter image description here

error i get is:

enter image description here

CodePudding user response:

"It isn’t possible to define anything other than static values in a .tfvars file.", see Reference environment variables in .tfvars file.

Alternativly, you can rename the environment variables to start with TF_VAR_ prefix, e.g. TF_VAR_resource_group or you can try to pass the values via -var parameter.

CodePudding user response:

I normally recommend against this type of solutions as its non-canonical, e.g. there are ways to solve the problem as @sschmeck has posted, so adding a third-party tool can just create more of a headache. But this article details the use of using a "Replace Tokens" task in Azure DevOps.

Simply put, you can tell this task to scan for *.tfvars files, and have it replace some tokens with a patern such as __example__, so in your example:

resource_group = __resource_group__

And set the resource_group variable in a Azure DevOps variable group - it will then search for your specified pattern and replace it.

Again - I would say use TF_VARs as its canonical, but this may also work for you.

  • Related