Home > database >  The given key does not identify an element in this collection value for azure storage account primar
The given key does not identify an element in this collection value for azure storage account primar

Time:12-22

I am trying to pull the primary_access_key value from storage account foreach and assign to function app. However I am not able to get the primary_access_key value since it is not part of the foreach collection.

variables.tf

variable "stack_version"{
type = string
}
variable "functionappname"
{
type = string
}
variable "storage_list" {
    type = list
}
variable "rg" {
      type        = string
}
variable "loc" {
     type        = string
}

variables.tfvars

resourcegroupname = "test-rg"
location          = "eastus"
storage_list  = ["mystorageaccount"]
functionappname = "myfunctionapp"
stack_version   = { node = "~14" }

storage account module

resource "azurerm_storage_account" "storageaccount" {
  for_each = toset(var.storage_list) 
  name=each.value
  resource_group_name = var.rg
  location = var.loc
  account_tier = "Standard"
  account_replication_type = "GRS"
}

output.tf

output "sa_primary_access_key_out"{
    value = { for storageaccount in azurerm_storage_account.storageaccount : storageaccount.name =>  storageaccount.primary_access_key }
    sensitive = true   
}

output "sa_name_out"{
    value = { for storageaccount in azurerm_storage_account.storageaccount : storageaccount.name =>  storageaccount.name }
}

when I try to use storage account module output for Function app I am getting below error.

module "FUNAPP_StorageAccount" {
  source            = "../../modules/StorageAccount"
  storage_list      = var.storage_list
  resourcegroupname = module.ResourceGroup.rg_name_out
  location          = var.loc
}
module "FunctionApp" {
  depends_on = [
    module.ResourceGroup,
    module.StorageAccount
  ]
  source                            = "../../modules/FunctionApp"
  resourcegroupname                 = module.ResourceGroup.rg_name_out
  location                          = var.loc
  service_plan_id                   = data.azurerm_service_plan.shared-appservice-plan.id
  storageaccountname                = module.FUNAPP_StorageAccount.sa_name_out["mystorageaccount"]
  storage_account_access_key        = module.FUNAPP_StorageAccount.sa_primary_access_key_out["sa_primary_access_key_out"]
  functionappname                   = var.functionappname
  stack_version                     = var.stack_version                     
}

error:

│ Error: Invalid index
│ 
│   on main.tf line 45, in module "FunctionApp":
│   45:   storage_account_access_key        = module.FUNAPP_StorageAccount.sa_primary_access_key_out["primary_access_key"]
│     ├────────────────
│     │ module.FUNAPP_StorageAccount.sa_primary_access_key_out has a sensitive value
│ 
│ The given key does not identify an element in this collection value.

CodePudding user response:

You do not have any key called sa_primary_access_key_out in your code defined. Instead you have output call that way. It should be:

storage_account_access_key        = module.FUNAPP_StorageAccount.sa_primary_access_key_out["mystorageaccount"]
  • Related