Closed dekimsey closed 2 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.)
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.
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.
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
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 .
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
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
@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.
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.
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.
Terraform Version
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.
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
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