Skip to content

Weird behavior when using the lookup function in terraform 0.12.x #22267

@manniche

Description

@manniche

Description of the issue

I am trying to use the lookup function with a variable, which fails. At the same time, using the function on a local with similar characteristics succeeds. The error message given seems to imply different semantics than what is described in the documentation

Terraform version

0.12.2 and 0.12.5

Minimal example

Files to reproduce the issue

terraform.tfvars:

test_elem1 = {
  "instance-01" = {
    name = "foo"
  }
}

main.tf

variable "test_elem1" {
  type = "map"
}
locals {
  test_elem1 = var.test_elem1
  test_elem2 = {"instance-01" = {"name" = "bar"}}
}

Commands to reproduce the issue

sme@ada: ~/tmp/terraform_lookup_bug > terraform_0_12_2 init

Initializing the backend...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
sme@ada: ~/tmp/terraform_lookup_bug > terraform_0_12_2 console
> local.test_elem1
{
  "instance-01" = {
    "name" = "foo"
  }
}
> local.test_elem2
{
  "instance-01" = {
    "name" = "bar"
  }
}
> lookup(local.test_elem1, "instance-01")

>  
Error: Error in function call

  on <console-input> line 1:
  (source code not available)
    |----------------
    | local.test_elem1 is map of object with 1 element

Call to function "lookup" failed: lookup() can only be used with flat lists.

> lookup(local.test_elem2, "instance-01")
{
  "name" = "bar"
}
>  

Expected output

I was expecting the lookup on the variable to behave in the same way as the lookup on the local.

As an aside (and as mentioned in the top of this issue), I am puzzled by the error message coming from terraform: lookup() can only be used with flat lists., which to me differs in explanation to that of the documentation:

lookup retrieves the value of a single element from a map, given its key

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions