Search code examples
azureterraformterraform-provider-azure

Can I manage my already created resources in Azure by Terraform


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


Solution

  • Can I manage my already created resources in Azure by Terraform

    Yes. There are two methods. The first method would be to use the terraform import statement. The second method is to use the terraform block

    import {
      to = resource.name
      id = "id.12345"
    }
    

    In 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.

    can I use terraform for using it as a tool to restore my backup files ( those RSV are already created)

    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.