terraform azurerm_kubernetes_cluster nodes

10/2/2018

I tied different approaches to get list of vm's of azurerm_kubernetes_cluster in terraform but with no success. There is number of possible elements in here: https://www.terraform.io/docs/providers/azurerm/d/kubernetes_cluster.html but not of them seams to allow getting list of VM's. Is there a way?

-- Mateusz
azure-kubernetes
terraform-provider-azure

1 Answer

10/2/2018

Ok I found way via subnet. To use this approach you need to have Kubernetes cluster created with advanced networking with your subnet which you know.

First section gets ip_configurations from subnet and extracts network interfaces names with ugly split.

data "null_data_source" "all_kubernetes_nic_name" {
  count = "${length(azurerm_subnet.kubernetes.ip_configurations)}"
  inputs {
    nic = "${element(split("/", azurerm_subnet.kubernetes.ip_configurations[count.index]), 8)}"
  }
}

Because of each kubernetes node acquiring number of ip addresses I need to distinct on previous list.

data "null_data_source" "kubernetes_nic_name" {
  count = "${length(distinct(data.null_data_source.all_kubernetes_nic_name.*.outputs.nic))}"
  inputs {
    nic = "${element(distinct(data.null_data_source.all_kubernetes_nic_name.*.outputs.nic), count.index)}"
  }
}

Then it's easy to get exact reference to network interface of each node in kubernetes cluster. Note resource_group_name being extracted directly from cluster object.

data "azurerm_network_interface" "kubernetes_nic" {
  count = "${length(data.null_data_source.kubernetes_nic_name.*.outputs.nic)}"
  name = "${data.null_data_source.kubernetes_nic_name.*.outputs.nic[count.index]}"
  resource_group_name = "${azurerm_kubernetes_cluster.cluster.node_resource_group}"
}
-- Mateusz
Source: StackOverflow