0
votes

I am deploying my infra with terraform, but for AKS I use ARM templating because it has some features that are not in TF yet. So in my tf template I have the following resource defined to deploy an ARM template:

resource "azurerm_template_deployment" "k8s" {
  name                  = "${var.environment}-aks-deployment"
  resource_group_name   = "${azurerm_resource_group.kubernetes.name}"

  parameters              = {
    workspaceResourceId = "${azurerm_log_analytics_workspace.k8s-law.id}"
    aksClusterName      = "fntm-k8s-${var.environment}"
    subnetKubernetes    = "${azurerm_subnet.kubernetes.id}"
    servicePrincipal    = "${azuread_service_principal.k8s_sp.application_id}"
    clientSecret        = "${random_string.sp_password.result}"
    clientAppID         = "${var.clientAppID}"
    serverAppID         = "${var.serverAppID}"
    tenantID            = "${var.tenant_id}"
    serverAppSecret     = "${var.serverAppSecret}"
  }

  template_body         = "${file("kubernetes/azuredeploy.json")}"

    deployment_mode = "Incremental"
}

The deployment of the cluster goes fine, but after that I need to get data from the AKS cluster which will be used by a different module. If I use the data resource for AKS it tries to get the cluster data before it is deployed. So the below part doesn't work.

data "azurerm_kubernetes_cluster" "kubernetes" {
  name                = "fntm-k8s-${var.environment}"
  resource_group_name = "${azurerm_resource_group.kubernetes.name}"
}

I thought maybe a depends_on but that is not supported in the data resource.

Anybody maybe an idea how I can get the data attribute node_resource_group from the AKS cluster with output? Or any other thoughts/solutions?

output "k8s_resource_group" {
  value = "${lookup(azurerm_template_deployment.k8s.outputs, "?????")}"
}
1
Can you also share your azuredeploy.json file?ydaetskcoR
@ydaetskcoR pastebin.com/cFMJfmDybramvdk
I tried some more to get it to work: I added this output to my json: "outputs": { "aksClusterName": { "type": "string", "value": "[parameters('aksClusterName')]" } }bramvdk
And then in my cluster.tf I did this: output "aksClusterName" { value = "${lookup(azurerm_template_deployment.k8s.outputs, "aksClusterName")}" depends_on = ["azurerm_template_deployment.k8s"] } data "azurerm_kubernetes_cluster" "kubernetes" { name = "fntm-k8s-${var.environment}" resource_group_name = "${azurerm_resource_group.kubernetes.name}" }bramvdk
But then i get the error that managed resource "output" has not been declared.bramvdk

1 Answers

0
votes

In your azuredeploy.json use this for the output:

"outputs": {
    "aksClusterName": {
        "type": "string",
        "value": "[parameters('aksClusterName')]"
    }
}

And in your tf file use:

output "aksClusterName" {
  value = "${azurerm_template_deployment.k8s.outputs["aksClusterName"]}"
}

data "azurerm_kubernetes_cluster" "kubernetes" {
  name                = ""
  resource_group_name = "${azurerm_resource_group.kubernetes.name}"
}

output "k8s_resource_group" {
  value = "${data.azurerm_kubernetes_cluster.kubernetes.node_resource_group}"
}