$npx -y skills add hashicorp/agent-skills --skill terraform-search-importDiscover existing cloud resources using Terraform Search queries and bulk import them into Terraform management. Use when bringing unmanaged infrastructure under Terraform control, auditing cloud resources, or migrating to IaC.
| 1 | # Terraform Search and Bulk Import |
| 2 | |
| 3 | Discover existing cloud resources using declarative queries and generate configuration for bulk import into Terraform state. |
| 4 | |
| 5 | **References:** |
| 6 | - [Terraform Search - list block](https://developer.hashicorp.com/terraform/language/block/tfquery/list) |
| 7 | - [Bulk Import](https://developer.hashicorp.com/terraform/language/import/bulk) |
| 8 | |
| 9 | ## When to Use |
| 10 | |
| 11 | - Bringing unmanaged resources under Terraform control |
| 12 | - Auditing existing cloud infrastructure |
| 13 | - Migrating from manual provisioning to IaC |
| 14 | - Discovering resources across multiple regions/accounts |
| 15 | |
| 16 | ## IMPORTANT: Check Provider Support First |
| 17 | |
| 18 | **BEFORE starting, you MUST verify the target resource type is supported:** |
| 19 | |
| 20 | ```bash |
| 21 | # Check what list resources are available |
| 22 | ./scripts/list_resources.sh aws # Specific provider |
| 23 | ./scripts/list_resources.sh # All configured providers |
| 24 | ``` |
| 25 | |
| 26 | ## Decision Tree |
| 27 | |
| 28 | 1. **Identify target resource type** (e.g., aws_s3_bucket, aws_instance) |
| 29 | 2. **Check if supported**: Run `./scripts/list_resources.sh <provider>` |
| 30 | 3. **Choose workflow**: |
| 31 | - ** If supported**: Check for terraform version available. |
| 32 | - ** If terraform version is above 1.14.0** Use Terraform Search workflow (below) |
| 33 | - ** If not supported or terraform version is below 1.14.0 **: Use Manual Discovery workflow (see [references/MANUAL-IMPORT.md](references/MANUAL-IMPORT.md)) |
| 34 | |
| 35 | **Note**: The list of supported resources is rapidly expanding. Always verify current support before using manual import. |
| 36 | |
| 37 | ## Prerequisites |
| 38 | |
| 39 | Before writing queries, verify the provider supports list resources for your target resource type. |
| 40 | |
| 41 | ### Discover Available List Resources |
| 42 | |
| 43 | Run the helper script to extract supported list resources from your provider: |
| 44 | |
| 45 | ```bash |
| 46 | # From a directory with provider configuration (runs terraform init if needed) |
| 47 | ./scripts/list_resources.sh aws # Specific provider |
| 48 | ./scripts/list_resources.sh # All configured providers |
| 49 | ``` |
| 50 | |
| 51 | Or manually query the provider schema: |
| 52 | |
| 53 | ```bash |
| 54 | terraform providers schema -json | jq '.provider_schemas | to_entries | map({key: (.key | split("/")[-1]), value: (.value.list_resource_schemas // {} | keys)})' |
| 55 | ``` |
| 56 | |
| 57 | Terraform Search requires an initialized working directory. Ensure you have a configuration with the required provider before running queries: |
| 58 | |
| 59 | ```hcl |
| 60 | # terraform.tf |
| 61 | terraform { |
| 62 | required_providers { |
| 63 | aws = { |
| 64 | source = "hashicorp/aws" |
| 65 | version = "~> 6.0" |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | ``` |
| 70 | |
| 71 | Run `terraform init` to download the provider, then proceed with queries. |
| 72 | |
| 73 | ## Terraform Search Workflow (Supported Resources Only) |
| 74 | |
| 75 | 1. Create `.tfquery.hcl` files with `list` blocks defining search queries |
| 76 | 2. Run `terraform query` to discover matching resources |
| 77 | 3. Generate configuration with `-generate-config-out=<file>` |
| 78 | 4. Review and refine generated `resource` and `import` blocks |
| 79 | 5. Run `terraform plan` and `terraform apply` to import |
| 80 | |
| 81 | ## Query File Structure |
| 82 | |
| 83 | Query files use `.tfquery.hcl` extension and support: |
| 84 | - `provider` blocks for authentication |
| 85 | - `list` blocks for resource discovery |
| 86 | - `variable` and `locals` blocks for parameterization |
| 87 | |
| 88 | ```hcl |
| 89 | # discovery.tfquery.hcl |
| 90 | provider "aws" { |
| 91 | region = "us-west-2" |
| 92 | } |
| 93 | |
| 94 | list "aws_instance" "all" { |
| 95 | provider = aws |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | ## List Block Syntax |
| 100 | |
| 101 | ```hcl |
| 102 | list "<list_type>" "<symbolic_name>" { |
| 103 | provider = <provider_reference> # Required |
| 104 | |
| 105 | # Optional: filter configuration (provider-specific) |
| 106 | # The `config` block schema is provider-specific. Discover available options using `terraform providers schema -json | jq '.provider_schemas."registry.terraform.io/hashicorp/<provider>".list_resource_schemas."<resource_type>"'` |
| 107 | |
| 108 | config { |
| 109 | filter { |
| 110 | name = "<filter_name>" |
| 111 | values = ["<value1>", "<value2>"] |
| 112 | } |
| 113 | region = "<region>" # AWS-specific |
| 114 | } |
| 115 | # Optional: limit results |
| 116 | limit = 100 |
| 117 | } |
| 118 | ``` |
| 119 | |
| 120 | ## Supported List Resources |
| 121 | |
| 122 | Provider support for list resources varies by version. **Always check what's available for your specific provider version using the discovery script.** |
| 123 | |
| 124 | ## Query Examples |
| 125 | |
| 126 | ### Basic Discovery |
| 127 | |
| 128 | ```hcl |
| 129 | # Find all EC2 instances in configured region |
| 130 | list "aws_instance" "all" { |
| 131 | provider = aws |
| 132 | } |
| 133 | ``` |
| 134 | |
| 135 | ### Filtered Discovery |
| 136 | |
| 137 | ```hcl |
| 138 | # Find instances by tag |
| 139 | list "aws_instance" "production" { |
| 140 | provider = aws |
| 141 | |
| 142 | config { |
| 143 | filter { |
| 144 | name = "tag:Environment" |
| 145 | values = ["production"] |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | # Find instances by type |
| 151 | list "aws_instance" "large" { |
| 152 | provider = aws |
| 153 | |
| 154 | config { |
| 155 | filter { |
| 156 | name = "instance-type" |
| 157 | values = ["t3.large", "t3.xlarge"] |
| 158 | } |