Search code examples
jsonterraformoutput

How to get a specific value out of the Terraform output


Hi I have this JSON output saved in my terraform state. how can extract only info that I need?

If I run terraform output -json | jq I received the output below:

{
  "db_instance_address_ie": {
    "sensitive": false,
    "type": [
      "object",
      {
        "Service1": "string",
        "Service2": "string"
      }
    ],
    "value": {
      "Service1": "Service1-mysql.xxxxxxxxxxxx.eu-west-1.rds.amazonaws.com",
      "Service2": "Service2-mysql.xxxxxxxxxxxx.eu-west-1.rds.amazonaws.com"
    }
  },
  "grafana_alb_endpoint_ie": {
    "sensitive": false,
    "type": [
      "object",
      {
        "Service1": "string",
        "Service2": "string"
      }
    ],
    "value": {
      "Service1": "Service1-grafana-alb-xxxxxxxxx.eu-west-1.elb.amazonaws.com",
      "Service2": "Service2-grafana-alb-xxxxxxxxx.eu-west-1.elb.amazonaws.com"
    }
  },
  "grafana_private_key_ie": {
    "sensitive": true,
    "type": [
      "object",
      {
        "Service1": "string",
        "Service2": "string"
      }
    ],
    "value": {
      "Service1": "-----BEGIN RSA PRIVATE KEY-----xxxxxxxx-----END RSA PRIVATE KEY-----",
      "Service2": "-----BEGIN RSA PRIVATE KEY-----xxxxxxxx-----END RSA PRIVATE KEY-----"
    }
  },
  "influxdb_password_ie": {
    "sensitive": true,
    "type": [
      "object",
      {
        "Service1": "string",
        "Service2": "string"
      }
    ],
    "value": {
      "Service1": "********************",
      "Service2": "********************"
    }
  },
  "mysql_password_ie": {
    "sensitive": true,
    "type": [
      "object",
      {
        "Service1": "string",
        "Service2": "string"
      }
    ],
    "value": {
      "Service1": "********************",
      "Service2": "********************"
    }
  },
  "zabbix_lb_endpoint_ie": {
    "sensitive": false,
    "type": [
      "object",
      {
        "Service1": "string",
        "Service2": "string"
      }
    ],
    "value": {
      "Service1": "Service1-zbx-nlb-xxxxxxxxxxxx.elb.eu-west-1.amazonaws.com",
      "Service2": "Service2-zbx-nlb-xxxxxxxxxxxx.elb.eu-west-1.amazonaws.com"
    }
  },
  "zabbix_private_key_ie": {
    "sensitive": true,
    "type": [
      "object",
      {
        "Service1": "string",
        "Service2": "string"
      }
    ],
    "value": {
      "Service1": "-----BEGIN RSA PRIVATE KEY-----xxxxxxxx-----END RSA PRIVATE KEY-----",
      "Service2": "-----BEGIN RSA PRIVATE KEY-----xxxxxxxx-----END RSA PRIVATE KEY-----"
    }
  }
}

This is my full output json.

I tried to use the command below to show value, for example for :

terraform show -json | jq '.values.outputs.zabbix_private_key_ie[]'

but the output is:

true
{
  "Service1": "-----BEGIN RSA PRIVATE KEY----------END RSA PRIVATE KEY-----",
  "Service2": "-----BEGIN RSA PRIVATE KEY----------END RSA PRIVATE KEY-----"
}
[
  "object",
  {
    "Service1": "string",
    "Service2": "string"
  }
]

I need to extract only the values for Service1. Is it possible?


Solution

  • Thanks a lot. I solved with this command:

    terraform show -json | jq '.values.outputs.zabbix_private_key_ie.value.service1'
    

    and the output is:

    "-----BEGIN RSA PRIVATE KEY-----\nxxxxxxxxx\n-----END RSA PRIVATE KEY-----\n"