@@ -407,10 +407,7 @@ func (ctx *BuiltinEvalContext) EvaluateReplaceTriggeredBy(expr hcl.Expression, r
407407return nil , false , diags
408408}
409409
410- path , _ := traversalToPath (ref .Remaining )
411- attrBefore , _ := path .Apply (change .Before )
412- attrAfter , _ := path .Apply (change .After )
413-
410+ attrBefore , attrAfter , diags := evalTriggeredByRefPath (ref , change )
414411if attrBefore == cty .NilVal || attrAfter == cty .NilVal {
415412replace := attrBefore != attrAfter
416413return ref , replace , diags
@@ -421,6 +418,42 @@ func (ctx *BuiltinEvalContext) EvaluateReplaceTriggeredBy(expr hcl.Expression, r
421418return ref , replace , diags
422419}
423420
421+ // evalTriggeredByRefPath evaluates the attribute reference path in the context of the
422+ // resource change objects. It returns the before and after values of the attribute
423+ // and any diagnostics that occurred during evaluation.
424+ func evalTriggeredByRefPath (ref * addrs.Reference , change * plans.ResourceInstanceChange ) (cty.Value , cty.Value , tfdiags.Diagnostics ) {
425+ var diags tfdiags.Diagnostics
426+ path , key := traversalToPath (ref .Remaining )
427+
428+ applyPath := func (value cty.Value ) (cty.Value , tfdiags.Diagnostics ) {
429+ attr , err := path .Apply (value )
430+ if err != nil {
431+ return cty .NilVal , tfdiags.Diagnostics {}.Append (tfdiags .AttributeValue (
432+ tfdiags .Error ,
433+ "Invalid attribute path" ,
434+ fmt .Sprintf (
435+ "The specified path %q could not be applied to the object specified in replace_triggered_by:\n " +
436+ "Path: %s\n " +
437+ "Error: %s\n " +
438+ "Please check your configuration and ensure the path is valid." ,
439+ key , ref .DisplayString (), err .Error (),
440+ ),
441+ path ,
442+ ))
443+ }
444+ return attr , nil
445+ }
446+
447+ // Apply the path to the "before" and "after" states
448+ attrBefore , beforeDiags := applyPath (change .Before )
449+ diags = diags .AppendWithoutDuplicates (beforeDiags ... )
450+
451+ attrAfter , afterDiags := applyPath (change .After )
452+ diags = diags .AppendWithoutDuplicates (afterDiags ... )
453+
454+ return attrBefore , attrAfter , diags
455+ }
456+
424457func (ctx * BuiltinEvalContext ) EvaluationScope (self addrs.Referenceable , source addrs.Referenceable , keyData InstanceKeyEvalData ) * lang.Scope {
425458switch scope := ctx .scope .(type ) {
426459case evalContextModuleInstance :
0 commit comments