Skip to content

Conversation

DanielMSchmidt
Copy link
Contributor

When one uses the -target flag on a resource we want all of the resource to execute, including all associated actions. This also includes actions associated with dependent resources since those resources also are applied / planned.

Fixes #

Target Release

1.15.x

Rollback Plan

  • If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.

CHANGELOG entry

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing.

@DanielMSchmidt DanielMSchmidt requested a review from a team as a code owner September 24, 2025 13:59
@DanielMSchmidt DanielMSchmidt added the no-changelog-needed Add this to your PR if the change does not require a changelog entry label Sep 24, 2025
Copy link
Member

@liamcervante liamcervante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, will let someone else approve it though as I helped write it!

@DanielMSchmidt DanielMSchmidt added the 1.14-backport If you add this label to a PR before merging, backport-assistant will open a new PR once merged label Sep 24, 2025
// triggering node has planned so that we can ensure the actions are only planned if the triggering
// resource has an action (Create / Update) corresponding to one of the events in the action trigger
// blocks event list.
func (t *TargetsTransformer) addDependencies(g *Graph, v dag.Vertex, targetedNodes dag.Set, addrs []addrs.Targetable) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems odd to me - we already have dependency transformers on resources, so this method should only add action dependencies, correct? If I'm reading it correctly, let's name this something clearer, like addTriggeredActionDependencies (addMaybeTriggeredActionDependencies?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah at this point in the graph we have already added all dependencies and such. What this does is adding the dependencies we identify for the vertex (so the ancestors being what we added so far and the children that are action trigger related is what we are adding now) to the list of targeted nodes. I'll add a more descirptive name mentioning that the targeted nodes is being changed, I think that should help

for _, key := range keys {
absResourceInstanceAddr := n.lifecycleActionTrigger.resourceAddress.Absolute(module).Instance(key)

if n.resourceTargets != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary, is this another quirk of actions runtime behaviors? The targets transformer should drop all unnecessary nodes from the graph, so I would hope we wouldn't need to manage that behavior here as well as in the transformer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a similar thing in the resource nodes. It is necessary because the expansion happens as part of the graph execution, therefore if you target a resource instance we need to filter out all other resource instances ad-hoc. The same applies for action triggers of not-targeted resource instances. Let me add a comment there to explain this.

Copy link
Contributor

@mildwonkey mildwonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick comment to capture internal conversation - resources which are dependences of actions that haven't been triggered (but are in the lifecycle of the targeted resource) are getting pulled in during targeting, so we need to remove (or not add) non-triggered action dependencies from the graph.

Update: wiser terraformers than I pointed out that this behavior is actually quite correct, and I agree now that I understand how targeting works. Targeting only takes static configuration into account, and therefore will pick up all actions included in a resource's action_triggers (and therefore all of those actions' dependencies) regardless of if they are triggered by the current operation. That is for the best - if a user is targeting a resource this means that terraform will ensure all actions are "ready to go".

@mildwonkey mildwonkey dismissed their stale review September 24, 2025 15:45

(update in the original comment - this behavior is correct)

jbardin
jbardin previously approved these changes Sep 24, 2025
Copy link
Member

@jbardin jbardin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks correct for the current implementation as I understand it. The special cases for specific node types can be kind of fragile, but given the state of flux I don't have a better idea at the moment.

@DanielMSchmidt DanielMSchmidt marked this pull request as ready for review October 1, 2025 08:42
@mildwonkey
Copy link
Contributor

mildwonkey commented Oct 1, 2025

I played with this locally a bit, and I noticed that we're no longer pulling in dependencies from non-triggered actions. Last I recall, we'd decided that was correct behavior - even if the action isn't triggered by the current (-target) operation, we do still want terraform to ensure that all possible dependencies of the targeted resource are included in the plan. I could be wrong - there's been a lot of back and forth on features - but I didn't think we wanted to lose that functionality. WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.14-backport If you add this label to a PR before merging, backport-assistant will open a new PR once merged no-changelog-needed Add this to your PR if the change does not require a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants