I've saved terraform plan -out=my-plan
and intend to save it to source control and inject further to custom tool for ingestion and performing testing etc.
However, the file contents of my-plan
are jumbled and I'm wondering what the encoding used is.
What is the encoding being used for the Terraform plan file?
The Terraform plan output is a binary format that is not designed to be used outside of Terraform. Technically you could probably serialise it using whatever Terraform uses to handle the format but there is no stable API for this and could change at any point.
The terraform show command is used to provide human-readable output from a state or plan file. This can be used to inspect a plan to ensure that the planned operations are expected, or to inspect the current state as Terraform sees it. Machine-readable output is generated by adding the -json command-line flag.
Output values are stored as plain text in your state file — including those flagged as sensitive. Use the grep command to see the values of the sensitive outputs in your state file. Tip: If you are using an operating system without the grep command, open the terraform.
Terraform output variables are used within the same parent or child module to print specific values in the command line output and are also used as inputs to create resources using the terraform apply command. Below, you can see the command displays the output's EC2 instance arn and instance public IP address.
While the other tools mentioned here are useful, things change regularly in the Terraform space and third-party tools often aren't able to be kept up-to-date.
For some time now Terraform has directly supported viewing a plan file in the same human-readable format that is displayed at the time you run plan
:
terraform show <filename>
Since v0.12 you can now also view a plan file in JSON format, which you could save to work on further with other tools:
terraform show -json <filename>
There's an explanation of the JSON schema at https://www.terraform.io/docs/internals/json-format.html. As of writing, note that:
The output ... currently has major version zero to indicate that the format is experimental and subject to change. A future version will assign a non-zero major version ... We do not anticipate any significant breaking changes to the format before its first major version, however.
The Terraform plan output is a binary format that is not designed to be used outside of Terraform. Technically you could probably serialise it using whatever Terraform uses to handle the format but there is no stable API for this and could change at any point.
One of the Hashicorp employees (Phinze) briefly covered this in this issue: https://github.com/hashicorp/terraform/issues/7976
One, probably reasonably fragile, option would be to simply parse the text output from running terraform plan
. I use Terraform Landscape to format plan outputs locally when working with JSON diffs that Terraform doesn't handle at all and that copes with this fine. However it also tends to break on the "minor" version upgrades (eg 0.9 to 0.10) as Terraform doesn't specify this as an API at all. Terraform Plan Parser also parses the textual output and notes that it is very much not to be used with the binary output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With