terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Add -json flag to terraform state show

Open onetwopunch opened this issue 6 years ago • 20 comments

Current Terraform Version

Terraform v0.12.20

Use-cases

I need to automate fetching a value from the terraform state that a module developer didn't add to an output.

Attempted Solutions

Currently I'd need to write a very clunky jq query. As an example, if I have a google_compute_address and I had to get the IP address out of that, I'd have do something like this, assuming I've downloaded the terraform state from the remote backend:

cat terraform.tfstate | jq '.resources[] | \
  select(.name == "foo") | \
  select(.type == "google_compute_address") | \
  .attributes.address'

While I could use the terraform_remote_state data provider, this requires an extra terraform config to do and is not trivial to integrate into other CLI interfaces.

Proposal

I'd like to be able to run something like this.

terraform state show -json google_compute_address.foo | jq .address

But get an error since the -json flag is not supported for the state. It seems to me that if terraform show has a -json flag, so should terraform state show since HCL is nowhere near as ubiquitous as JSON.

References

  • #21777

onetwopunch avatar Jan 24 '20 00:01 onetwopunch

This would be a very useful feature

adamrushuk avatar Jul 15 '20 13:07 adamrushuk

This is also needed because terraform state show produces output which is hard to parse because of the additional special characters around attribute names.

jurajseffer avatar Aug 17 '20 13:08 jurajseffer

Bump.

hillnicholas avatar Apr 23 '21 15:04 hillnicholas

This is necessary as well to extract sensitive data from state (specific fields) without downloading the entire state as a giant json blob to disk.

wernerb avatar May 19 '21 12:05 wernerb

This is a very needed feature :+1:

pleszczy avatar Jun 17 '21 13:06 pleszczy

I agree this is needed feature and I would like to see this feature realized so that we can parse/process the json output as per our needs for further processing. At this point it is much harder to parse through the data.

mmshaikh88 avatar Jun 23 '21 22:06 mmshaikh88

I've created a module (Invicton-Labs/get-state/null) that cleanly extracts state data and doesn't require jq to be installed or having downloaded the terraform.tfstate file. It should work on any Linux or Windows system without anything preinstalled (Mac currently untested).

It returns data in a similar format to the way it's stored in the terraform.tfstate file, so you'd have to do something like:

module "get_state" {
    source = "Invicton-Labs/get-state/null
}

output "ip_address" {
  value = module.get_state.resources["google_compute_address.foo"].instances[0].attributes.address
}

The module page has an extensive example to show what the output of the resources map looks like.

KyleKotowick avatar Jul 31 '21 03:07 KyleKotowick

+1

bk-paperspace avatar Oct 13 '21 19:10 bk-paperspace

Having the same issue, strange the docs says that -json is supported by Terraform 0.12 or later. See https://www.terraform.io/docs/cli/commands/show.html

npalm avatar Dec 12 '21 15:12 npalm

@npalm that's terraform show, this issue is about terraform state show

jbg avatar Dec 12 '21 15:12 jbg

@npalm that's terraform show, this issue is about terraform state show

Arch, and I need terraform state show -json arch

npalm avatar Dec 12 '21 16:12 npalm

+1

EarthmanT avatar Jan 04 '22 20:01 EarthmanT

+1

innovia avatar Feb 10 '22 18:02 innovia

+1

m1mohamad avatar Feb 16 '22 19:02 m1mohamad

Just a reminder to use the 👍 on the original post to upvote an issue. Thanks!

crw avatar Feb 17 '22 00:02 crw

Please make it happen! Thank you!

Nachasic avatar Jan 23 '23 08:01 Nachasic

Any updates on this? This would be much appreciated in infrastructure governance tasks

MrwanBaghdad avatar Mar 16 '23 12:03 MrwanBaghdad

We can do it already with help of terraform state pull

In my case, I'm in the middle of importing our azure stuff, and wish to see what things I have missed, here is how we can do it:

$resources = az resource list | ConvertFrom-Json | Select-Object id, name, resourceGroup

$managed = terraform state pull | ConvertFrom-Json | Select-Object -ExpandProperty resources | Select-Object -ExpandProperty instances | Select-Object -ExpandProperty attributes | Select-Object -ExpandProperty id -Unique

$resources | Where-Object id -notin $managed

marchenko1985 avatar Mar 22 '23 17:03 marchenko1985

here's a simpler example with posix shell and jq

terraform state pull > tfstate.json
ebs_volume_id_1="$(cat tfstate.json | jq -r '.resources[] | select(.type == "aws_ebs_volume" and .name == "volume_one") | .instances[].attributes.id')"
ebs_volume_id_2="$(cat tfstate.json | jq -r '.resources[] | select(.type == "aws_ebs_volume" and .name == "volume_two") | .instances[].attributes.id')"
rm tfstate.json

forest-code42 avatar Jul 16 '24 20:07 forest-code42

+1 - How hard can it be?

stonefield avatar May 02 '25 14:05 stonefield