Skip to content

core: Do not create "delete" changes for nonexistent outputs#31471

Merged
kmoe merged 1 commit intomainfrom
kmoe/output-destroy-plan
Aug 3, 2022
Merged

core: Do not create "delete" changes for nonexistent outputs#31471
kmoe merged 1 commit intomainfrom
kmoe/output-destroy-plan

Conversation

@kmoe
Copy link
Copy Markdown
Member

@kmoe kmoe commented Jul 19, 2022

If there are outputs in configuration, a destroy plan will always contain a "delete" change for each of these outputs.

If an output was not present in state to begin with, the change will look like this:

 "output_changes": {
    "foo": {
      "actions": [
        "delete"
      ],
      "before": null,
      "after": null,
      "after_unknown": false,
      "before_sensitive": false,
      "after_sensitive": false
    }
  },

Since there is a change in the plan, this plan will be considered applyable, and the user will be presented with text instructing them to apply a plan in which there are no actual changes.

Applying the destroy plan will not actually do anything, since the output was not in state to begin with.

This commit stops the above from happening in the case of root module outputs.

A simple example follows. For a more realistic example and the issue that motivated this change, please see #23387 (comment).

Example

Configuration

output "foo" {
  value = null
}

Steps

  1. terraform init
  2. terraform plan -detailed-exitcode -destroy

Result prior to change

Exit code 2.


Changes to Outputs:

You can apply this plan to save these new output values to the
Terraform state, without changing any real infrastructure.

───────────────────────────────────────────────────────────────────

Plan prior to change

{
  "format_version": "1.1",
  "terraform_version": "1.3.0-dev",
  "planned_values": {
    "root_module": {}
  },
  "output_changes": {
    "foo": {
      "actions": [
        "delete"
      ],
      "before": null,
      "after": null,
      "after_unknown": false,
      "before_sensitive": false,
      "after_sensitive": false
    }
  },
  "configuration": {
    "root_module": {
      "outputs": {
        "foo": {
          "expression": {
            "constant_value": null
          }
        }
      }
    }
  }
}

Result after change

Exit code 0.


No changes. No objects need to be destroyed.

Either you have not created any objects yet or the existing objects were already deleted outside of Terraform.

Plan after change

{
  "format_version": "1.1",
  "terraform_version": "1.3.0-dev",
  "planned_values": {
    "root_module": {}
  },
  "configuration": {
    "root_module": {
      "outputs": {
        "foo": {
          "expression": {
            "constant_value": null
          }
        }
      }
    }
  }
}

If there are outputs in configuration, a destroy plan will always contain a "delete" change for each of these outputs.

This leads to meaningless delete changes being present for outputs which were not present in state and therefore cannot be deleted. Since there is a change in the plan, this plan will then be considered applyable, and the user will be presented with text instructing them to apply a plan in which there are no actual changes.

This commit stops the above from happening in the case of root module outputs.
@kmoe kmoe force-pushed the kmoe/output-destroy-plan branch from ca3867a to 3081083 Compare July 19, 2022 17:24
Copy link
Copy Markdown
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.

LGTM! These were never in there for any particular reason. Since outputs are always re-calculated during a plan, they were always added to the changes regardless of what that change was.

@kmoe kmoe merged commit c4a0066 into main Aug 3, 2022
@kmoe kmoe deleted the kmoe/output-destroy-plan branch August 3, 2022 16:45
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Aug 3, 2022

Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Sep 3, 2022

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants