skip to Main Content

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?

│ Error: services "azure-vote-back" already exists
│
│   with kubernetes_service.example2,
│   on main.tf line 91, in resource "kubernetes_service" "example2":
│   91: resource "kubernetes_service" "example2" {
provider "azurerm" {
  features {}
}

data "azurerm_kubernetes_cluster" "aks" {
  name                = "kubernetescluster"
  resource_group_name = "myResourceGroup"
}

provider "kubernetes" {
  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 "kubernetes_namespace" "azurevote" {
  metadata {
    annotations = {
      name = "azurevote-annotation"
    }

    labels = {
      mylabel = "azurevote-value"
    }

    name = "azurevote"
  }
}

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

    type = "LoadBalancer"
  }
}

resource "kubernetes_pod" "example" {
  metadata {
    name = "azure-vote-front"
    labels = {
      app = "azure-vote-front"
    }
  }

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

resource "kubernetes_pod" "example2" {
  metadata {
    name = "azure-vote-back"
    namespace = "azure-vote"
    labels = {
      app = "azure-vote-back"
    }
  }

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

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

    type = "ClusterIP"
  }
}

2

Answers


  1. 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 :/

    You could try to just remove the record from the state file:

    terraform state rm 'kubernetes_service.example2'

    Terraform now will no longer track this record and the good thing it will not be deleted on the remote system.

    On the next run terraform then will recognise that this resource exists on the remote system and add the record to the state.

    Login or Signup to reply.
  2. I would like to add a bit to @Philip Welz‘s answer.

    The terraform state rm command is used to remove items from the Terraform state. This command can remove single resources, single instances of a resource, entire modules, and more. [1]

    (Just in case) To list all state:

    terraform state list
    

    According to the documentation, exactly as @Philip Welz mentioned, this command will cause Terraform to "forget" all of the instances of the kubernetes_service resource named "example2:

    terraform state rm 'kubernetes_service.example2'
    

    After all you should see:

    Successfully removed 1 resource instance(s).
    

    See also links:

    [1] Doc about Command: state rm

    [2] This question

    [3] This guide

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search