Hopefully, this is the correct place to post.
I’m investigating ways to work with Terraform state.
My goal is to build tooling to clean up resources that Terraform isn’t capable of doing, such as resources in my mongo DB cluster.
To do this I need to be able to read from Terraform state in GoLang, ideally without making a CLI wrapper.
Is anyone aware of any documentation around reading terraform state to get details in the resources created?
The supported integration point for working programmatically with the state is to run
terraform show -json and then parse the JSON integration format. Although Terraform’s own snapshots happen to also be serialized in a JSON-based format, that format is an implementation detail of Terraform and not guaranteed to remain compatible across all past and future Terraform releases.
If an example would help, I can describe how Terraform Cloud integrates with Terraform plans and state snapshots:
terraform plan, Terraform Cloud runs
terraform show -json PLANFILE and saves the result as metadata attached to the run. Similarly, after
terraform apply it runs
terraform show -json to capture a representation of the resulting state and also saves that as metadata attached to the run.
Then when Terraform Cloud’s UI or other features present, for example, a list of the resource instances that belong to a workspace, or the proposed actions in a plan, it can refer to those two artifacts to get the necessary information from its own database, without needing to interact any further with Terraform CLI until the next plan/apply run.
Gave your suggestion a try and that looks like it will solve my use case.
Before I go set up my own structs for the JSON are you aware of any struct I can import from the terraform project?