Output number of changes from Terraform Plan

When I do a Terraform Plan, I want to get the number of changes as a number (integer)

  1. Resources to Destroy
  2. Resources to Change
  3. Resources to Add.

The reason is in my CI\CD pipline, I want to compare this to the lower level account to see if make sure the changes for example to DEV are the same number of changes going to Prod.

Hi @lasiewicz, you can get the machine readable output from a plan using terraform show -json. There is no tally for the changes, but all changes are listed in the list of resource_changes .

In addition to this, the JSON UI output from terraform plan -json does include a machine-readable tally:

{"@level":"info","@message":"Plan: 1 to add, 0 to change, 1 to destroy.","@module":"terraform.ui","@timestamp":"2021-11-30T16:02:48.147795-08:00","changes":{"add":1,"change":0,"remove":1,"operation":"plan"},"type":"change_summary"}

However, that interface is intended for building alternative UIs on top of Terraform rather than accessing data alongside the normal UI, so it comes with the disadvantage that you’d no longer have the normal human-readable plan output.

With that said, since the actual diff rendering is generally the most complex part of an alternative Terraform UI, one other way around this is to invert the flow and use machine-readable mode for the actual planning step and then use the human-readable rendering of the saved plan:

$ terraform plan -json -out=tfplan
$ terraform show tfplan

This approach can also help with customizing the level of displayed output from the planning step, if that’s interesting to you. For example, some folks find the “Refreshing…” prompts noisy in a CI environment where we’re typically not expecting realtime feedback anyway, so the model above can double as a way to reduce the verbosity if the program you have parsing the output is selective about which message types it reacts to.