terraform-search-import
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTerraform Search and Bulk Import
Terraform 搜索与批量导入
Discover existing cloud resources using declarative queries and generate configuration for bulk import into Terraform state.
References:
When to Use
适用场景
- Bringing unmanaged resources under Terraform control
- Auditing existing cloud infrastructure
- Migrating from manual provisioning to IaC
- Discovering resources across multiple regions/accounts
- 将未受管控的资源纳入Terraform管控
- 审计现有云基础设施
- 从手动配置迁移至IaC
- 跨多个区域/账号发现资源
IMPORTANT: Check Provider Support First
重要提示:请先检查提供商支持情况
BEFORE starting, you MUST verify the target resource type is supported:
bash
undefined开始操作前,必须验证目标资源类型是否受支持:
bash
undefinedCheck what list resources are available
查看可用的list资源
./scripts/list_resources.sh aws # Specific provider
./scripts/list_resources.sh # All configured providers
undefined./scripts/list_resources.sh aws # 指定提供商
./scripts/list_resources.sh # 所有已配置的提供商
undefinedDecision Tree
决策流程
-
Identify target resource type (e.g., aws_s3_bucket, aws_instance)
-
Check if supported: Run
./scripts/list_resources.sh <provider> -
Choose workflow:
- ** If supported**: Check for terraform version available.
- ** If terraform version is above 1.14.0** Use Terraform Search workflow (below)
- ** If not supported or terraform version is below 1.14.0 **: Use Manual Discovery workflow (see references/MANUAL-IMPORT.md)
Note: The list of supported resources is rapidly expanding. Always verify current support before using manual import.
-
确定目标资源类型(例如:aws_s3_bucket、aws_instance)
-
检查支持情况:运行
./scripts/list_resources.sh <provider> -
选择工作流:
- 若受支持:检查可用的Terraform版本。
- 若Terraform版本高于1.14.0:使用下方的Terraform Search工作流
- 若不受支持或Terraform版本低于1.14.0:使用手动发现工作流(参见 references/MANUAL-IMPORT.md)
注意:受支持的资源列表正在快速扩充。使用手动导入前,请务必确认当前的支持情况。
Prerequisites
前置条件
Before writing queries, verify the provider supports list resources for your target resource type.
编写查询之前,请确认提供商支持目标资源类型的list资源。
Discover Available List Resources
发现可用的List资源
Run the helper script to extract supported list resources from your provider:
bash
undefined运行辅助脚本从提供商中提取受支持的list资源:
bash
undefinedFrom a directory with provider configuration (runs terraform init if needed)
在包含提供商配置的目录中运行(如有需要会执行terraform init)
./scripts/list_resources.sh aws # Specific provider
./scripts/list_resources.sh # All configured providers
Or manually query the provider schema:
```bash
terraform providers schema -json | jq '.provider_schemas | to_entries | map({key: (.key | split("/")[-1]), value: (.value.list_resource_schemas // {} | keys)})'Terraform Search requires an initialized working directory. Ensure you have a configuration with the required provider before running queries:
hcl
undefined./scripts/list_resources.sh aws # 指定提供商
./scripts/list_resources.sh # 所有已配置的提供商
或手动查询提供商架构:
```bash
terraform providers schema -json | jq '.provider_schemas | to_entries | map({key: (.key | split("/")[-1]), value: (.value.list_resource_schemas // {} | keys)})'Terraform Search需要已初始化的工作目录。运行查询前,请确保您的配置中包含所需的提供商:
hcl
undefinedterraform.tf
terraform.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
Run `terraform init` to download the provider, then proceed with queries.terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
运行 `terraform init` 下载提供商,然后继续执行查询。Terraform Search Workflow (Supported Resources Only)
Terraform Search工作流(仅适用于受支持的资源)
- Create files with
.tfquery.hclblocks defining search querieslist - Run to discover matching resources
terraform query - Generate configuration with
-generate-config-out=<file> - Review and refine generated and
resourceblocksimport - Run and
terraform planto importterraform apply
- 创建包含块的
list文件,定义搜索查询.tfquery.hcl - 运行 发现匹配的资源
terraform query - 使用 生成配置
-generate-config-out=<file> - 审阅并优化生成的和
resource块import - 运行 和
terraform plan完成导入terraform apply
Query File Structure
查询文件结构
Query files use extension and support:
.tfquery.hcl- blocks for authentication
provider - blocks for resource discovery
list - and
variableblocks for parameterizationlocals
hcl
undefined查询文件使用扩展名,支持:
.tfquery.hcl- 用于身份验证的块
provider - 用于资源发现的块
list - 用于参数化的和
variable块locals
hcl
undefineddiscovery.tfquery.hcl
discovery.tfquery.hcl
provider "aws" {
region = "us-west-2"
}
list "aws_instance" "all" {
provider = aws
}
undefinedprovider "aws" {
region = "us-west-2"
}
list "aws_instance" "all" {
provider = aws
}
undefinedList Block Syntax
List块语法
hcl
list "<list_type>" "<symbolic_name>" {
provider = <provider_reference> # Required
# Optional: filter configuration (provider-specific)
config {
filter {
name = "<filter_name>"
values = ["<value1>", "<value2>"]
}
region = "<region>" # AWS-specific
}
}Note: The block schema is provider-specific. Discover available options:
configbash
terraform providers schema -json | jq '.provider_schemas."registry.terraform.io/hashicorp/<provider>".list_resource_schemas."<resource_type>"'}
Optional: limit results
limit = 100
}
undefinedhcl
list "<list_type>" "<symbolic_name>" {
provider = <provider_reference> # 必填
# 可选:过滤配置(提供商特定)
config {
filter {
name = "<filter_name>"
values = ["<value1>", "<value2>"]
}
region = "<region>" # AWS特定
}
}注意:块的架构由提供商决定。可通过以下方式发现可用选项:
configbash
terraform providers schema -json | jq '.provider_schemas."registry.terraform.io/hashicorp/<provider>".list_resource_schemas."<resource_type>"'可选:限制结果数量
limit = 100
}
undefinedSupported List Resources
受支持的List资源
Provider support for list resources varies by version. Always check what's available for your specific provider version using the discovery script.
提供商对list资源的支持因版本而异。请始终使用发现脚本检查您所使用的特定提供商版本支持的资源。
Query Examples
查询示例
Basic Discovery
基础发现
hcl
undefinedhcl
undefinedFind all EC2 instances in configured region
查找已配置区域中的所有EC2实例
list "aws_instance" "all" {
provider = aws
}
undefinedlist "aws_instance" "all" {
provider = aws
}
undefinedFiltered Discovery
过滤发现
hcl
undefinedhcl
undefinedFind instances by tag
按标签查找实例
list "aws_instance" "production" {
provider = aws
config {
filter {
name = "tag:Environment"
values = ["production"]
}
}
}
list "aws_instance" "production" {
provider = aws
config {
filter {
name = "tag:Environment"
values = ["production"]
}
}
}
Find instances by type
按实例类型查找
list "aws_instance" "large" {
provider = aws
config {
filter {
name = "instance-type"
values = ["t3.large", "t3.xlarge"]
}
}
}
undefinedlist "aws_instance" "large" {
provider = aws
config {
filter {
name = "instance-type"
values = ["t3.large", "t3.xlarge"]
}
}
}
undefinedMulti-Region Discovery
多区域发现
hcl
provider "aws" {
region = "us-west-2"
}
locals {
regions = ["us-west-2", "us-east-1", "eu-west-1"]
}
list "aws_instance" "all_regions" {
for_each = toset(local.regions)
provider = aws
config {
region = each.value
}
}hcl
provider "aws" {
region = "us-west-2"
}
locals {
regions = ["us-west-2", "us-east-1", "eu-west-1"]
}
list "aws_instance" "all_regions" {
for_each = toset(local.regions)
provider = aws
config {
region = each.value
}
}Parameterized Queries
参数化查询
hcl
variable "target_environment" {
type = string
default = "staging"
}
list "aws_instance" "by_env" {
provider = aws
config {
filter {
name = "tag:Environment"
values = [var.target_environment]
}
}
}hcl
variable "target_environment" {
type = string
default = "staging"
}
list "aws_instance" "by_env" {
provider = aws
config {
filter {
name = "tag:Environment"
values = [var.target_environment]
}
}
}Running Queries
运行查询
bash
undefinedbash
undefinedExecute queries and display results
执行查询并显示结果
terraform query
terraform query
Generate configuration file
生成配置文件
terraform query -generate-config-out=imported.tf
terraform query -generate-config-out=imported.tf
Pass variables
传递变量
terraform query -var='target_environment=production'
undefinedterraform query -var='target_environment=production'
undefinedQuery Output Format
查询输出格式
list.aws_instance.all account_id=123456789012,id=i-0abc123,region=us-west-2 web-serverColumns:
<query_address> <identity_attributes> <name_tag>list.aws_instance.all account_id=123456789012,id=i-0abc123,region=us-west-2 web-server列说明:
<query_address> <identity_attributes> <name_tag>Generated Configuration
生成的配置
The flag creates:
-generate-config-outhcl
undefined-generate-config-outhcl
undefinedgenerated by Terraform
generated by Terraform
resource "aws_instance" "all_0" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
... all attributes
}
import {
to = aws_instance.all_0
provider = aws
identity = {
account_id = "123456789012"
id = "i-0abc123"
region = "us-west-2"
}
}
undefinedresource "aws_instance" "all_0" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
... 所有属性
}
import {
to = aws_instance.all_0
provider = aws
identity = {
account_id = "123456789012"
id = "i-0abc123"
region = "us-west-2"
}
}
undefinedPost-Generation Cleanup
生成后清理
Generated configuration includes all attributes. Clean up by:
- Remove computed/read-only attributes
- Replace hardcoded values with variables
- Add proper resource naming
- Organize into appropriate files
hcl
undefined生成的配置包含所有属性。可通过以下方式清理:
- 移除计算/只读属性
- 用变量替换硬编码值
- 添加合适的资源命名
- 整理到对应的文件中
hcl
undefinedBefore: generated
之前:生成的配置
resource "aws_instance" "all_0" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
arn = "arn:aws:ec2:..." # Remove - computed
id = "i-0abc123" # Remove - computed
... many more attributes
}
resource "aws_instance" "all_0" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
arn = "arn:aws:ec2:..." # 移除 - 计算属性
id = "i-0abc123" # 移除 - 计算属性
... 更多属性
}
After: cleaned
之后:清理后的配置
resource "aws_instance" "web_server" {
ami = var.ami_id
instance_type = var.instance_type
subnet_id = var.subnet_id
tags = {
Name = "web-server"
Environment = var.environment
}
}
undefinedresource "aws_instance" "web_server" {
ami = var.ami_id
instance_type = var.instance_type
subnet_id = var.subnet_id
tags = {
Name = "web-server"
Environment = var.environment
}
}
undefinedImport by Identity
通过身份信息导入
Generated imports use identity-based import (Terraform 1.12+):
hcl
import {
to = aws_instance.web
provider = aws
identity = {
account_id = "123456789012"
id = "i-0abc123"
region = "us-west-2"
}
}生成的导入配置使用基于身份的导入(Terraform 1.12+):
hcl
import {
to = aws_instance.web
provider = aws
identity = {
account_id = "123456789012"
id = "i-0abc123"
region = "us-west-2"
}
}Best Practices
最佳实践
Query Design
查询设计
- Start broad, then add filters to narrow results
- Use to prevent overwhelming output
limit - Test queries before generating configuration
- 先从宽泛的查询开始,再添加过滤条件缩小结果范围
- 使用避免输出结果过多
limit - 生成配置前先测试查询
Configuration Management
配置管理
- Review all generated code before applying
- Remove unnecessary default values
- Use consistent naming conventions
- Add proper variable abstraction
- 应用前审阅所有生成的代码
- 移除不必要的默认值
- 使用一致的命名规范
- 添加合适的变量抽象
Troubleshooting
故障排除
| Issue | Solution |
|---|---|
| "No list resources found" | Check provider version supports list resources |
| Query returns empty | Verify region and filter values |
| Generated config has errors | Remove computed attributes, fix deprecated arguments |
| Import fails | Ensure resource not already in state |
| 问题 | 解决方案 |
|---|---|
| "未找到list资源" | 检查提供商版本是否支持list资源 |
| 查询返回空结果 | 验证区域和过滤值是否正确 |
| 生成的配置存在错误 | 移除计算属性,修复已弃用的参数 |
| 导入失败 | 确保资源未已存在于状态中 |
Complete Example
完整示例
hcl
undefinedhcl
undefinedmain.tf - Initialize provider
main.tf - 初始化提供商
terraform {
required_version = ">= 1.14"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0" # Always use latest version
}
}
}
terraform {
required_version = ">= 1.14"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0" # 始终使用最新版本
}
}
}
discovery.tfquery.hcl - Define queries
discovery.tfquery.hcl - 定义查询
provider "aws" {
region = "us-west-2"
}
list "aws_instance" "team_instances" {
provider = aws
config {
filter {
name = "tag:Owner"
values = ["platform"]
}
filter {
name = "instance-state-name"
values = ["running"]
}
}
limit = 50
}
```bashprovider "aws" {
region = "us-west-2"
}
list "aws_instance" "team_instances" {
provider = aws
config {
filter {
name = "tag:Owner"
values = ["platform"]
}
filter {
name = "instance-state-name"
values = ["running"]
}
}
limit = 50
}
```bashExecute workflow
执行工作流
terraform init
terraform query
terraform query -generate-config-out=generated.tf
terraform init
terraform query
terraform query -generate-config-out=generated.tf
Review and clean generated.tf
审阅并清理generated.tf
terraform plan
terraform apply
undefinedterraform plan
terraform apply
undefined