hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.53k stars 4.6k forks source link

BUG: resource output with count index stopped working when trying to upgrade from azurerm 2.x to 3.x #16608

Open luckeyca opened 2 years ago

luckeyca commented 2 years ago

Is there an existing issue for this?

Community Note

When trying to upgrade from azurerm 2.x(tested 2.73.0 and 2.99.0) to 3.x(all current releases tested), resource output with count index stopped working. This does NOT happen when upgrading within 2.x(tested from 2.73.0 to 2.99.0) or creating new resource using 3.x(tested 3.0.0 to 3.4.0)

Terraform Version

1.1.9

AzureRM Provider Version

3.0.0-3.4.0

Affected Resource(s)/Data Source(s)

azurerm_kubernetes_cluster

Terraform Configuration Files

terraform {
  required_version = "~> 1.1.9"

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 2.73.0"
    }
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "~> 2.11.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "aksrg" {
  name     = "testrg"
  location = "canadacentral"
}

resource "azurerm_kubernetes_cluster" "aks" {
  count = 1

  name               = "testaks"
  kubernetes_version = "1.23.5"

  location            = "canadacentral"
  resource_group_name = azurerm_resource_group.aksrg.name

  dns_prefix = "testaks"

  default_node_pool {
    name                 = "agentpool"
    orchestrator_version = "1.23.5"
    vm_size              = "Standard_D4s_v3"
    type                 = "VirtualMachineScaleSets"
    enable_auto_scaling  = false
    node_count           = 1
    min_count            = null
    max_count            = null

    os_disk_size_gb = 30

  }

  identity {
    type = "SystemAssigned"
  }
}

provider "kubernetes" {
  host = azurerm_kubernetes_cluster.aks[0].kube_config.0.host

  client_key             = base64decode(azurerm_kubernetes_cluster.aks[0].kube_config.0.client_key)
  client_certificate     = base64decode(azurerm_kubernetes_cluster.aks[0].kube_config.0.client_certificate)
  cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.aks[0].kube_config.0.cluster_ca_certificate)
}

resource "kubernetes_namespace" "namespaces" {
  count = 1
  metadata {
    name = "testnamespace"
  }
}

Debug Output/Panic Output

Error:

value = azurerm_kubernetes_cluster.aks[0].kube_config.0.host
│     ├────────────────
│     │ azurerm_kubernetes_cluster.aks is empty tuple

Error: Get "http://localhost/api/v1/namespaces/testnamespace": dial tcp [::1]:80: connect: connection refused
│ 
│   with kubernetes_namespace.namespaces[0],
│   on main.tf line 44, in resource "kubernetes_namespace" "namespaces":
│   44: resource "kubernetes_namespace" "namespaces" {

Expected Behaviour

since the same code works when upgrading resource from 2.73.0 to 2.99.0 AND works when creating brand new resource using 3.x(3.0.0-3.4.0) providers, it should work when upgrading the provider version from 2.x to 3.x

Actual Behaviour

failed with the following error. It seems that when upgrading from 2.x to 3.x, if the resource has a count.index, the outputs are not working correctly. after removing the count index for testing, upgrade from 2.x to 3.x went fine.

Error:

value = azurerm_kubernetes_cluster.aks[0].kube_config.0.host │ ├──────────────── │ │ azurerm_kubernetes_cluster.aks is empty tuple

Error: Get "http://localhost/api/v1/namespaces/testnamespace": dial tcp [::1]:80: connect: connection refused │ │ with kubernetes_namespace.namespaces[0], │ on main.tf line 44, in resource "kubernetes_namespace" "namespaces": │ 44: resource "kubernetes_namespace" "namespaces" {

Steps to Reproduce

use the code(providers.tf and main.tf) in the configuration file section above,

  1. set the azurerm provider version to anything below 2.99.0 to create a new aks
  2. (OPTIONAL). set the azurerm provider version to 2.99.0 and run plan/apply. this will WORK fine
  3. set the azurerm provider version to 3.x(tested from 3.0.0 to 3.4.0 with same results), both plan and apply will fail with the error mentioned above.

Important Factoids

No response

References

No response

luckeyca commented 2 years ago

Any update?