Can I manage my already created resources in Azure by Terraform, can I use terraform for using it as a tool to restore my backup files ( those RSV are already created)
One of my main requirement is to restore when DR happens and restore resources using their backup through terraform so that it will be automated
Just to have an overall understanding on how I can implement Terraform for future provisioning of resources in Azure and which are already provisioned
2
Answers
Here to achieve the requirement we can use two ways
Importing the resource:
As Rui Jarimba suggested Managing existing resources with Terraform requires importing them into Terraform’s state first. This is done by using the
terraform import
command, accompanied by the resource’s address and its ID in Azure. You must also write a Terraform configuration that corresponds to the existing resources. Each resource has to be imported individually with theterraform import
command.Future Provisioning
For new resources, you can write Terraform configurations as usual and apply them with
terraform apply
. This approach allows you to define the desired state of your infrastructure and let Terraform handle the provisioning and configurationTerraform configuration:
Deployment succeeded:
Yes. There are two methods. The first method would be to use the
terraform import
statement. The second method is to use the terraform blockIn either scenario you must have knowledge of the resource id from the resource provider prior to the import.
Using
import
in code is usually preferred with a PR based CI/CD or Gitops style deployments. For example you are backfilling some resources managed by hand. You can evaluate a string for the id but it must be known at import plan time. So you can pass in a variable, I have not tried using a data source, but that seems feasible if the data source is available during planning.Using the
terraform import
via CLI is beneficial for by hand scripting or in a scenario (like yours) where you might need to fetch the id and simply want to use your existing code with a different set of resources in a DR scenario. (Presumably you want to fail back)One thing to consider in a DR secario is make sure you are use a different key for your terraform state.
In theory you could pass the API calls via https://learn.microsoft.com/en-us/azure/developer/terraform/overview-azapi-provider, but I would not recommend it. This isn’t what terraform is designed to do. You really should have a runbook script for this day two type operations.