|
| 1 | +# TAGLINE |
| 2 | + |
| 3 | +Search existing infrastructure to import into Terraform |
| 4 | + |
| 5 | +# TLDR |
| 6 | + |
| 7 | +**List resources** that match `.tfquery.hcl` files in the working directory |
| 8 | + |
| 9 | +```terraform query``` |
| 10 | + |
| 11 | +Pass an **input variable** declared in the query file |
| 12 | + |
| 13 | +```terraform query -var="[env]=[prod]"``` |
| 14 | + |
| 15 | +Load variables from a **tfvars file** |
| 16 | + |
| 17 | +```terraform query -var-file=[path/to/file.tfvars]``` |
| 18 | + |
| 19 | +Print **JSON** results |
| 20 | + |
| 21 | +```terraform query -json``` |
| 22 | + |
| 23 | +**Generate import blocks** and resource configuration for the matches |
| 24 | + |
| 25 | +```terraform query -generate-config-out=[generated.tf]``` |
| 26 | + |
| 27 | +Evaluate a **policy set** against the resources the query finds |
| 28 | + |
| 29 | +```terraform query -policies=[path/to/policy-set]``` |
| 30 | + |
| 31 | +# SYNOPSIS |
| 32 | + |
| 33 | +**terraform** **query** [_options_] |
| 34 | + |
| 35 | +# PARAMETERS |
| 36 | + |
| 37 | +**-var** _'name=value'_ |
| 38 | + |
| 39 | +> Set an input variable declared in the query configuration. Repeat the flag to set more than one variable. |
| 40 | +
|
| 41 | +**-var-file** _filename_ |
| 42 | + |
| 43 | +> Load variable values from a file, in addition to **terraform.tfvars** and **\*.auto.tfvars**. Repeat the flag to include more than one file. |
| 44 | +
|
| 45 | +**-policies** _path_ |
| 46 | + |
| 47 | +> Evaluate policies from a policy set directory against the resources the query discovers. Repeat the flag for more than one policy set. **--policies** is also accepted. The path must be a directory. |
| 48 | +
|
| 49 | +**-generate-config-out** _path_ |
| 50 | + |
| 51 | +> Write **import** and **resource** blocks for the results, including resource identities, to a new file. The path must not already exist. Combined with **-json**, the generated configuration is included in the JSON output instead of written to a file. |
| 52 | +
|
| 53 | +**-json** |
| 54 | + |
| 55 | +> Print machine-readable JSON instead of the human-readable listing. |
| 56 | +
|
| 57 | +**-no-color** |
| 58 | + |
| 59 | +> Disable colored output. |
| 60 | +
|
| 61 | +# DESCRIPTION |
| 62 | + |
| 63 | +**terraform query** asks configured providers to list remote objects that match **list** blocks in **.tfquery.hcl** files, then prints the matches. It is the bulk counterpart of importing one resource at a time: the query itself does not change state. Copy the generated **import** and **resource** blocks into the configuration and run **terraform apply** to bring those objects under management. |
| 64 | + |
| 65 | +Each result names the list block that found it, as **list.**_type_**.**_label_, and the identity of the discovered resource. Providers may add a short description or other fields. How many rows come back depends on the provider and on limits and filters in the query file. |
| 66 | + |
| 67 | +The working directory needs a normal Terraform configuration with a **required_providers** block, plus the query files, and it must already be initialized so the provider plugins and credentials are available. **terraform validate -query** checks query files without contacting providers. Since Terraform 1.15, **terraform fmt** reformats **.tfquery.hcl** files. |
| 68 | + |
| 69 | +# CONFIGURATION |
| 70 | + |
| 71 | +**.tfquery.hcl** |
| 72 | + |
| 73 | +> Query files in the configuration root. Each **list** "_type_" "_name_" block names a resource type, a **provider**, and optional provider-specific filters inside a **config** block. **variable** and **locals** blocks in these files supply values to the query. |
| 74 | +
|
| 75 | +**terraform.tfvars**, **\*.auto.tfvars** |
| 76 | + |
| 77 | +> Variable files loaded automatically, the same way as for **plan** and **apply**. **-var** and **-var-file** override them. |
| 78 | +
|
| 79 | +**cloud** block |
| 80 | + |
| 81 | +> When the configuration is connected to HCP Terraform or Terraform Enterprise, query results can be compared with resources already managed in other workspaces. **-generate-config-out** still writes its file on the local machine. |
| 82 | +
|
| 83 | +# CAVEATS |
| 84 | + |
| 85 | +**terraform query** requires **Terraform 1.14** or later. Earlier releases have no query command and do not load **.tfquery.hcl** files. |
| 86 | + |
| 87 | +The command reads live infrastructure. It needs provider credentials and a successful **terraform init**. It does not create, update, or destroy resources, and it does not write them into state until you apply the generated **import** blocks. |
| 88 | + |
| 89 | +Only resource types whose provider implements listing can appear. A **list** block for an unsupported type fails the query. |
| 90 | + |
| 91 | +**-generate-config-out** refuses to overwrite an existing file. Delete or move the previous output before running the command again. With **-json**, Terraform does not write that file; the generated configuration is part of the JSON document. |
| 92 | + |
| 93 | +**-policies** expects directories. A path that is not a directory is rejected before the query runs. |
| 94 | + |
| 95 | +# HISTORY |
| 96 | + |
| 97 | +**terraform query** shipped in **Terraform 1.14.0** (November 19, 2025) together with **list** blocks in **.tfquery.hcl** files, so existing infrastructure can be discovered and turned into import configuration. **Terraform 1.15.0** (April 29, 2026) taught **terraform fmt** to format those query files. |
| 98 | + |
| 99 | +Terraform itself was created by **Mitchell Hashimoto** at **HashiCorp** and first released in **2014**. |
| 100 | + |
| 101 | +# INSTALL |
| 102 | + |
| 103 | +```pacman: sudo pacman -S terraform``` |
| 104 | + |
| 105 | +```nix: nix profile install nixpkgs#terraform``` |
| 106 | + |
| 107 | +<!-- packages: 2026-09-27 --> |
| 108 | + |
| 109 | +# SEE ALSO |
| 110 | + |
| 111 | +[terraform](/man/terraform)(1), [terraform-init](/man/terraform-init)(1), [terraform-validate](/man/terraform-validate)(1), [terraform-plan](/man/terraform-plan)(1) |
| 112 | + |
| 113 | +# RESOURCES |
| 114 | + |
| 115 | +```[Documentation](https://developer.hashicorp.com/terraform/cli/commands/query)``` |
| 116 | + |
| 117 | +```[Homepage](https://www.terraform.io)``` |
| 118 | + |
| 119 | +```[Source code](https://github.com/hashicorp/terraform)``` |
| 120 | + |
| 121 | +<!-- verified: 2026-09-27 --> |
0 commit comments