<p>In Terraform I wrote a resource that deploys to AKS. I want to apply the terraform changes multiple times, but don't want to have the error below. The system automatically needs to detect whether the resource already exists / is identical. Currently it shows me 'already exists', but I don't want it to fail. Any suggestions how I can fix this issue?</p>
<pre><code>│ Error: services &quot;azure-vote-back&quot; already exists
│
│   with kubernetes_service.example2,
│   on main.tf line 91, in resource &quot;kubernetes_service&quot; &quot;example2&quot;:
│   91: resource &quot;kubernetes_service&quot; &quot;example2&quot; {
</code></pre>
<pre class="lang-json prettyprint-override"><code>provider &quot;azurerm&quot; {
  features {}
}

data &quot;azurerm_kubernetes_cluster&quot; &quot;aks&quot; {
  name                = &quot;kubernetescluster&quot;
  resource_group_name = &quot;myResourceGroup&quot;
}

provider &quot;kubernetes&quot; {
  host = data.azurerm_kubernetes_cluster.aks.kube_config[0].host

  client_certificate     = base64decode(data.azurerm_kubernetes_cluster.aks.kube_config.0.client_certificate)
  client_key             = base64decode(data.azurerm_kubernetes_cluster.aks.kube_config.0.client_key)
  cluster_ca_certificate = base64decode(data.azurerm_kubernetes_cluster.aks.kube_config.0.cluster_ca_certificate)
}

resource &quot;kubernetes_namespace&quot; &quot;azurevote&quot; {
  metadata {
    annotations = {
      name = &quot;azurevote-annotation&quot;
    }

    labels = {
      mylabel = &quot;azurevote-value&quot;
    }

    name = &quot;azurevote&quot;
  }
}

resource &quot;kubernetes_service&quot; &quot;example&quot; {
  metadata {
    name = &quot;azure-vote-front&quot;
  }
  spec {
    selector = {
      app = kubernetes_pod.example.metadata.0.labels.app
    }
    session_affinity = &quot;ClientIP&quot;
    port {
      port        = 80
      target_port = 80
    }

    type = &quot;LoadBalancer&quot;
  }
}

resource &quot;kubernetes_pod&quot; &quot;example&quot; {
  metadata {
    name = &quot;azure-vote-front&quot;
    labels = {
      app = &quot;azure-vote-front&quot;
    }
  }

  spec {
    container {
        image = &quot;mcr.microsoft.com/azuredocs/azure-vote-front:v1&quot;
        name  = &quot;front&quot;
        env {
          name = &quot;REDIS&quot;
          value = &quot;azure-vote-back&quot;
        }
    }
  }
}

resource &quot;kubernetes_pod&quot; &quot;example2&quot; {
  metadata {
    name = &quot;azure-vote-back&quot;
    namespace = &quot;azure-vote&quot;
    labels = {
      app = &quot;azure-vote-back&quot;
    }
  }

  spec {
    container {
        image = &quot;mcr.microsoft.com/oss/bitnami/redis:6.0.8&quot;
        name  = &quot;back&quot;
        env {
          name = &quot;ALLOW_EMPTY_PASSWORD&quot;
          value = &quot;yes&quot;
        }
    }
  }
}

resource &quot;kubernetes_service&quot; &quot;example2&quot; {
  metadata {
    name = &quot;azure-vote-back&quot;
    namespace = &quot;azure-vote&quot;
  }
  spec {
    selector = {
      app = kubernetes_pod.example2.metadata.0.labels.app
    }
    session_affinity = &quot;ClientIP&quot;
    port {
      port        = 6379
      target_port = 6379
    }

    type = &quot;ClusterIP&quot;
  }
}

</code></pre>

<p>Thats the ugly thing with deploying thing inside Kubernetes with terraform....you will meet this nice errors from time to time and thats why it is not recommended to do it :/</p>
<p>You could try to just <a href="https://www.terraform.io/cli/commands/state/rm" rel="nofollow noreferrer">remove the record from the state file</a>:</p>
<p><code>terraform state rm 'kubernetes_service.example2'</code></p>
<p>Terraform now will no longer track this record and the good thing <strong>it will not be deleted</strong> on the remote system.</p>
<p>On the next run terraform then will recognise that this resource exists on the remote system and add the record to the state.</p>
