Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform --var-file "vars.json" will only read first submap in a nested map. #9239

Closed
dmikalova opened this issue Oct 5, 2016 · 6 comments
Milestone

Comments

@dmikalova
Copy link

Terraform Version

0.7.4

Affected Resource(s)

maps and var files.

Terraform Configuration Files

main.tf:

variable "map" {
  type = "map"

  default = {
    regiona = {
      amia = "main.tf"
      amib = "main.tf"
    }

    regionb = {
      amia = "main.tf"
      amib = "main.tf"
    }
  }
}

output "map" {
  value = "${var.map}"
}

vars.json:

{
    "map": {
        "regiona": {
            "amia": "vars.json",
            "amib": "vars.json"
        },
        "regionb": {
            "amia": "vars.json"
        },
        "regionc": {
            "amia": "vars.json",
            "amib": "vars.json"
        }
    }
}

Expected Behavior

Terraform should have merged the map from main.tf with the map and all of it's submaps declared in vars.json.

map = {
  regiona = [map[amia:vars.json amib:vars.json]]
  regionb = [map[amia:vars.json amib:maint.tf]]
  regionc = [map[amia:vars.json amib:vars.json]]
}

Actual Behavior

Terraform merges only the first submap from vars.json (map.regiona) and ignores any other maps (map.regionb, map.regionc). The first submap depends on the order they are declared in vars.json.

map = {
  regiona = [map[amia:vars.json amib:vars.json]]
  regionb = [map[amia:main.tf amib:maint.tf]]
}

Note that regiona was successfully overwritten, the change to regionb was not made, and regionc is completely missing.

Steps to Reproduce

terraform apply -var-file vars.json outputs the following:

Important Factoids

Nested maps work fine if declared in hcl, ie if regionc is moved from vars.json to main.tf this would work and is our current workaround.

@jbardin
Copy link
Member

jbardin commented Dec 13, 2016

Just an update, but this will now print an error in this case:

invalid value "vars.json" for flag -var-file: multiple map declarations not supported for variables

Because of the internal structure of HCL, the "correct" format for the JSON is actually:

{
    "map": [
        {
            "regiona": [
                {
                    "amia": "vars.a.a",
                    "amib": "vars.b.b"
                }
            ],
            "regionb": [
                {
                    "amia": "vars.b.a"
                }
            ]
        }
    ]
}

Not this isn't very intuitive, and we usually do try to accept the more condensed JSON as input, so I'll keep this open to try and fix that issue (as well as the less than helpful error message we now get).

@beelit94
Copy link

@jbardin

list of objects won't work, either. I assume it's the same issue.
May I ask for a workaround for this? I tried with the structure above but no luck.

variable "map" {
  type = "list"

  default = [
    {
      amia = "main.tf"
      amib = "main.tf"
    },
    {
      amia = "main.tf"
      amib = "main.tf"
    }
  ]
}

output "map" {
  value = "${var.map}"
}

json file

{
    "map": [
        {
            "amia": "vars.json",
            "amib": "vars.json"
        },   
        {
            "amia": "vars.json"
        }
    ]
}

expected

map = [
    {
        amia = main.tf,
        amib = main.tf
    },
    {
        amia = main.tf,
        amib = main.tf
    }
]

Actual

multiple map declarations not supported for variables

@jbardin
Copy link
Member

jbardin commented Dec 16, 2016

@beelit94,

Sorry, I missed that you had a list type rather than a map.

I think this is another edge case without a workaround at the moment. Fixing JSON input is something we're going to be working on soon.

@tamsky
Copy link
Contributor

tamsky commented Jan 16, 2017

@jbardin can you provide a working example for 2 or more variables declared in a json -var-file=?

I can get terraform to accept one json var value in a -var-file.

It's when I ask terraform to accept 2 (or more) variable declarations in a -var-file (v0.8.4),
it fails with the same message, but I'm not passing a map, just two simple variable = "string" values.

invalid value "vars.json" for flag -var-file: multiple map declarations not supported for variables

I'm wondering if it's my json formatting or?

@apparentlymart apparentlymart added config and removed core labels Nov 7, 2018
@apparentlymart apparentlymart added this to the v0.12.0 milestone Nov 7, 2018
@apparentlymart
Copy link
Contributor

Hi all,

I just verified this in v0.12.0-alpha2 using the same files as given (with the minor adjustment of specifying a more precise type (`map(map(string))`` for the variable to work around #19141):

$ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

map = {
  "regiona" = {
    "amia" = "main.tf"
    "amib" = "main.tf"
  }
  "regionb" = {
    "amia" = "main.tf"
    "amib" = "main.tf"
  }
}
$ terraform apply -var-file vars.tfvars.json

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

map = {
  "regiona" = {
    "amia" = "vars.json"
    "amib" = "vars.json"
  }
  "regionb" = {
    "amia" = "vars.json"
  }
  "regionc" = {
    "amia" = "vars.json"
    "amib" = "vars.json"
  }
}

The fix here comes from the switch to the new JSON parser implementation that is also now used for .tf files. This fix is already in master and ready to be included in the forthcoming v0.12.0 final release, and so I'm going to close this issue.

Sorry for the weird behavior, and for the long delay in getting it fixed.

@ghost
Copy link

ghost commented Mar 31, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Mar 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants