hashicorp / terraform-plugin-sdk

Terraform Plugin SDK enables building plugins (providers) to manage any service providers or custom in-house solutions
https://developer.hashicorp.com/terraform/plugin
Mozilla Public License 2.0
438 stars 232 forks source link

panic: Call to function "flatten" failed: panic in function implementation: can't use ElementIterator on null value #178

Closed dekimsey closed 2 years ago

dekimsey commented 5 years ago

Terraform Version

terraform -v
Terraform v0.12.9
+ provider.aws v2.28.1
+ provider.azuread v0.3.1
+ provider.null v2.1.2
+ provider.random v2.1.2
+ provider.template v2.1.2
+ provider.tls v2.0.1

Terraform Configuration Files

Our module is a thin wrapper over the ec2-instance one from the terraform registry. Here's our invocation of the module.

module "this" {
  source  = "terraform-aws-modules/ec2-instance/aws"
  version = "~> 2.8.0"
  name    = local.cluster_name

  ami           = coalesce(var.app_ami, data.aws_ami.centos7.id)
  instance_type = var.app_instance_type

  subnet_ids = var.app_subnets

  monitoring     = "true"
  use_num_suffix = "true"

  vpc_security_group_ids = concat(
    var.app_security_groups,
    [aws_security_group.this-app.id],
)

  iam_instance_profile = aws_iam_instance_profile.this.id
  user_data            = coalesce(var.user_data, data.template_file.cloudinit.rendered)
  key_name             = var.prov_key_name

  instance_count = var.app_instance_count

  ebs_optimized    = substr(var.app_instance_type, 0, 1) != "t" ? true : false
  ebs_block_device = var.ebs_block_device

  root_block_device = [
    {
      # CentOS doesn't delete volumes by default, let's add that.
      volume_size           = var.root_volume_size
      volume_type           = "gp2"
      delete_on_termination = true
    },
  ]

  tags = {
    ansible_group = var.name
    namespace     = var.namespace
    terraform     = "true"
  }
}

Debug Output

Crash Output

No crash.log file generated

Expected Behavior

The aws_instance resource should have been successfully imported (or gracefully failed).

Actual Behavior

$ terraform import module.elasticsearch-high-node.module.this.aws_instance.this[0] i-04935a3a14146c837
module.elasticsearch-high-node.module.this.aws_instance.this[0]: Importing from ID "i-04935a3a14146c837"...
module.elasticsearch-high-node.module.this.aws_instance.this[0]: Import prepared!
  Prepared aws_instance for import
module.elasticsearch-high-node.module.this.aws_instance.this[0]: Refreshing state... [id=i-04935a3a14146c837]

Error: Error in function call

  on .terraform/modules/elasticsearch-high-master.this/terraform-aws-modules-terraform-aws-ec2-instance-9fe491f/outputs.tf line 14, in locals:
  14:   this_credit_specification         = flatten(aws_instance.this.*.credit_specification)
    |----------------
    | aws_instance.this is tuple with 3 elements

Call to function "flatten" failed: panic in function implementation: can't use
ElementIterator on null value
goroutine 7956 [running]:
runtime/debug.Stack(0xc004e65eb0, 0x25e08a0, 0x2d9a450)
        /opt/goenv/versions/1.12.9/src/runtime/debug/stack.go:24 +0x9d
github.com/zclconf/go-cty/cty/function.errorForPanic(...)
        /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.0.1-0.20190708163926-19588f92a98f/cty/function/error.go:44
github.com/zclconf/go-cty/cty/function.Function.ReturnTypeForValues.func1(0xc004e663a8,
0xc004e663b8)
        /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.0.1-0.20190708163926-19588f92a98f/cty/function/function.go:193
+0x79
panic(0x25e08a0, 0x2d9a450)
        /opt/goenv/versions/1.12.9/src/runtime/panic.go:522 +0x1b5
github.com/zclconf/go-cty/cty.Value.ElementIterator(0x2e0e7e0, 0xc005466420,
0x0, 0x0, 0x100be98, 0x30)
        /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.0.1-0.20190708163926-19588f92a98f/cty/value_ops.go:922
+0xd5
github.com/hashicorp/terraform/lang/funcs.flattener(0x2e0e7e0, 0xc005466420,
0x0, 0x0, 0xc006f56ff0, 0x2e0e7e0, 0xc005466420, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/funcs/collection.go:488
+0x8c
github.com/hashicorp/terraform/lang/funcs.flattener(0x2e0e8e0, 0xc002c20460,
0x25bcb40, 0xc002c204a0, 0x1, 0x3, 0xc002c204a0, 0xc004e661a8)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/funcs/collection.go:495
+0x28f
github.com/hashicorp/terraform/lang/funcs.glob..func19(0xc002c20440, 0x1, 0x1,
0xc004e663b8, 0xc004e661f8, 0x0, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/funcs/collection.go:457
+0xa7
github.com/zclconf/go-cty/cty/function.Function.ReturnTypeForValues(0xc0004768a0,
0xc002c20440, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.0.1-0.20190708163926-19588f92a98f/cty/function/function.go:197
+0x282
github.com/zclconf/go-cty/cty/function.Function.Call(0xc0004768a0,
0xc002c20440, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.0.1-0.20190708163926-19588f92a98f/cty/function/function.go:204
+0x9d
github.com/hashicorp/hcl2/hcl/hclsyntax.(*FunctionCallExpr).Value(0xc000ec9a40,
0xc002712a00, 0x0, 0xc004e67700, 0x1, 0x1, 0x0, 0x0, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/hashicorp/hcl2@v0.0.0-20190821123243-0c888d1241f6/hcl/hclsyntax/expression.go:385
+0x10c8
github.com/hashicorp/terraform/lang.(*Scope).EvalExpr(0xc004e34550, 0x2e0d520,
0xc000ec9a40, 0x2e0e7a0, 0x3f3e6a0, 0x0, 0x0, 0xc0067ff908, 0x1, 0x1, ...)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/eval.go:95
+0x1b3
github.com/hashicorp/terraform/terraform.(*BuiltinEvalContext).EvaluateExpr(0xc0030cea90,
0x2e0d520, 0xc000ec9a40, 0x2e0e7a0, 0x3f3e6a0, 0x0, 0x0, 0x0, 0xc007be3080,
0x0, ...)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval_context_builtin.go:279
+0x1e4
github.com/hashicorp/terraform/terraform.(*EvalLocal).Eval(0xc0024a42c0,
0x2e40020, 0xc0030cea90, 0x2, 0x2, 0x1b353dd, 0x26f2820)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval_local.go:43
+0x73a
github.com/hashicorp/terraform/terraform.EvalRaw(0x2dcd080, 0xc0024a42c0,
0x2e40020, 0xc0030cea90, 0x2675140, 0x3df3767, 0x25e08a0, 0xc002bcadc0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval.go:57
+0x131
github.com/hashicorp/terraform/terraform.Eval(0x2dcd080, 0xc0024a42c0,
0x2e40020, 0xc0030cea90, 0xc0024a42c0, 0x2dcd080, 0xc0024a42c0, 0x0)
        /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval.go:35
+0x4d
github.com/hashicorp/terraform/terraform.(*Graph).walk.func1(0x2803980,
0xc003afcd50, 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(0xc00306c180,
0x2803980, 0xc003afcd50, 0xc004422d40)
        /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
.

Steps to Reproduce

$ terraform import module.elasticsearch-high-node.module.this.aws_instance.this[0] i-04935a3a14146c837

Additional Context

I'm in the midst of a 0.12 upgrade. All instances of this module trigger the exception during the import/state refresh. Manually commenting out this one case seems to address it. I'm guessing this is an error in the module too. But functions generating panic's seem odd

References

https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/tree/v2.8.0

teamterraform commented 5 years ago

Hi @dekimsey! Sorry for this crash and thanks for reporting it.

Indeed, this should not be reported as a crash. It looks like this might be a situation where the function ought to report a proper error though: some of the lists in the given list seem to be null, and thus cannot be flattened.

In order to avoid that problem (whether it's reported as a crash or as a proper error), you could try using a for expression to filter out the nulls, if skipping them is an appropriate result in your case:

  this_credit_specification         = flatten([
    for cs in aws_instance.this.*.credit_specification : cs if cs != null
  ])

...though we understand that this problem is coming from a child module that you might not be able to edit directly yourself.

When we look into this bug we'll consider whether it would be safe and clear for flatten to consider a null list as being the same as an empty list, which might avoid the need for that explicit filter. We tend to be cautious about special implicit behavior like that because it can make Terraform's behavior feel unpredictable and surprising to those who aren't aware of the details, but the tradeoff might be worth it in this case if real-world resource types tend to be returning null lists rather than empty lists.

(This might instead be considered an SDK bug... if credit_specification is a block type, then our usual convention would be for it to never be null and always be a list. Terraform 0.11 didn't have null, so we expected that forcing all block types to appear in expressions as non-null lists would be the most compatible with Terraform 0.11, but that's only true if the SDK preserves to that assumption. We'll investigate that when we look into this, too.)

dekimsey commented 5 years ago

Thanks for the evaluation @teamterraform! I too felt like there may be more than one issue here. :) Sorry I couldn't provide a more streamlined test. It seems to only happen during an import. State refreshing worked fine.

xpehter commented 4 years ago

The same execution error

terraform {
  required_version = "0.12.24"
  required_providers {
    digitalocean = "1.15.1"
    local        = "1.4.0"
    null         = "2.1.2"
  }
}

provider "digitalocean" {
  token = file(var.do_token_path)
}

resource "digitalocean_droplet" "stend" {
  image    = var.image_id
  name     = var.droplet_name
  region   = var.region
  size     = var.droplet_size
  ssh_keys = ["${data.digitalocean_ssh_key.my_key.fingerprint}"]
}

data "digitalocean_ssh_key" "my_key" {
  name = var.ssh_key_name
}

resource "local_file" "ansible_inventory" {
  content = templatefile("${path.module}/${var.templatefile_path}", {
    ip_addr = "${digitalocean_droplet.stend.ipv4_address}"
  })
  filename             = "${path.module}${var.ans_dir}${var.inventory}"
  directory_permission = "0775"
  file_permission      = "0664"
}

resource "null_resource" "ansible" {
  triggers = {
    instance_ids  = join(", ", compact(flatten(values(digitalocean_droplet.stend))))
    inventory_ids = join(", ", compact(flatten(values(local_file.ansible_inventory))))
  }
  provisioner "local-exec" {
    command = "echo bla-bla"
  }
}
[user@comp ~]$ terraform apply
data.digitalocean_ssh_key.my_key: Refreshing state...

Error: Error in function call

  on main.tf line 37, in resource "null_resource" "ansible":
  37:     instance_ids  = join(", ", compact(flatten(values(digitalocean_droplet.stend))))
    |----------------
    | digitalocean_droplet.stend is object with 27 attributes

Call to function "flatten" failed: panic in function implementation: can't use
ElementIterator on null value
goroutine 295 [running]:
runtime/debug.Stack(0xc00037a7a8, 0x1a9dda0, 0x22f7c30)
    /opt/goenv/versions/1.12.13/src/runtime/debug/stack.go:24 +0x9d
github.com/zclconf/go-cty/cty/function.errorForPanic(...)
    /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.2.1/cty/function/error.go:44
github.com/zclconf/go-cty/cty/function.Function.Call.func1(0xc00037acf0,
0xc00037ad10)
    /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.2.1/cty/function/function.go:287
+0x8f
panic(0x1a9dda0, 0x22f7c30)
    /opt/goenv/versions/1.12.13/src/runtime/panic.go:522 +0x1b5
github.com/zclconf/go-cty/cty.Value.ElementIterator(0x236fd00, 0xc00050fb70,
0x0, 0x0, 0xb000000000000000, 0xc0003a8098)
    /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.2.1/cty/value_ops.go:1038
+0x103
github.com/hashicorp/terraform/lang/funcs.flattener(0x236fd00, 0xc00050fb70,
0x0, 0x0, 0xc0007c7201, 0x236fd00, 0xc00050fb70, 0x0)
    /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/funcs/collection.go:488
+0x8c
github.com/hashicorp/terraform/lang/funcs.flattener(0x236fd40, 0xc000699ba0,
0x1a78780, 0xc000699bc0, 0x1b, 0x3523918, 0xc00037aa98, 0x42afff)
    /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/funcs/collection.go:495
+0x2b3
github.com/hashicorp/terraform/lang/funcs.glob..func21(0xc000699ae0, 0x1, 0x1,
0x236fc00, 0x3523918, 0x3523918, 0x0, 0x0, 0x1, 0xc00037ab90, ...)
    /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/funcs/collection.go:474
+0xa7
github.com/zclconf/go-cty/cty/function.Function.Call(0xc0003fa630,
0xc000699ae0, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.2.1/cty/function/function.go:291
+0x51c
github.com/hashicorp/hcl/v2/hclsyntax.(*FunctionCallExpr).Value(0xc0004ec2d0,
0xc000699420, 0x0, 0xc000699ac0, 0xc0003fa510, 0x1, 0x1, 0x1, 0xc00037bf88)
    /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.0/hclsyntax/expression.go:398
+0x10fd
github.com/hashicorp/hcl/v2/hclsyntax.(*FunctionCallExpr).Value(0xc0004ec3c0,
0xc000699420, 0x0, 0xc00039dab0, 0x236fbc0, 0xc00003e3e9, 0x236fbc0,
0xc00003e3e9, 0x1a9dda0)
    /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.0/hclsyntax/expression.go:364
+0x2136
github.com/hashicorp/hcl/v2/hclsyntax.(*FunctionCallExpr).Value(0xc0004ec4b0,
0xc000699420, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.0/hclsyntax/expression.go:364
+0x2136
github.com/hashicorp/hcl/v2/hclsyntax.(*ObjectConsExpr).Value(0xc000452a00,
0xc000699420, 0x0, 0x8, 0xc00061be98, 0x1, 0x0, 0x0, 0x1e79f42)
    /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.0/hclsyntax/expression.go:744
+0x288
github.com/hashicorp/hcl/v2/hcldec.(*AttrSpec).decode(0xc000a39500,
0xc0004f7980, 0x0, 0x0, 0x0, 0xc000699420, 0x30, 0x30, 0xc0007c6c60,
0x7ff118c91008, ...)
    /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.0/hcldec/spec.go:205
+0x2a9
github.com/hashicorp/hcl/v2/hcldec.ObjectSpec.decode(0xc000a394d0,
0xc0004f7980, 0x0, 0x0, 0x0, 0xc000699420, 0xc00037eca8, 0xc0007c1a70, 0x20,
0x20, ...)
    /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.0/hcldec/spec.go:79
+0x239
github.com/hashicorp/hcl/v2/hcldec.decode(0x236ef80, 0xc0006999a0, 0x0, 0x0,
0x0, 0xc000699420, 0x236f980, 0xc000a394d0, 0x0, 0x0, ...)
    /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.0/hcldec/decode.go:21
+0x118
github.com/hashicorp/hcl/v2/hcldec.Decode(...)
    /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.0/hcldec/public.go:15
github.com/hashicorp/terraform/lang.(*Scope).EvalBlock(0xc0003e0550,
0x236e7c0, 0xc000a394a0, 0xc0003d6b70, 0x1, 0x1, 0x0, 0x0, 0x0, 0xc0001aef40,
...)
    /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/eval.go:68
+0x2aa
github.com/hashicorp/terraform/terraform.(*BuiltinEvalContext).EvaluateBlock(0xc0007ca1a0,
0x236e7c0, 0xc000a394a0, 0xc0003d6b70, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
    /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval_context_builtin.go:272
+0x2b6
github.com/hashicorp/terraform/terraform.(*EvalDiff).Eval(0xc000156620,
0x23a49e0, 0xc0007ca1a0, 0x2, 0x2, 0x0, 0x0)
    /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval_diff.go:137
+0x273
github.com/hashicorp/terraform/terraform.EvalRaw(0x232bcc0, 0xc000156620,
0x23a49e0, 0xc0007ca1a0, 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(0xc0006981a0,
0x23a49e0, 0xc0007ca1a0, 0x2, 0x2, 0xef4975, 0x232c0e0)
    /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval_sequence.go:20
+0xfd
github.com/hashicorp/terraform/terraform.EvalRaw(0x232c000, 0xc0006981a0,
0x23a49e0, 0xc0007ca1a0, 0x1b36460, 0x33d75e2, 0x1a9dda0, 0xc00039d300)
    /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval.go:57
+0x131
github.com/hashicorp/terraform/terraform.Eval(0x232c000, 0xc0006981a0,
0x23a49e0, 0xc0007ca1a0, 0xc0006981a0, 0x232c000, 0xc0006981a0, 0x0)
    /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval.go:35
+0x4d
github.com/hashicorp/terraform/terraform.(*Graph).walk.func1(0x1dd79a0,
0xc00039c270, 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(0xc000531d80,
0x1dd79a0, 0xc00039c270, 0xc00042ce00)
    /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
.

If you comment out a line

instance_ids  = join(", ", compact(flatten(values(digitalocean_droplet.stend))))

then everything works as it should.

paultyng commented 4 years ago

I can't find stend in the reference for the Droplet: https://www.terraform.io/docs/providers/do/r/droplet.html

Is that something new you are adding?

Edit: Oh sorry, I just realized this is the resource name, but you don't reference the actual .id on it. Also, its not a list (with a count) or a map (using for_each) so no reason to use these list based functions I think?

This should probably be something like:

instance_ids  = digitalocean_droplet.stend.id
scarbeau commented 4 years ago

Same issue for the below configuration running 0.12.24:

locals {

...

  additional_cidr_blocks = compact(
    flatten(concat(
      [var.cidr_block_public],
      [var.cidr_block_private_3rd_az],
      [var.cidr_block_public_3rd_az],
      [var.additional_cidr_blocks],
    ))
  )

...
}

Error:

Error: Error in function call
67   on locals.tf line 33, in locals:
68   33:     flatten(concat(
69   34: 
70   35: 
71   36: 
72   37: 
73   38: 
74     |----------------
75     | var.additional_cidr_blocks is null
76     | var.cidr_block_private_3rd_az is ""
77     | var.cidr_block_public is "172.32.0.0/26"
78     | var.cidr_block_public_3rd_az is ""
79 Call to function "flatten" failed: panic in function implementation: can't use
80 ElementIterator on null value
81 goroutine 1096 [running]:
82 runtime/debug.Stack(0xc000716ba0, 0x1ae8ae0, 0x23411b0)
83  /opt/goenv/versions/1.12.13/src/runtime/debug/stack.go:24 +0xa4
84 github.com/zclconf/go-cty/cty/function.errorForPanic(...)
85  /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.2.1/cty/function/error.go:44
86 github.com/zclconf/go-cty/cty/function.Function.ReturnTypeForValues.func1(0xc0007170f8,
87 0xc000717108)
88  /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.2.1/cty/function/function.go:217
89 +0x80
90 panic(0x1ae8ae0, 0x23411b0)
91  /opt/goenv/versions/1.12.13/src/runtime/panic.go:522 +0x1c3
92 github.com/zclconf/go-cty/cty.Value.ElementIterator(0x23b9320, 0xc000429940,
93 0x0, 0x0, 0xc000000000000000, 0xc0008fbc68)
94  /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.2.1/cty/value_ops.go:1038
95 +0x10a
96 github.com/hashicorp/terraform/lang/funcs.flattener(0x23b9320, 0xc000429940,
97 0x0, 0x0, 0xc000c8ac01, 0x23b9320, 0xc000429940, 0x0)
98  /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/funcs/collection.go:488
99 +0x93
100 github.com/hashicorp/terraform/lang/funcs.flattener(0x23b9420, 0xc000cf0a80,
101 0x1ac3a00, 0xc000cf0aa0, 0x1, 0xc000716ef8, 0xc000716ea8, 0xc000716e98)
102     /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/funcs/collection.go:495
103 +0x2ba
104 github.com/hashicorp/terraform/lang/funcs.glob..func20(0xc000cf08a0, 0x1, 0x1,
105 0xc000717108, 0xc000716ef0, 0x0, 0x0)
106     /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/funcs/collection.go:457
107 +0xae
108 github.com/zclconf/go-cty/cty/function.Function.ReturnTypeForValues(0xc000406840,
109 0xc000cf08a0, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0)
110     /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.2.1/cty/function/function.go:221
111 +0x45c
112 github.com/zclconf/go-cty/cty/function.Function.Call(0xc000406840,
113 0xc000cf08a0, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
114     /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/zclconf/go-cty@v1.2.1/cty/function/function.go:228
115 +0xa4
116 github.com/hashicorp/hcl/v2/hclsyntax.(*FunctionCallExpr).Value(0xc000179c20,
117 0xc000cf0860, 0x0, 0xc000cf0880, 0xc000406720, 0x1, 0x1, 0x1,
118 0x8732e59200000003)
119     /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.0/hclsyntax/expression.go:398
120 +0x1104
121 github.com/hashicorp/hcl/v2/hclsyntax.(*FunctionCallExpr).Value(0xc000179d10,
122 0xc000cf0860, 0x0, 0xc001bb9700, 0x1, 0x1, 0x0, 0x0, 0x0)
123     /opt/teamcity-agent/work/9e329aa031982669/pkg/mod/github.com/hashicorp/hcl/v2@v2.3.0/hclsyntax/expression.go:364
124 +0x213d
125 github.com/hashicorp/terraform/lang.(*Scope).EvalExpr(0xc000751bd0, 0x23b8220,
126 0xc000179d10, 0x23b92e0, 0x356c258, 0x0, 0x0, 0xc001bb9908, 0x1, 0x1, ...)
127     /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/lang/eval.go:95
128 +0x1ba
129 github.com/hashicorp/terraform/terraform.(*BuiltinEvalContext).EvaluateExpr(0xc001f880d0,
130 0x23b8220, 0xc000179d10, 0x23b92e0, 0x356c258, 0x0, 0x0, 0x0, 0x0, 0x1, ...)
131     /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval_context_builtin.go:279
132 +0x1eb
133 github.com/hashicorp/terraform/terraform.(*EvalLocal).Eval(0xc0011b58c0,
134 0x23eefa0, 0xc001f880d0, 0x2, 0x2, 0xf8bdc4, 0x1c032c0)
135     /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval_local.go:43
136 +0x741
137 github.com/hashicorp/terraform/terraform.EvalRaw(0x2375260, 0xc0011b58c0,
138 0x23eefa0, 0xc001f880d0, 0x1b81080, 0x34217e4, 0x1ae8ae0, 0xc0014a4fc0)
139     /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval.go:57
140 +0x138
141 github.com/hashicorp/terraform/terraform.Eval(0x2375260, 0xc0011b58c0,
142 0x23eefa0, 0xc001f880d0, 0xc0011b58c0, 0x2375260, 0xc0011b58c0, 0x0)
143     /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/eval.go:35
144 +0x54
145 github.com/hashicorp/terraform/terraform.(*Graph).walk.func1(0x1d1de40,
146 0xc001a25170, 0x0, 0x0, 0x0)
147     /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/terraform/graph.go:90
148 +0xf47
149 github.com/hashicorp/terraform/dag.(*Walker).walkVertex(0xc0000a5a00,
150 0x1d1de40, 0xc001a25170, 0xc001450e40)
151     /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/dag/walk.go:392
152 +0x35a
153 created by github.com/hashicorp/terraform/dag.(*Walker).Update
154     /opt/teamcity-agent/work/9e329aa031982669/src/github.com/hashicorp/terraform/dag/walk.go:314
155 +0xaa2
156 .
mieseprem commented 3 years ago

Hello,

I got the same issue with terraform 0.13.4:

$ terraform apply data.google_iam_role.dashboardEditor["roles/monitoring.dashboardEditor"]: Refreshing state... [id=roles/monitoring.dashboardEditor] data.google_iam_role.dashboardEditor["roles/storage.objectCreator"]: Refreshing state... [id=roles/storage.objectCreator] data.google_iam_role.dashboardEditor["roles/monitoring.compute.viewer"]: Refreshing state...

Error: Error in function call

on xxx.tf line 18, in locals: 18: permissions = flatten(local.matchkeys[*].included_permissions) |---------------- | local.matchkeys is list of object with 3 elements

Call to function "flatten" failed: panic in function implementation: can't use ElementIterator on null value

The tf code leading to this is

provider "google" {
  region = "europe-west1"
}

data "google_iam_role" "dashboardEditor" {
    for_each = toset(local.roles)
    name = each.value
}

locals {
  roles = [
        "roles/monitoring.compute.viewer",
        "roles/monitoring.dashboardEditor",
        "roles/storage.objectCreator",
    ]
  matchkeys = matchkeys(values(data.google_iam_role.dashboardEditor), keys(data.google_iam_role.dashboardEditor), local.roles)
  permissions = flatten(local.matchkeys[*].included_permissions)
}

ps: I'm aware of the workaround mentioned in https://github.com/hashicorp/terraform-plugin-sdk/issues/178#issuecomment-533227650

Overbryd commented 3 years ago

Similar issue (panic in function implementation: value is marked, so must be unmarked first) here:

$ terraform -v
Terraform v0.14.2
+ provider registry.terraform.io/hashicorp/google v3.51.0
+ provider registry.terraform.io/hashicorp/google-beta v3.51.0
+ provider registry.terraform.io/hashicorp/helm v1.3.2
+ provider registry.terraform.io/hashicorp/kubernetes v1.13.3
+ provider registry.terraform.io/hashicorp/random v3.0.0
+ provider registry.terraform.io/hashicorp/template v2.2.0
Error: Error in function call

  on ../modules/kubernetes-airflow-with-gitsync/main.tf line 29, in resource "helm_release" "this":
  29:   values     = flatten([
  30:     yamlencode(local.airflow_component_config),
  31:     yamlencode({
  32:       "extraEnvVars" = local.extra_env_vars
  33:       "extraVolumes" = var.extra_volumes
  34:     }),
  35:     var.values,
  36:   ])
    |----------------
    | local.airflow_component_config is object with 3 attributes
    | local.extra_env_vars is tuple with 10 elements
    | var.extra_volumes is list of object with 4 elements
    | var.values is (sensitive value)

Call to function "flatten" failed: panic in function implementation: value is
marked, so must be unmarked first
paddycarver commented 3 years ago

@Overbryd that's actually a different issue (I know, it's hard to tell :( ) and isn't something we can actually do anything about in the SDK. It's more likely to get traction in the hashicorp/terraform repo as a new issue.

bflad commented 2 years ago

Hi folks 👋 If you are still running into an issue similar to this one, please open a new issue in the Terraform CLI repository. There have been many changes to the CLI and this SDK since this issue was originally reported, so it may be likely that using more recent versions of both will resolve these problems.

github-actions[bot] commented 2 years ago

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.