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

for_each causes Terraform crash #22560

Closed
rohrerb opened this issue Aug 22, 2019 · 2 comments
Closed

for_each causes Terraform crash #22560

rohrerb opened this issue Aug 22, 2019 · 2 comments
Labels
bug config crash v0.12 Issues (primarily bugs) reported against v0.12 releases

Comments

@rohrerb
Copy link

rohrerb commented Aug 22, 2019

Terraform Version

Terraform v0.12.6
+ provider.null v2.1.2

Terraform Configuration Files

locals {
  warehouse_dbs_metadata = {
    db1 = {
      edition           = "Standard",
      performance_level = "S0",
      firewall_rules = [
        { name = "test1", start_ip = "10.0.17.62", end_ip = "10.0.17.62" },
        { name = "test2", start_ip = "10.0.17.63", end_ip = "10.0.17.63" }
    ] },
    db2 = {
      edition           = "Standard",
      performance_level = "S0",
      firewall_rules = [
        { name = "test3", start_ip = "10.0.17.62", end_ip = "10.0.17.62" },
        { name = "test4", start_ip = "10.0.17.63", end_ip = "10.0.17.63" }
    ] }
  }

  nestedforeach_p = flatten([
    for w in keys(local.warehouse_dbs_metadata) : [
      for v in values(local.warehouse_dbs_metadata) : [
        for f in v.firewall_rules : [
           {
            key      = w,
            name     = f.name,
            start_ip = f.start_ip,
            end_ip   = f.end_ip
          }
        ]
      ]
    ]
  ])

}

output test {
  value = local.nestedforeach_p
}

resource "null_resource" "resource" {
  for_each = local.nestedforeach_p

  triggers = {
    name = each.key
  }
}

Crash Output

https://gist.github.com/rohrerb/3bc9f8de1be183986d298c8f39eaf0fe

Expected Behavior

Eight null_resource's would be created.

Actual Behavior

panic: not a string

goroutine 46 [running]:
github.com/zclconf/go-cty/cty.Value.AsString(...)
        /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/[email protected]/cty/value_ops.go:1026
github.com/zclconf/go-cty/cty.Value.AsValueMap(0x2e278a0, 0xc0005144a0, 0x25cba80, 0xc0005144c0, 0x8)
        /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/[email protected]/cty/value_ops.go:1095 +0x38a
github.com/hashicorp/terraform/terraform.evaluateResourceForEachExpressionKnown(0x2e26560, 0xc0004c5c20, 0x2e594c0, 0xc0000d2d00, 0xc0000e2dc0, 0xc0000e2e70, 0xc0000e2f20, 0x100b609, 0xc00065b6b8)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval_for_each.go:84 +0x5bd
github.com/hashicorp/terraform/terraform.(*EvalValidateResource).validateForEach(0xc000260870, 0x2e594c0, 0xc0000d2d00, 0x2e26560, 0xc0004c5c20, 0x0, 0xc0000bdef0, 0xc0000b6900)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval_validate.go:572 +0x59
github.com/hashicorp/terraform/terraform.(*EvalValidateResource).Eval(0xc000260870, 0x2e594c0, 0xc0000d2d00, 0x2, 0x2, 0x0, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval_validate.go:394 +0x1b57
github.com/hashicorp/terraform/terraform.EvalRaw(0x2de5e80, 0xc000260870, 0x2e594c0, 0xc0000d2d00, 0x0, 0x0, 0x0, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval.go:57 +0x131
github.com/hashicorp/terraform/terraform.(*EvalSequence).Eval(0xc000435b00, 0x2e594c0, 0xc0000d2d00, 0x2, 0x2, 0x1ad38c5, 0x2de5e80)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval_sequence.go:20 +0xfd
github.com/hashicorp/terraform/terraform.EvalRaw(0x2de5de0, 0xc000435b00, 0x2e594c0, 0xc0000d2d00, 0x26826a0, 0x3e03465, 0x25ef1a0, 0xc0005be560)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval.go:57 +0x131
github.com/hashicorp/terraform/terraform.Eval(0x2de5de0, 0xc000435b00, 0x2e594c0, 0xc0000d2d00, 0xc000435b00, 0x2de5de0, 0xc000435b00, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval.go:35 +0x4d
github.com/hashicorp/terraform/terraform.(*Graph).walk.func1(0x28e1c20, 0xc000432080, 0x0, 0x0, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/graph.go:90 +0xf40
github.com/hashicorp/terraform/dag.(*Walker).walkVertex(0xc000442700, 0x28e1c20, 0xc000432080, 0xc000548300)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/dag/walk.go:392 +0x353
created by github.com/hashicorp/terraform/dag.(*Walker).Update
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/dag/walk.go:314 +0xa9b



!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Terraform crashed! This is always indicative of a bug within Terraform.
A crash log has been placed at "crash.log" relative to your current
working directory. It would be immensely helpful if you could please
report the crash with Terraform[1] so that we can fix this.

When reporting bugs, please include your terraform version. That
information is available on the first line of crash.log. You can also
get it by running 'terraform --version' on the command line.

[1]: https://github.com/hashicorp/terraform/issues

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Steps to Reproduce

  1. terraform init
  2. terraform apply

Additional Context

Issue is also posted here > https://discuss.hashicorp.com/t/for-each-flattened-list/2494 and https://discuss.hashicorp.com/t/help-with-for-each-in-resource/2435/3

@ghost ghost added bug crash labels Aug 22, 2019
@hashibot hashibot added config v0.12 Issues (primarily bugs) reported against v0.12 releases labels Aug 22, 2019
@pselle
Copy link
Contributor

pselle commented Aug 26, 2019

This panic is now fixed in 0.12.7 -- the example code given here is passing for_each a tuple, which is not a supported type for for_each.

In order to create instances, for_each uses the keys of the map (or the set of strings) provided to identify the instances. If you'd like to identify instances by their index in the list, you can use count, but that has clear downsides as if you remove an item from the middle of the list, that will cause cascading changes to be needed.

Thank you for the report!

@pselle pselle closed this as completed Aug 26, 2019
@ghost
Copy link

ghost commented Sep 26, 2019

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 Sep 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug config crash v0.12 Issues (primarily bugs) reported against v0.12 releases
Projects
None yet
Development

No branches or pull requests

3 participants