@@ -137,6 +137,8 @@ func (e *evaluator) EvaluateAll(ctx context.Context) (terraform.Modules, map[str
137
137
138
138
// expand out resources and modules via count, for-each and dynamic
139
139
// (not a typo, we do this twice so every order is processed)
140
+ // TODO: using a module in for_each or count does not work,
141
+ // because the child module is evaluated later
140
142
e .blocks = e .expandBlocks (e .blocks )
141
143
e .blocks = e .expandBlocks (e .blocks )
142
144
@@ -239,10 +241,17 @@ func (e *evaluator) evaluateSubmodule(ctx context.Context, sm *submodule) bool {
239
241
sm .modules , sm .fsMap = sm .eval .EvaluateAll (ctx )
240
242
outputs := sm .eval .exportOutputs ()
241
243
244
+ valueMap := e .ctx .Get ("module" ).AsValueMap ()
245
+ if valueMap == nil {
246
+ valueMap = make (map [string ]cty.Value )
247
+ }
248
+
242
249
// lastState needs to be captured after applying outputs – so that they
243
250
// don't get treated as changes – but before running post-submodule
244
251
// evaluation, so that changes from that can trigger re-evaluations of
245
252
// the submodule if/when they feed back into inputs.
253
+ ref := sm .definition .Definition .Reference ()
254
+ e .ctx .Set (blockInstanceValues (sm .definition .Definition , valueMap , outputs ), "module" , ref .NameLabel ())
246
255
e .ctx .Set (outputs , "module" , sm .definition .Name )
247
256
sm .lastState = sm .definition .inputVars ()
248
257
e .evaluateSteps ()
@@ -564,7 +573,7 @@ func (e *evaluator) getValuesByBlockType(blockType string) cty.Value {
564
573
if valueMap == nil {
565
574
valueMap = make (map [string ]cty.Value )
566
575
}
567
- valueMap [ref .NameLabel ()] = blockInstanceValues (b , valueMap )
576
+ valueMap [ref .NameLabel ()] = blockInstanceValues (b , valueMap , b . Values () )
568
577
569
578
// Update the map of all blocks with the same type.
570
579
values [ref .TypeLabel ()] = cty .ObjectVal (valueMap )
@@ -588,7 +597,7 @@ func (e *evaluator) getResources() map[string]cty.Value {
588
597
typeValues = make (map [string ]cty.Value )
589
598
values [ref .TypeLabel ()] = typeValues
590
599
}
591
- typeValues [ref .NameLabel ()] = blockInstanceValues (b , typeValues )
600
+ typeValues [ref .NameLabel ()] = blockInstanceValues (b , typeValues , b . Values () )
592
601
}
593
602
594
603
return lo .MapValues (values , func (v map [string ]cty.Value , _ string ) cty.Value {
@@ -600,14 +609,14 @@ func (e *evaluator) getResources() map[string]cty.Value {
600
609
// If the count argument is used, a tuple is returned where the index corresponds to the argument index.
601
610
// If the for_each argument is used, an object is returned where the key corresponds to the argument key.
602
611
// In other cases, the values of the block itself are returned.
603
- func blockInstanceValues (b * terraform.Block , typeValues map [string ]cty.Value ) cty.Value {
612
+ func blockInstanceValues (b * terraform.Block , typeValues map [string ]cty.Value , values cty. Value ) cty.Value {
604
613
ref := b .Reference ()
605
614
key := ref .RawKey ()
606
615
607
616
switch {
608
617
case key .Type ().Equals (cty .Number ) && b .GetAttribute ("count" ) != nil :
609
618
idx , _ := key .AsBigFloat ().Int64 ()
610
- return insertTupleElement (typeValues [ref .NameLabel ()], int (idx ), b . Values () )
619
+ return insertTupleElement (typeValues [ref .NameLabel ()], int (idx ), values )
611
620
case isForEachKey (key ) && b .GetAttribute ("for_each" ) != nil :
612
621
keyStr := ref .Key ()
613
622
@@ -621,11 +630,10 @@ func blockInstanceValues(b *terraform.Block, typeValues map[string]cty.Value) ct
621
630
instances = make (map [string ]cty.Value )
622
631
}
623
632
624
- instances [keyStr ] = b . Values ()
633
+ instances [keyStr ] = values
625
634
return cty .ObjectVal (instances )
626
-
627
635
default :
628
- return b . Values ()
636
+ return values
629
637
}
630
638
}
631
639
0 commit comments