|
| 1 | +--- |
| 2 | +page_title: locals block reference for `.tfquery.hcl` files |
| 3 | +description: |- |
| 4 | + Add the `locals` block to your `.tfquery.hcl` files define values to reuse within the local Terraform module. |
| 5 | +--- |
| 6 | + |
| 7 | +# `terraform query` command |
| 8 | + |
| 9 | +The `terraform query` command queries existing infrastructure for resources according to the `tfquery.hcl` file so that you can import them into your Terraform workspace. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +```shell-session |
| 14 | +$ terraform query [flags] [options] |
| 15 | +``` |
| 16 | + |
| 17 | +## Description |
| 18 | + |
| 19 | +When you run the `terraform query` command, Terraform checks the current configuration directory for files with a `.tfquery.hcl` extension. Then, Terraform queries remote infrastructure for resources that match the `list` blocks defined in the query file and prints the results to the terminal. You can also add the `-generate-config-out` flag to generate configuration that you can use to import the resources. Refer to [Import resources in bulk](/terraform/language/import/bulk) for more information. |
| 20 | + |
| 21 | +## Flags |
| 22 | + |
| 23 | +- `-var '<INPUT-VARIABLE>=<VALUE>'`: Specifies a key-value pair that sets an input variable defined in the `tfquery.hcl` configuration. You can use this flag more than once to set additional input variables. |
| 24 | + - Data type: String. |
| 25 | + - Example: [Set input variables](#set-input-variabls) |
| 26 | + |
| 27 | +- `-var-file=<FILENAME>`: Specifies a file containing input variable values, in addition to the default `terraform.tfvars` and `*.auto.tfvars` files. |
| 28 | + - Data type: Filename |
| 29 | + - Example: [Set input variables](#set-input-variabls) |
| 30 | + |
| 31 | +## Options |
| 32 | + |
| 33 | +- `-generate-config-out=<PATH/TO/OUTPUT>`: Instructs Terraform to generate `import` and `resource` blocks for results so that you can import them. Terraform writes the configuration to a new file. The file must not already exist. When used with the `-json` option, Terraform generates the configuration as part of the JSON output instead of writing it to a file. |
| 34 | + - Data type: Path |
| 35 | + - Example: [Generate import configuration](#generate-import-configuration) |
| 36 | + |
| 37 | +- `-json`: Instructs Terraform to print results to the console in JSON format. |
| 38 | + - Data type: Boolean |
| 39 | + - Example: [Generate import configuration](#generate-import-configuration) |
| 40 | + |
| 41 | +- `-no-color`: Terraform prints monochrome results. |
| 42 | + - Data type: Boolean |
| 43 | +## Examples |
| 44 | + |
| 45 | +The following examples show how to use the command for common use cases. |
| 46 | + |
| 47 | +### Set input variables |
| 48 | + |
| 49 | +The following command passes sets the value of the `env` input variable to `prod`: |
| 50 | + |
| 51 | +```shell-session |
| 52 | +$ terraform query -var 'env=prod' |
| 53 | +``` |
| 54 | + |
| 55 | +Corresponding `tfquery.hcl` configuration: |
| 56 | + |
| 57 | +```hcl |
| 58 | +list "aws_instance" "instances" { |
| 59 | + provider = aws |
| 60 | +} |
| 61 | +
|
| 62 | +variable "env" { |
| 63 | + type = string |
| 64 | + default = "test" |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +The following command loads variable values defined in `my-vars.tfvars`: |
| 69 | + |
| 70 | +```shell-session |
| 71 | +$ terraform query -var-file=my-vars.tfvars |
| 72 | +``` |
| 73 | + |
| 74 | +### Generate import configuration |
| 75 | + |
| 76 | +The following command generates `import` and `resource` blocks for the query results to a file in the `to-import` directory: |
| 77 | + |
| 78 | +```shell-session |
| 79 | +$ terraform query -generate-config=to-import/file.tf |
| 80 | +``` |
| 81 | + |
| 82 | +The following command prints `import` and `resource` blocks for the query results as JSON: |
| 83 | + |
| 84 | +```shell-session |
| 85 | +$ terraform query -generate-config=file.tf -json |
| 86 | +``` |
0 commit comments