terraformterraform-provider-azureinfrastructure-as-code

Terraform State file and Cloud Infra out of sync


I am new to Terraform and have been trying to figure this out. We have a cloud env which was created as part of development and testing. In the process, a lot of unwanted resources were created by the team. Eventually a Terraform code was created and a State file was also created. Somehow someone (or many) have added and deleted/destroyed resources as per their whims. As result, we now have some resources that are not part of the State file.

We are now trying to streamline the process and clean up the code when we realized that the existing critical resources will either be changed or destroyed. This is not going to work for us.

Example:

  1. Synapse: This was created manually and is critical for us. However, The State file does not recognize it (which i think is fine). I have create a synapse.tf with all necessary resources and have executed a Terraform Plan, in which it came to light that a new Synapse Account and its dependents will be created. We don't want that.

  2. module.network.azurerm_network_security_rule.appgw-subnet-nsg-block-bad-ips will be destroyed (because azurerm_network_security_rule.appgw-subnet-nsg-block-bad-ips is not in configuration) This we don't to happen as it will affect the network rules.

There are many other similar items which I am trying to address.

What are my options and what would be a good approach to clean up the State file?? Any suggestions and inputs would be helpful.


Solution

  • Have you tried using terraform import?

    $ terraform import azurerm_synapse_workspace.<name of Synapse Workspace> <the resource id of your Synapse workspace>
    

    This is assuming you are referring to a Synapse Workspace when you say 'Synapse.'

    First, write a .tf file describing the existing resource (as you have already done, it seems).

    Then, run the following command (replacing the dummy values with the correct values).

    $ terraform import azurerm_synapse_workspace.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Synapse/workspaces/workspace1