<p>Current state:
I have all services within a cluster and under just one resource_group. My problem is that I have to push all the services every time and my deploy is getting slow.</p>
<p>What I want to do: I want to split every service within my directory so I can deploy it separately. Now I have a backend to each service, so that can have his own remote state and won't change things when I deploy. However, can I push still have all the services within the same resource_group? If yes, how can I achieve that? If I need to create a resource group for each service that I want to deploy separately, can I still use the same cluster?</p>
<p><em>main.tf</em></p>
<pre class="lang-yaml prettyprint-override"><code>provider &quot;azurerm&quot; {
  version = &quot;2.23.0&quot;
  features {}
}

resource &quot;azurerm_resource_group&quot; &quot;main&quot; {
  name     = &quot;${var.resource_group_name}-${var.environment}&quot;
  location = var.location

  timeouts {
    create = &quot;20m&quot;
    delete = &quot;20m&quot;
  }
}
resource &quot;tls_private_key&quot; &quot;key&quot; {
  algorithm = &quot;RSA&quot;
}

resource &quot;azurerm_kubernetes_cluster&quot; &quot;main&quot; {

  name                            = &quot;${var.cluster_name}-${var.environment}&quot;
  location                        = azurerm_resource_group.main.location
  resource_group_name             = azurerm_resource_group.main.name
  dns_prefix                      = &quot;${var.dns_prefix}-${var.environment}&quot;
  node_resource_group             = &quot;${var.resource_group_name}-${var.environment}-worker&quot;
  kubernetes_version = &quot;1.18.6&quot;

  linux_profile {
    admin_username = var.admin_username

    ssh_key {
      key_data = &quot;${trimspace(tls_private_key.key.public_key_openssh)} ${var.admin_username}@azure.com&quot;
    }
  }

  default_node_pool {
    name            = &quot;default&quot;
    node_count      = var.agent_count
    vm_size         = &quot;Standard_B2s&quot;
    os_disk_size_gb = 30
  }

  role_based_access_control {
    enabled = &quot;false&quot;
  }

  addon_profile {
    kube_dashboard {
      enabled = &quot;true&quot;
    }
  }

  network_profile {
    network_plugin    = &quot;kubenet&quot;
    load_balancer_sku = &quot;Standard&quot;
  }

  timeouts {
    create = &quot;40m&quot;
    delete = &quot;40m&quot;
  }

  service_principal {
    client_id     = var.client_id
    client_secret = var.client_secret
  }

  tags = {
    Environment = &quot;Production&quot;
  }
}

provider &quot;kubernetes&quot; {
  version          = &quot;1.12.0&quot;
  load_config_file = &quot;false&quot;

  host = azurerm_kubernetes_cluster.main.kube_config[0].host

  client_certificate = base64decode(
    azurerm_kubernetes_cluster.main.kube_config[0].client_certificate,
  )

  client_key = base64decode(azurerm_kubernetes_cluster.main.kube_config[0].client_key)
  cluster_ca_certificate = base64decode(
    azurerm_kubernetes_cluster.main.kube_config[0].cluster_ca_certificate,
  )
}
</code></pre>
<p>backend.tf (for main)</p>
<pre><code>terraform {
  backend &quot;azurerm&quot; {}
}
</code></pre>
<p>client.tf (service that I want to deploy separately)</p>
<pre><code>resource &quot;kubernetes_deployment&quot; &quot;client&quot; {
  metadata {
    name = &quot;client&quot;

    labels = {
      serviceName = &quot;client&quot;
    }
  }

  timeouts {
    create = &quot;20m&quot;
    delete = &quot;20m&quot;
  }

  spec {

    progress_deadline_seconds = 600

    replicas = 1

    selector {
      match_labels = {
        serviceName = &quot;client&quot;
      }
    }

    template {
      metadata {
        labels = {
          serviceName = &quot;client&quot;
        }
      }
      }
    }
  }
}

resource &quot;kubernetes_service&quot; &quot;client&quot; {
  metadata {
    name = &quot;client&quot;
  }

  spec {
    selector = {
      serviceName = kubernetes_deployment.client.metadata[0].labels.serviceName
    }

    port {
      port        = 80
      target_port = 80
    }
  }
}

</code></pre>
<p>backend.tf (for client)</p>
<pre><code>terraform {
  backend &quot;azurerm&quot; {
    resource_group_name = &quot;test-storage&quot;
    storage_account_name = &quot;test&quot;
    container_name = &quot;terraform&quot;
    key=&quot;test&quot;
  }
}

</code></pre>
<p>deployment.sh</p>
<pre><code>terraform -v
terraform init \
    -backend-config=&quot;resource_group_name=$TF_BACKEND_RES_GROUP&quot; \
    -backend-config=&quot;storage_account_name=$TF_BACKEND_STORAGE_ACC&quot; \
    -backend-config=&quot;container_name=$TF_BACKEND_CONTAINER&quot; \

terraform plan
    
terraform apply -target=&quot;azurerm_resource_group.main&quot; -auto-approve \
    -var &quot;environment=$ENVIRONMENT&quot; \
    -var &quot;tag_version=$TAG_VERSION&quot; \
</code></pre>
<p>PS: I can build the test resource-group from scratch if needed. Don't worry about his current state.</p>
<p>PS2: The state files are being saved into the right place, no issue about that.</p>

Arthur Costa
<p>If you want to deploy resources separately, you could take a look at <code>terraform apply</code> with this option.</p>
<pre><code>  -target=resource       Resource to target. Operation will be limited to this
                         resource and its dependencies. This flag can be used
                         multiple times.
</code></pre>
<p>For example, just deploy a resource group and its dependencies like this,</p>
<pre><code>terraform apply -target=&quot;azurerm_resource_group.main&quot;
</code></pre>
