Deploy and destroy from Azure pipelin

Hi everyone, I try to deploy terraform code via Azure devops pipeline.
Deploy runs fine however destroy doesn’t work. Code is in github which is authenticated via pipeline. I assume it can’t find the terraform state file. Is there a way to provide it to the pipeline? As of now, I don’t know where it is :slight_smile:

Thanks

Erik

can you share with us which backend you are using?

It sounds like you’re using a local backend which is getting lost from stage to stage in the pipeline

Thank you for answering. This is the backend I am using:

backend “local” {}

Maybe this is required to have the terraform state in the pipeline?

terraform {
backend “azurerm” {
resource_group_name = “XXXXXXXXXXX”
storage_account_name = “XXXXXXXXXXX”
container_name = “XXXXXXXX”
key = “XXXXXXXXXXXX”
}
}

yes, that will put the state in an Azure storage account container. You can use any of the available backends, but if you’re running in ADO, this is a good choice.

The local state you were using before is persisted as a file in the container where the stage is running, and since that container is deleted when the stage ends, you also lose the state.

Another option, if you want to keep the local state backend, is to add the state file to the cache of the pipeline. I’m not sure how this is done in Azure, but this seems to show the gist of it: