Skip to content

Commit f32f1f2

Browse files
Atru/query and import bulk (#942)
this PR adds documentation for configuring constructs associated with querying resources and importing them in bulk, such as the list block and the terraform query command.. These are beta features in 1.14
2 parents 90d25c7 + 7bcbb3a commit f32f1f2

File tree

10 files changed

+804
-164
lines changed

10 files changed

+804
-164
lines changed

content/terraform/v1.14.x (alpha)/data/cli-nav-data.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@
5959
{ "title": "Overview", "path": "import" },
6060
{ "title": "Import existing resources", "path": "import/usage" },
6161
{
62-
"title": "Reference",
62+
"title": "import command reference",
6363
"href": "/cli/commands/import"
64+
},
65+
{
66+
"title": "query command reference",
67+
"href": "/cli/commands/query"
6468
}
6569
]
6670
},
@@ -281,6 +285,7 @@
281285
"title": "<code>providers schema</code>",
282286
"href": "/cli/commands/providers/schema"
283287
},
288+
{ "title": "<code>query</code>", "href": "/cli/commands/query" },
284289
{ "title": "<code>refresh</code>", "href": "/cli/commands/refresh" },
285290
{ "title": "<code>show</code>", "href": "/cli/commands/show" },
286291
{ "title": "<code>state</code>", "href": "/cli/commands/state" },
@@ -370,6 +375,7 @@
370375
{ "title": "providers schema", "path": "commands/providers/schema" }
371376
]
372377
},
378+
{ "title": "query", "path": "commands/query" },
373379
{ "title": "refresh", "path": "commands/refresh" },
374380
{ "title": "show", "path": "commands/show" },
375381
{

content/terraform/v1.14.x (alpha)/data/language-nav-data.json

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
{ "title": "Overview", "path": "files" },
3131
{ "title": "Override files", "path": "files/override" },
3232
{ "title": "Dependency lock file", "path": "files/dependency-lock" },
33-
{ "title": "Test files", "path": "files/tests" }
33+
{ "title": "Test files", "path": "files/tests" },
34+
{ "title": "Query files", "path": "files/tfquery" }
3435
]
3536
},
3637
{
@@ -191,11 +192,16 @@
191192
{
192193
"title": "Import existing resources",
193194
"routes": [
194-
{ "title": "Import a resource", "path": "import" },
195-
{
196-
"title": "Generate resource configuration",
197-
"path": "import/generating-configuration"
198-
}
195+
{ "title": "Overview", "path": "import"},
196+
{
197+
"title": "Import resources in bulk",
198+
"path": "import/bulk"
199+
},
200+
{ "title": "Import a single resource", "path": "import/single-resource" },
201+
{
202+
"title": "Generate configuration for single imports",
203+
"path": "import/generating-configuration"
204+
}
199205
]
200206
},
201207
{
@@ -326,6 +332,32 @@
326332
}
327333
]
328334
},
335+
{
336+
"title": "Query blocks",
337+
"routes": [
338+
{
339+
"title": "list",
340+
"path": "block/tfquery/list",
341+
"badge": {
342+
"text": "BETA",
343+
"type": "outlined",
344+
"color": "neutral"
345+
}
346+
},
347+
{
348+
"title": "locals",
349+
"href": "/language/block/locals"
350+
},
351+
{
352+
"title": "provider",
353+
"href": "/language/block/provider"
354+
},
355+
{
356+
"title": "variable",
357+
"href": "/language/block/variable"
358+
}
359+
]
360+
},
329361
{
330362
"title": "Meta-arguments",
331363
"path": "meta-arguments"
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)