Recover/Build new state file for Azure resources

Hello, I was posting to see if someone has a solution to a problem we are having.

We currently have infrastructure built out in Azure with terraform. This includes a Resource Group which contains a function app, storage account, application insight, private endpoints, keyvault and a whole lot of configuration settings.

The problem is that we were keeping the state file within GitLab however it has gotten deleted. When we try to deploy our infrastructure as code using our pipeline, we get numerous errors say that the resources such as the resource group, storage account, function app, etc… already exist.

Is there any way we can get all of the previous state back that was deleted to allow our infrastructure to redeploy and keep all of the settings and configuration? Thank you!

Error Message: “A resource with the ID xxx already exists - to be managed via Terraform this resource needs to be imported into the state.”

You need to look at using the terraform import command or import blocks.

1 Like

Is there a way to import the entire resource group along with all of the resources?

You will need to run import (or have an import block) for each resource you have in your code (including all instances created via count or for_each) - each resource will have a different ID or method of unique identification.