An error for map variable after upgrading to Terraform 0.12.26


I’ve following error after upgrading from Terraform 0.11.14 to 0.12.26:

- provider “helm” (hashicorp/helm) 1.2.3
- provider “azurerm” (hashicorp/azurerm) 1.33.1
- provider “kubernetes” (hashicorp/kubernetes) 1.10.0
- provider “tls” (hashicorp/tls) 2.1.1

terraform plan -var-file myvalues.tfvars

Error message:

Error: Invalid index

  on line 196, in module "mod1":
 196:   ingress_host         = var.ingress["myval1"]
    | var.ingress is map of string with 1 element

The given key does not identify an element in this collection value.

Configuration files:

module "mod1" {
  source               = "../modules/mod1"
  ingress_host         = var.ingress["myval1"]   #I believe here's the offending line

variable "ingress" {
  type        = map(string)
  description = "Map of ingress hosts"

  default = {
    myval1    = ""
    myval2    = "
    myval3    = ""

myvalyes.tfvars file:

ingress={myval1="", myval2="", myval3=""}

Module mod1

variable "ingress_host" {
  type        = string
  default     = ""
  description = ""

And honestly, I don’t understand what’s wrong with the ‘var.ingress[“myval1”]’ line. The original line for Terraform 0.11.14 was:

ingress_host = "${var.ingress["myval1"]}"

so I can’t see any issue with new format.

Upgrade from 0.11.14 to 0.12.26 went fine without any issues reported by ‘terraform 0.12upgrade’ command. The problem is that I can’t create new environments using the new version of Terraform. I would like to make sure that I can declare variable (map of strings) and then pass particular values as strings to modules installed locally.

Could you help me with this issue, please?

Hi @rmandziarz,

The error message is reporting that var.ingress only has one element, which disagrees with both your default value (three elements) and the value in your myvalues.tfvars file (also three elements). This suggests to me that the variable is being overridden somehow, but I’m not sure how given what you’ve shared here.

Some ideas to start with:

  • Are there any other .tfvars files in the same directory that may be setting a different value for ingress?
  • If you grep in all of the files in the directory containing your module for ingress, do you see any mentions of it in surprising places that might be causing it to be set to only one element?
  • Does running Terraform without the -var-file myvalues.tfvars option change the behavior in a significant? I’d expect it to therefore be using the default value. If you still see a similar error in that case, please share it in full like you did with the error in your question.