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

Using "computed" values in maps not possible #7241

Closed
Alars-ALIT opened this issue Jun 20, 2016 · 10 comments
Closed

Using "computed" values in maps not possible #7241

Alars-ALIT opened this issue Jun 20, 2016 · 10 comments

Comments

@Alars-ALIT
Copy link

Terraform Version

Terraform v0.7.0-rc2

Affected Resource(s)

Terraform core

Terraform Configuration Files

resource "null_resource" "intermediates" {
    triggers = {
        test = "testing..."
    }
}


module "test_mod" {
  source = "./mod"

  services = {
    "name"             = "user-service"
    "min"              = "1"
    "max"              = "1"
    "desired"          = "1"
    "instance_type"    = "t2.small"
    "elb"              = "${null_resource.intermediates.triggers.test}" 
  }
}
variable "services"                { type = "list" }

resource "aws_autoscaling_group" "asg-test" {
  count                     = "${length(var.services)}"
  availability_zones        = ["test"]
  min_size                  = "${lookup(var.services[count.index], "min")}"
  max_size                  = "${lookup(var.services[count.index], "max")}"
  desired_capacity          = "${lookup(var.services[count.index], "desired")}"
  launch_configuration      = "any"
  vpc_zone_identifier       = ["any"]
  load_balancers            = ["${lookup(var.services[count.index], "elb")}"]  
}

Expected Behavior

Expected the map to contain a entry for "elb". I seems like "computed" values can't be passed in maps

Actual Behavior

Errors:

  • lookup: lookup failed to find 'elb' in:

${lookup(var.services[count.index], "elb")}

Steps to Reproduce

terraform plan

@jen20
Copy link
Contributor

jen20 commented Jun 24, 2016

Hi @Alars-ALIT! I can reproduce this - thanks for the concise example. I'll investigate what is going on here, as I see no reason in principle that this should not work.

@jen20
Copy link
Contributor

jen20 commented Jun 24, 2016

Note to self for investigative context - in interpolationFuncLookup:

2016/06/24 14:49:03 [INFO] args: ([]interface {}) (len=2 cap=2) {
 (map[string]ast.Variable) (len=5) {
  (string) (len=3) "min": (ast.Variable) {Variable (TypeString): 1},
  (string) (len=3) "max": (ast.Variable) {Variable (TypeString): 1},
  (string) (len=7) "desired": (ast.Variable) {Variable (TypeString): 1},
  (string) (len=13) "instance_type": (ast.Variable) {Variable (TypeString): t2.small},
  (string) (len=4) "name": (ast.Variable) {Variable (TypeString): user-service}
 },
 (string) (len=3) "elb"
}

@jen20
Copy link
Contributor

jen20 commented Jun 24, 2016

The issue here is that this value is not being passed through as a computed key.

@onlyanegg
Copy link

I'm seeing the same issue on 0.6.16. Is this related?

@jen20
Copy link
Contributor

jen20 commented Jun 27, 2016

@coutotyler Can you show a configuration for 0.6.16? I suspect this was always broken and people are discovering it now that maps are a more prominent feature. I have a fix in the works for the 0.7 at the moment.

@onlyanegg
Copy link

@ocsi01
Copy link

ocsi01 commented Jun 28, 2016

Is it possible at all ( in v0.7rc-2) to define a list of maps? ( If I understand it well, the 'services' variable is a list, where each element of the list is a map.)
The question is also open for nested maps.

@jen20
Copy link
Contributor

jen20 commented Jun 28, 2016

@ocsi01 Yes - the services variable in this example is a list of maps, where each element is a map. Nested maps are supported internally but are not exposed via HCL syntax at the moment.

@ocsi01
Copy link

ocsi01 commented Jun 28, 2016

@jen20 Does it mean at the moment ( v0.7rc-2) it's not possible to define multiple element in the 'services' list?
For example:

vpc_info          = [{      
"name"= "vpc1"
"cidr"= "10.3.0.0/16"
},{      
"name"= "vpc2"
"cidr"= "10.2.0.0/16"
}
]

I'm getting syntax error:

unexpected token while parsing list: LBRACE

As a workaround, I've found this syntax:

  vpc_info          = {      
          "name"= "vpc1"
          "cidr"= "10.3.0.0/16"
          }
  vpc_info          = {      
          "name"= "vpc1"
          "cidr"= "10.2.0.0/16"
          }

It is working well if I'm adding it as direct input to a module. However if I'm trying to pass it to an other module, I'm getting an error:

module "vpc" {
  source = "./vpc"
  vpc_info = "${var.vpc_info}"
}

cannot convert vpc_info2 value [map["cidr":"{Variable (TypeString): 10.3.0.0/16}" "name":"{Variable (TypeString): vpc1}"] map["name":"{Variable (TypeString): vpc2
}" "cidr":"{Variable (TypeString): 10.2.0.0/16}"]] to an ast.Variable for interpolation: value for conversion must be a string, interface{} or map[string]interface: got
ast.Variable

Any ideas?

jen20 added a commit that referenced this issue Jul 8, 2016
The reproduction of issue #7421 involves a list of maps being passed to
a module, where one or more of the maps has a value which is computed
(for example, from another resource). There is a failure at the point of
use (via lookup interpolation) of the computed value of the form:

```
lookup: lookup failed to find 'elb' in:
${lookup(var.services[count.index], "elb")}
```

Where 'elb' is the key of the map.
jen20 added a commit that referenced this issue Jul 8, 2016
The reproduction of issue #7421 involves a list of maps being passed to
a module, where one or more of the maps has a value which is computed
(for example, from another resource). There is a failure at the point of
use (via lookup interpolation) of the computed value of the form:

```
lookup: lookup failed to find 'elb' in:
${lookup(var.services[count.index], "elb")}
```

Where 'elb' is the key of the map.
jen20 added a commit that referenced this issue Jul 8, 2016
As part of evaluating a variable block, there is a pass made on unknown
keys setting them to the config.DefaultVariableValue sentinal value.
Previously this only took into account one level of nesting and assumed
all values were strings.

This commit now traverses the unknown keys via lists and maps and sets
unknown map keys surgically.

Fixes #7241.
jen20 added a commit that referenced this issue Jul 8, 2016
The reproduction of issue #7421 involves a list of maps being passed to
a module, where one or more of the maps has a value which is computed
(for example, from another resource). There is a failure at the point of
use (via lookup interpolation) of the computed value of the form:

```
lookup: lookup failed to find 'elb' in:
${lookup(var.services[count.index], "elb")}
```

Where 'elb' is the key of the map.
@jen20 jen20 closed this as completed in b6fff85 Jul 8, 2016
@ghost
Copy link

ghost commented Apr 24, 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 Apr 24, 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

4 participants