From 02eb293f7db281b270c085f37f6dffc2ef2b92b0 Mon Sep 17 00:00:00 2001 From: trujillo-adam Date: Sun, 14 Sep 2025 20:29:57 -0700 Subject: [PATCH 01/11] add reference topics --- .../v1.14.x (alpha)/data/cli-nav-data.json | 6 +- .../data/language-nav-data.json | 24 +- .../docs/cli/commands/query.mdx | 86 +++++ .../docs/language/block/tfquery/list.mdx | 325 ++++++++++++++++++ .../docs/language/files/tfquery.mdx | 25 ++ 5 files changed, 463 insertions(+), 3 deletions(-) create mode 100644 content/terraform/v1.14.x (alpha)/docs/cli/commands/query.mdx create mode 100644 content/terraform/v1.14.x (alpha)/docs/language/block/tfquery/list.mdx create mode 100644 content/terraform/v1.14.x (alpha)/docs/language/files/tfquery.mdx diff --git a/content/terraform/v1.14.x (alpha)/data/cli-nav-data.json b/content/terraform/v1.14.x (alpha)/data/cli-nav-data.json index ab233710c0..a1efc23990 100644 --- a/content/terraform/v1.14.x (alpha)/data/cli-nav-data.json +++ b/content/terraform/v1.14.x (alpha)/data/cli-nav-data.json @@ -59,9 +59,10 @@ { "title": "Overview", "path": "import" }, { "title": "Import existing resources", "path": "import/usage" }, { - "title": "Reference", + "title": "import command reference", "href": "/cli/commands/import" - } + }, + { "title": "query command reference", "href": "/cli/commands/query" } ] }, { @@ -370,6 +371,7 @@ { "title": "providers schema", "path": "commands/providers/schema" } ] }, + { "title": "query", "path": "commands/query" }, { "title": "refresh", "path": "commands/refresh" }, { "title": "show", "path": "commands/show" }, { diff --git a/content/terraform/v1.14.x (alpha)/data/language-nav-data.json b/content/terraform/v1.14.x (alpha)/data/language-nav-data.json index ebc5f2c523..141ad56054 100644 --- a/content/terraform/v1.14.x (alpha)/data/language-nav-data.json +++ b/content/terraform/v1.14.x (alpha)/data/language-nav-data.json @@ -30,7 +30,8 @@ { "title": "Overview", "path": "files" }, { "title": "Override files", "path": "files/override" }, { "title": "Dependency lock file", "path": "files/dependency-lock" }, - { "title": "Test files", "path": "files/tests" } + { "title": "Test files", "path": "files/tests" }, + { "title": "Query files", "path": "files/tfquery" } ] }, { @@ -326,6 +327,27 @@ } ] }, + { + "title": "tfquery blocks", + "routes": [ + { + "title": "list", + "path": "block/tfquery/list" + }, + { + "title": "locals", + "href": "/language/block/locals" + }, + { + "title": "provider", + "href": "/language/block/provider" + }, + { + "title": "variable", + "href": "/language/block/variable" + } + ] + }, { "title": "Meta-arguments", "path": "meta-arguments" diff --git a/content/terraform/v1.14.x (alpha)/docs/cli/commands/query.mdx b/content/terraform/v1.14.x (alpha)/docs/cli/commands/query.mdx new file mode 100644 index 0000000000..b240b49a6f --- /dev/null +++ b/content/terraform/v1.14.x (alpha)/docs/cli/commands/query.mdx @@ -0,0 +1,86 @@ +--- +page_title: locals block reference for `.tfquery.hcl` files +description: |- + Add the `locals` block to your `.tfquery.hcl` files define values to reuse within the local Terraform module. +--- + +# `terraform query` command + +The `terraform query` command queries existing infrastructure for unmanaged resources according to the `tfquery.hcl` file so that you can import them into your Terraform workspace. + +## Usage + +```shell-session +$ terraform query [flags] [options] +``` + +## Description + +When you run the `terraform query` command, Terraform searches the current configuration directory for `tfquery.hcl` files so that it can query remote infrastructure for resources that match the defined `list` blocks. Terraform prints the results to the terminal. You 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. + +## Flags + +- `-var '='`: 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. + - Data type: String. + - Example: [Set input variables](#set-input-variabls) + +- `-var-file=`: Specifies a file containing input variable values, in addition to the default `terraform.tfvars` and `*.auto.tfvars` files. + - Data type: Filename + - Example: [Set input variables](#set-input-variabls) + +## Options + +- `-generate-config-out=`: 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. + - Data type: Path + - Example: [Generate import configuration](#generate-import-configuration) + +- `-json`: Instructs Terraform to print results to the console in JSON format. + - Data type: Boolean + - Example: [Generate import configuration](#generate-import-configuration) + +- `-no-color`: Terraform prints monochrome results. + - Data type: Boolean +## Examples + +The following examples show how to use the command for common use cases. + +### Set input variables + +The following command passes sets the value of the `env` input variable to `prod`: + +```shell-session +$ terraform query -var 'env=prod' +``` + +Corresponding `tfquery.hcl` configuration: + +```hcl +list "aws_instance" "instances" { + provider = aws +} + +variable "env" { + type = string + default = "test" +} +``` + +The following command loads variable values defined in `my-vars.tfvars`: + +```shell-session +$ terraform query -var-file=my-vars.tfvars +``` + +### Generate inport configuration + +The following command generates `import` and `resource` blocks for the query results to a file in the `/to-import` directory: + +```shell-session +$ terraform query -generate-config=to-import +``` + +The following command prints `import` and `resource` blocks for the query results as json: + +```shell-session +$ terraform query -generate-config=to-import -json +``` diff --git a/content/terraform/v1.14.x (alpha)/docs/language/block/tfquery/list.mdx b/content/terraform/v1.14.x (alpha)/docs/language/block/tfquery/list.mdx new file mode 100644 index 0000000000..ba5d47f4ba --- /dev/null +++ b/content/terraform/v1.14.x (alpha)/docs/language/block/tfquery/list.mdx @@ -0,0 +1,325 @@ +--- +page_title: list block reference for `.tfquery.hcl` files +description: |- + Add the `list` block to your `.tfquery.hcl` files to declare Terraform providers and configure them when querying existing infrastructure for resources to import. +--- + +# `list` block reference + +The `list` block defines a query that reads existing infrastructure and returns unmanaged resources that you can import to your Terraform workspace. You can only add `list` blocks to configuration files with `.tfquery.hcl` extensions. This topic provides reference information about configuring `list` blocks in `.tfquery.hcl` files. Refer to [Import existing resources in bulk](/terraform/language/import/bulk) for information about querying and importing resources. + +## Configuration model + +A `list` block supports the following configuration: + +- [`list "" "