Skip to content

Commit e8b3062

Browse files
committed
terraform: unknown value for variables not set
Fixes hashicorp#12836 Realistically, these should be caught during validation anyways. In this case, this was causing 12386 because refresh with a data source will attempt to use module variables. I don't see any clear logic to prune those module variables or not add them so its easier to return unknown to cause the data to be computed and not run.
1 parent 14e07b1 commit e8b3062

4 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎terraform/context_refresh_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,32 @@ func TestContext2Refresh(t *testing.T) {
6060
}
6161
}
6262

63+
func TestContext2Refresh_dataComputedModuleVar(t *testing.T) {
64+
p := testProvider("aws")
65+
m := testModule(t, "refresh-data-module-var")
66+
ctx := testContext2(t, &ContextOpts{
67+
Module: m,
68+
Providers: map[string]ResourceProviderFactory{
69+
"aws": testProviderFuncFixed(p),
70+
},
71+
})
72+
73+
p.RefreshFn = nil
74+
p.RefreshReturn = &InstanceState{
75+
ID: "foo",
76+
}
77+
78+
s, err := ctx.Refresh()
79+
if err != nil {
80+
t.Fatalf("err: %s", err)
81+
}
82+
83+
checkStateString(t, s, `
84+
<no state>
85+
module.child:
86+
<no state>`)
87+
}
88+
6389
func TestContext2Refresh_targeted(t *testing.T) {
6490
p := testProvider("aws")
6591
m := testModule(t, "refresh-targeted")

‎terraform/interpolate.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (i *Interpolater) valueResourceVar(
262262
// If it truly is missing, we'll catch it on a later walk.
263263
// This applies only to graph nodes that interpolate during the
264264
// config walk, e.g. providers.
265-
if i.Operation == walkInput {
265+
if i.Operation == walkInput || i.Operation == walkRefresh {
266266
result[n] = unknownVariable()
267267
return nil
268268
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
variable "key" {}
2+
3+
data "aws_data_source" "foo" {
4+
id = "${var.key}"
5+
}
6+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "aws_instance" "A" {
2+
foo = "bar"
3+
}
4+
5+
module "child" {
6+
source = "child"
7+
key = "${aws_instance.A.id}"
8+
}

0 commit comments

Comments
 (0)