简体   繁体   中英

Terraform: Error: Invalid index operation

I am using Terraform 0.14 for to automate the creation of some Azure resources.

I am trying to create assign a pull role to an Azure Kubernetes cluster to pull images from an Azure container registry using a Managed system identity

Here is my code

Azure Kubernetes cluster ( main.tf file)

resource "azurerm_kubernetes_cluster" "akc" {
  name                = var.cluster_name
  location            = var.location
  resource_group_name = var.resource_group_name
  dns_prefix          = var.dns_prefix
  kubernetes_version  = var.kubernetes_version
  api_server_authorized_ip_ranges = var.api_server_authorized_ip_ranges

  identity {
    type = "SystemAssigned"
  }
}

Azure Kubernetes cluster ( outputs.tf file)

output "principal_id" {
  value = azurerm_kubernetes_cluster.akc.identity[0]["principal_id"]
}

Azure role assignment ( main.tf file)

# Create a role assignment
resource "azurerm_role_assignment" "ara" {
  scope                            = var.scope
  role_definition_name             = var.role_definition_name
  principal_id                     = var.principal_id
}

Test environment ( main.tf file)

# Create azure kubernetes cluster
module "azure_kubernetes_cluster" {
  source                   = "../modules/azure-kubernetes-cluster"
  cluster_name             = var.cluster_name
  location                 = var.location
  dns_prefix               = var.dns_prefix
  resource_group_name      = var.resource_group_name
  kubernetes_version       = var.kubernetes_version
  node_count               = var.node_count
  min_count                = var.min_count
  max_count                = var.max_count
  os_disk_size_gb          = "100"
  max_pods                 = "110"
  vm_size                  = var.vm_size
  aad_group_name           = var.aad_group_name
  vnet_subnet_id           = var.vnet_subnet_id
}

# Create azure container registry
module "azure_container_registry" {
  source                   = "../modules/azure-container-registry"
  container_registry_name  = var.container_registry_name
  resource_group_name      = var.resource_group_name
  location                 = var.location
  sku                      = var.sku
  admin_enabled            = var.admin_enabled
}

# Create azure role assignment
module "azure_role_assignment" {
  source                   = "../modules/azure-role-assignment"
  scope                    = module.azure_container_registry.acr_id
  role_definition_name     = var.role_definition_name
  principal_id             = module.azure_kubernetes_cluster.principal_id
}

However, when I run the terraform plan command, I get the error below:

Error: Invalid index operation

  on ../modules/aks-cluster/outputs.tf line 14, in output "principal_id":
  14:   value = azurerm_kubernetes_cluster.cluster.identity[0]["principal_id"]

Only attribute access is allowed here. Did you mean to access attribute
"principal_id" using the dot operator?

Trying to figure out the solution to this.

I later figured out the solution to the error. Some modifications were made in Terraform 0.12 and later versions on how index operations are called. So rather than this:

Azure Kubernetes cluster ( outputs.tf file)

output "principal_id" {
  value = azurerm_kubernetes_cluster.akc.identity[0]["principal_id"]
}

It will be this:

Azure Kubernetes cluster ( outputs.tf file)

output "principal_id" {
  value = azurerm_kubernetes_cluster.akc.identity.*.principal_id
}

And also instead of this:

Test environment ( main.tf file)

# Create azure role assignment
module "azure_role_assignment" {
  source                   = "../modules/azure-role-assignment"
  scope                    = module.azure_container_registry.acr_id
  role_definition_name     = var.role_definition_name
  principal_id             = module.azure_kubernetes_cluster.principal_id
}

It will be this:

Test environment ( main.tf file)

# Create azure role assignment
module "azure_role_assignment" {
  source                   = "../modules/azure-role-assignment"
  scope                    = module.azure_container_registry.acr_id
  role_definition_name     = var.role_definition_name
  principal_id             = module.azure_kubernetes_cluster.principal_id[0]
}

Resources : Invalid index when referencing output from module in 0.12

That's all

I hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM