diff --git a/terraform/node_local.go b/terraform/node_local.go index de41fc5fc9..591eb305a6 100644 --- a/terraform/node_local.go +++ b/terraform/node_local.go @@ -47,7 +47,7 @@ func (n *NodeLocal) ReferenceableAddrs() []addrs.Referenceable { // GraphNodeReferencer func (n *NodeLocal) References() []*addrs.Reference { refs, _ := lang.ReferencesInExpr(n.Config.Expr) - return refs + return appendResourceDestroyReferences(refs) } // GraphNodeEvalable diff --git a/terraform/node_output.go b/terraform/node_output.go index cd686a33a6..c42dd4860f 100644 --- a/terraform/node_output.go +++ b/terraform/node_output.go @@ -108,7 +108,7 @@ func referencesForOutput(c *configs.Output) []*addrs.Reference { // GraphNodeReferencer func (n *NodeApplyableOutput) References() []*addrs.Reference { - return referencesForOutput(n.Config) + return appendResourceDestroyReferences(referencesForOutput(n.Config)) } // GraphNodeEvalable diff --git a/terraform/transform_reference.go b/terraform/transform_reference.go index 4e8cae06da..4e2c629b56 100644 --- a/terraform/transform_reference.go +++ b/terraform/transform_reference.go @@ -453,6 +453,29 @@ func ReferenceFromInterpolatedVar(v config.InterpolatedVariable) []string { } } +// appendResourceDestroyReferences identifies resource and resource instance +// references in the given slice and appends to it the "destroy-phase" +// equivalents of those references, returning the result. +// +// This can be used in the References implementation for a node which must also +// depend on the destruction of anything it references. +func appendResourceDestroyReferences(refs []*addrs.Reference) []*addrs.Reference { + given := refs + for _, ref := range given { + switch tr := ref.Subject.(type) { + case addrs.Resource: + newRef := *ref // shallow copy + newRef.Subject = tr.Phase(addrs.ResourceInstancePhaseDestroy) + refs = append(refs, &newRef) + case addrs.ResourceInstance: + newRef := *ref // shallow copy + newRef.Subject = tr.Phase(addrs.ResourceInstancePhaseDestroy) + refs = append(refs, &newRef) + } + } + return refs +} + func modulePrefixStr(p addrs.ModuleInstance) string { return p.String() }