0
votes

I would like a guide on how to automatically deploy to a newly provisioned aks cluster after provisioning with terraform. for more context, i am building a one click full infrastructure provisioning and deployment all in one script. below is my structure for more understanding

main.tf

resource "azurerm_kubernetes_cluster" "aks" {
      name                = var.cluster_name
      kubernetes_version  = var.kubernetes_version
      location            = var.location
      resource_group_name = var.resource_group_name
      dns_prefix          = var.cluster_name
    
      default_node_pool {
        name                = "system"
        node_count          = var.system_node_count
        vm_size             = "Standard_DS2_v2"
        type                = "VirtualMachineScaleSets"
        availability_zones  = [1, 2, 3]
        enable_auto_scaling = false
      }
    
      identity {
        type = "SystemAssigned"
      }
    
      network_profile {
        load_balancer_sku = "Standard"
        network_plugin    = "kubenet"
      }
      role_based_access_control {
        enabled = true
      }
}

output.tf

resource "local_file" "kubeconfig" {
      depends_on = [azurerm_kubernetes_cluster.aks]
      filename   = "kubeconfig"
      content    = azurerm_kubernetes_cluster.aks.kube_config_raw
}

deployment.tf

resource "kubernetes_deployment" "sdc" {
      metadata {
        name = "sdc"
        labels = {
          app = "serviceName"
          #version = "v1.0"
        }
        namespace = "default"
      }
    
      spec {
        replicas = 1
        selector {
          match_labels = {
            app = "serviceName"
          }
        }
        template {
          metadata {
            labels = {
              app = "serviceName"
              # version = "v1.0"
            }
          }
          spec {
            container {
              image = "myImage"
              name  = "serviceName"
              port {
                container_port = 80
              }
            
            }
          }
        }
      }
      depends_on = [
        azurerm_kubernetes_cluster.aks
      ]
    }

Everything works perfectly, my kubeconfig file is created and downloaded. my major headache is how to make the terraform apply process use the kubeconfig file created and also run the deployment. making my terraform script fully automated. I basically want to provision and deploy into the newly provisioned cluster all in one run.

Looking forward to good help.

Thanks guys

That should be fairly easy if you have defined the required_providers with kubernetes and the provider "kuberentes" blocks properly. Do you have those? - Marko E
No I dont that, can you please show an example for more context - Ribo01
do you mind showing a sample use. this isn't so clear. or explain how to use in my case? - Ribo01
It's very simple: you need a kubeconfig file to "talk" to a Kubernetes cluster. Kubernetes API requires a context. Since you haven't defined the kubeconfig file and configured the kubernetes provider, terraform does not know in which cluster to create the Deployment object. It can't get much clearer than that. - Marko E