diff --git a/terraform/evaluate.go b/terraform/evaluate.go index 3fe5f5c4d..342e695b7 100644 --- a/terraform/evaluate.go +++ b/terraform/evaluate.go @@ -452,8 +452,16 @@ func (d *evaluationStateData) GetResourceInstance(addr addrs.ResourceInstance, r ms := d.Evaluator.State.ModuleByPath(d.ModulePath) if ms == nil { - // Not evaluated yet? - return cty.DynamicVal, diags + // If we have no module state in the apply walk, that suggests we've hit + // a rather awkward edge-case: the resource this variable refers to + // has count = 0 and is the only resource processed so far on this walk, + // and so we've ended up not creating any resource states yet. We don't + // create a module state until the first resource is written into it, + // so the module state doesn't exist when we get here. + // + // In this case we act as we would if we had been passed a module + // with an empty resource state map. + ms = &ModuleState{} } // Note that the state structs currently have confusing legacy names: