I'm new to Terraform, I'm building Azure Infrastructure using Terraform.
Here's my main.tf
file.
module "azResourceGroup" {
source = "./Modules/ResourceGroup"
resource_group_name = var.resource_group_name
tags = var.tags
}
module "azStorageAccount" {
source = "./Modules/StorageAccount"
resource_group_name = var.resource_group_name
tags = var.tags
for_each = var.storage_account_list
storage_account_name = each.value.storage_account_name
account_kind = each.value.account_kind
account_tier = each.value.storage_account_tier
enable_https_traffic_only = each.value.enable_https_traffic_only
}
This is the variable used for Storage Account (from variables.tf
file) :
variable "storage_account_list" {
type = list(object({
storage_account_name = string
account_tier = string
account_kind = string
enable_https_traffic_only = bool
}))
}
And below is the value for the variable (from terraform.tfvars
file)
storage_account_list = [
{
storage_account_name = "my-storage"
account_tier = "Standard"
account_kind = "BlobStorage"
enable_https_traffic_only = false
}
]
On the terraform plan
command, I'm getting this below error. Please help me with this.
Error: Invalid for_each argument
│
│ on main.tf line 13, in module "azStorageAccount":
│ 13: for_each = var.storage_account_list
│ ├────────────────
│ │ var.storage_account_list is list of object with 1 element
│
│ The given "for_each" argument value is unsuitable: the "for_each" argument must be a map, or set of strings, and you have
│ provided a value of type list of object.
CodePudding user response:
You can't use a list of objects in for_each
. Only list of strings (no other type), or maps. So in your case you can change your list into map as follows:
module "azStorageAccount" {
source = "./Modules/StorageAccount"
resource_group_name = var.resource_group_name
tags = var.tags
for_each = {for idx, val in var.storage_account_list: idx => val}
storage_account_name = each.value.storage_account_name
account_kind = each.value.account_kind
account_tier = each.value.storage_account_tier
enable_https_traffic_only = each.value.enable_https_traffic_only
}