how to extract data from terrafrom state show

Viewed 1500

I would like to extract the data given from terraform state show. According to documentation we should use terraform show -json . https://www.terraform.io/docs/cli/commands/state/show.html

The output of terraform state show is intended for human consumption, not programmatic consumption. To extract state data for use in other software, use terraform show -json and decode the result using the documented structure.

Not sure how to use terrafrom state show in conjuction with terrafrom show

$ terraform state show 'packet_device.worker'
# packet_device.worker:
resource "packet_device" "worker" {
    billing_cycle = "hourly"
    created       = "2015-12-17T00:06:56Z"
    facility      = "ewr1"
    hostname      = "prod-xyz01"
    id            = "6015bg2b-b8c4-4925-aad2-f0671d5d3b13"
    locked        = false
}
1 Answers

The terraform state show command displays information on a single Terraform resource and does not support the -json flag. The command terraform show dumps the entire state, and does support the -json flag. Unlike the output from terraform state show, the output of terraform show -json is documented and intended for programmatic consumption.

If you want to obtain the info on a particular resource as displayed by terraform state show, you can extract it from the full-state JSON, for example by using jq:

terraform show -json | \
jq '.values.root_module.resources[] | select(.address == "packet_device.worker") | .values'

Whether this makes sense depends on what it is you're trying to do.

Related