mblakele
mblakele

Reputation: 7840

Can terraform plan show me a json diff for a changed resource?

When I run terraform plan it shows a changed resource, which happens to be JSON data in an aws_s3_bucket_object. But the JSON is long and it's difficult to see what changed. How can I display this as a diff?

Upvotes: 5

Views: 7761

Answers (2)

htaccess
htaccess

Reputation: 3110

I wrote a bash script to format terraforms ugly policy output:

#!/bin/bash

input=$( xclip -o )

old=$( echo "$input" | awk -F' => ' '{ print $1 }'  | sed 's/\\n/\n\r/g' | sed 's/\\"/"/g' | sed 's/"{/{/' | sed 's/}"/}/' )
new=$( echo "$input" | awk -F' => ' '{ print $2 }'  | sed 's/\\n/\n\r/g' | sed 's/\\"/"/g' | sed 's/"{/{/' | sed 's/}"/}/' )

echo "----------------------------------------------------------------------------------------------"
echo "old:"
echo "$old" | jq '.'
echo "----------------------------------------------------------------------------------------------"
echo "new:"
echo "$new" | jq '.'
echo "----------------------------------------------------------------------------------------------"
echo "diff:"
diff -u --color <( echo "$old" | jq '.' ) <( echo "$new" | jq '.' )
echo "----------------------------------------------------------------------------------------------"

It shows three blocks of output, the old, then the new and then the diff. It makes use of xclip, jq and diff. Usage is (on Linux) to highlight the terrafrom output and then invoke the script (I call it tf-diff and it lives in ~/bin).

Upvotes: 2

mblakele
mblakele

Reputation: 7840

https://github.com/coinbase/terraform-landscape can help with this.

  1. gem install terraform_landscape (may need sudo on macOS)
  2. terraform plan | landscape

This shows JSON changes as a diff. Here's an example from the github site:

enter image description here

Upvotes: 13

Related Questions