<p>I have a resource group on Azure which contains several resources that were originally created with a Terraform script. Somehow I deleted the Kubernetes cluster resource and also reset the TF state. My intention was recreating this AKS module, but now when I run the terraform script pipeline I get the error that the resource already exists for the following resources:</p>
<p><strong>module.keyvault.azurerm_key_vault_access_policy.service_principle_policy: Creating... module.keyvault.azurerm_key_vault_access_policy.users_policy: Creating... module.keyvault.azurerm_key_vault_access_policy.readers_policy: Creating... module.rg.azurerm_resource_group.rg: Creating... module.keyvault.azurerm_key_vault_access_policy.readers_policy: Creating...</strong></p>
<p>My question is, how could I recreate the AKS cluster while keeping the current resources?</p>
<p>Thanks in advance.</p>

Felipe
<p>As you have deleted the resources from state file, one possible way is to import the same resource via terraform import command like below:-</p>
<pre><code>terraform import module.keyvault.azurerm_key_vault_access_policy.service_principle_policy &lt;existing_key_vault_id&gt;/accesspolicies/&lt;policy_id&gt;
terraform import module.keyvault.azurerm_key_vault_access_policy.users_policy &lt;existing_key_vault_id&gt;/accesspolicies/&lt;policy_id&gt;
terraform import module.keyvault.azurerm_key_vault_access_policy.readers_policy &lt;existing_key_vault_id&gt;/accesspolicies/&lt;policy_id&gt;
terraform import module.rg.azurerm_resource_group.rg /subscriptions/&lt;subscription_id&gt;/resourceGroups/&lt;resource_group_name&gt;
</code></pre>
<p>Another  way is to get the terraform configuration of your existing state file and then add terraform configuration code blocks to match the existing state.</p>
<p>Check existing state like below:-</p>
<pre><code>terraform state list
</code></pre>
<p><img src="https://i.imgur.com/P9NzYzl.png" alt="enter image description here" /></p>
<p>Run terraform show command to check the existing configuration and add the code block of AKS that matches this configuration state.</p>
<pre><code>terraform show
</code></pre>
<p><img src="https://i.imgur.com/2uRuHiV.png" alt="enter image description here" /></p>
<p>After creating a configuration code for missing or already existing resources like I created one configuration block for NetWorkWatcherRG in my code and imported it in my tfstate :-</p>
<p><img src="https://i.imgur.com/vVX0joZ.png" alt="enter image description here" /></p>
<p><strong>Added the configuration block:-</strong></p>
<pre><code>resource &quot;azurerm_resource_group&quot; &quot;NetworkWatcherRG&quot; {
    name = &quot;NetworkWatcherRG&quot;
    location = var.resource_group_location
  
}
</code></pre>
<p><img src="https://i.imgur.com/afgKgHy.png" alt="enter image description here" /></p>
<p><strong>Reference:-</strong></p>
<p><a href="https://learn.microsoft.com/en-us/azure/aks/learn/quick-kubernetes-deploy-terraform?tabs=azure-cli" rel="nofollow noreferrer">Quickstart: Create an Azure Kubernetes Service (AKS) cluster by using Terraform - Azure Kubernetes Service | Microsoft Learn</a></p>
