terraform-search-import by hashicorp/agent-skills
npx skills add https://github.com/hashicorp/agent-skills --skill terraform-search-import使用声明式查询发现现有云资源,并生成配置以批量导入 Terraform 状态。
参考文档:
在开始之前,您必须验证目标资源类型是否受支持:
# 检查可用的 list 资源
./scripts/list_resources.sh aws # 特定提供商
./scripts/list_resources.sh # 所有已配置的提供商
确定目标资源类型 (例如,aws_s3_bucket, aws_instance)
检查是否支持 : 运行 ./scripts/list_resources.sh <provider>
选择工作流 :
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
注意 : 受支持资源的列表正在迅速扩展。在使用手动导入之前,请务必验证当前的支持情况。
在编写查询之前,请验证提供商是否支持针对您的目标资源类型的 list 资源。
运行辅助脚本从您的提供商中提取支持的 list 资源:
# 在包含提供商配置的目录中运行 (如果需要,会运行 terraform init)
./scripts/list_resources.sh aws # 特定提供商
./scripts/list_resources.sh # 所有已配置的提供商
或者手动查询提供商架构:
terraform providers schema -json | jq '.provider_schemas | to_entries | map({key: (.key | split("/")[-1]), value: (.value.list_resource_schemas // {} | keys)})'
Terraform 搜索需要一个已初始化的工作目录。在运行查询之前,请确保您有一个包含所需提供商的配置:
# terraform.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
运行 terraform init 以下载提供商,然后继续执行查询。
list 块的 .tfquery.hcl 文件terraform query 来发现匹配的资源-generate-config-out=<file> 生成配置resource 和 import 块terraform plan 和 terraform apply 进行导入查询文件使用 .tfquery.hcl 扩展名并支持:
provider 块list 块variable 和 locals 块# discovery.tfquery.hcl
provider "aws" {
region = "us-west-2"
}
list "aws_instance" "all" {
provider = aws
}
list "<list_type>" "<symbolic_name>" {
provider = <provider_reference> # 必需
# 可选:过滤器配置 (提供商特定)
# `config` 块的架构是提供商特定的。使用 `terraform providers schema -json | jq '.provider_schemas."registry.terraform.io/hashicorp/<provider>".list_resource_schemas."<resource_type>"'` 来发现可用选项
config {
filter {
name = "<filter_name>"
values = ["<value1>", "<value2>"]
}
region = "<region>" # AWS 特定
}
# 可选:限制结果数量
limit = 100
}
提供商对 list 资源的支持因版本而异。始终使用发现脚本检查您特定提供商版本的可用资源。
# 在配置的区域中查找所有 EC2 实例
list "aws_instance" "all" {
provider = aws
}
# 按标签查找实例
list "aws_instance" "production" {
provider = aws
config {
filter {
name = "tag:Environment"
values = ["production"]
}
}
}
# 按类型查找实例
list "aws_instance" "large" {
provider = aws
config {
filter {
name = "instance-type"
values = ["t3.large", "t3.xlarge"]
}
}
}
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
}
}
variable "target_environment" {
type = string
default = "staging"
}
list "aws_instance" "by_env" {
provider = aws
config {
filter {
name = "tag:Environment"
values = [var.target_environment]
}
}
}
# 执行查询并显示结果
terraform query
# 生成配置文件
terraform query -generate-config-out=imported.tf
# 传递变量
terraform query -var='target_environment=production'
list.aws_instance.all account_id=123456789012,id=i-0abc123,region=us-west-2 web-server
列:<query_address> <identity_attributes> <name_tag>
-generate-config-out 标志会创建:
# __generated__ by Terraform
resource "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"
}
}
生成的配置包含所有属性。通过以下方式进行清理:
# 之前:生成的
resource "aws_instance" "all_0" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
arn = "arn:aws:ec2:..." # 移除 - 计算属性
id = "i-0abc123" # 移除 - 计算属性
# ... 更多属性
}
# 之后:清理过的
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
}
}
生成的导入使用基于身份的导入 (Terraform 1.12+):
import {
to = aws_instance.web
provider = aws
identity = {
account_id = "123456789012"
id = "i-0abc123"
region = "us-west-2"
}
}
limit 防止输出过多| 问题 | 解决方案 |
|---|---|
| "未找到 list 资源" | 检查提供商版本是否支持 list 资源 |
| 查询返回空结果 | 验证区域和过滤器值 |
| 生成的配置有错误 | 移除计算属性,修复已弃用的参数 |
| 导入失败 | 确保资源尚未在状态中 |
# main.tf - 初始化提供商
terraform {
required_version = ">= 1.14"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0" # 始终使用最新版本
}
}
}
# 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
}
# 执行工作流
terraform init
terraform query
terraform query -generate-config-out=generated.tf
# 审查并清理 generated.tf
terraform plan
terraform apply
每周安装量
308
代码仓库
GitHub 星标数
477
首次出现
2026年3月3日
安全审计
安装于
codex299
opencode298
github-copilot298
gemini-cli296
amp296
kimi-cli296
Discover existing cloud resources using declarative queries and generate configuration for bulk import into Terraform state.
References:
BEFORE starting, you MUST verify the target resource type is supported:
# Check what list resources are available
./scripts/list_resources.sh aws # Specific provider
./scripts/list_resources.sh # All configured providers
Identify target resource type (e.g., aws_s3_bucket, aws_instance)
Check if supported : Run ./scripts/list_resources.sh <provider>
Choose workflow :
Note : The list of supported resources is rapidly expanding. Always verify current support before using manual import.
Before writing queries, verify the provider supports list resources for your target resource type.
Run the helper script to extract supported list resources from your provider:
# From a directory with provider configuration (runs terraform init if needed)
./scripts/list_resources.sh aws # Specific provider
./scripts/list_resources.sh # All configured providers
Or manually query the provider schema:
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:
# terraform.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
Run terraform init to download the provider, then proceed with queries.
.tfquery.hcl files with list blocks defining search queriesterraform query to discover matching resources-generate-config-out=<file>resource and import blocksterraform plan and terraform apply to importQuery files use .tfquery.hcl extension and support:
provider blocks for authentication
list blocks for resource discovery
variable and locals blocks for parameterization
provider "aws" { region = "us-west-2" }
list "aws_instance" "all" { provider = aws }
list "<list_type>" "<symbolic_name>" {
provider = <provider_reference> # Required
# Optional: filter configuration (provider-specific)
# 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>"'`
config {
filter {
name = "<filter_name>"
values = ["<value1>", "<value2>"]
}
region = "<region>" # AWS-specific
}
# Optional: limit results
limit = 100
}
Provider support for list resources varies by version. Always check what's available for your specific provider version using the discovery script.
# Find all EC2 instances in configured region
list "aws_instance" "all" {
provider = aws
}
# Find instances by tag
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"]
}
}
}
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
}
}
variable "target_environment" {
type = string
default = "staging"
}
list "aws_instance" "by_env" {
provider = aws
config {
filter {
name = "tag:Environment"
values = [var.target_environment]
}
}
}
# Execute queries and display results
terraform query
# Generate configuration file
terraform query -generate-config-out=imported.tf
# Pass variables
terraform query -var='target_environment=production'
list.aws_instance.all account_id=123456789012,id=i-0abc123,region=us-west-2 web-server
Columns: <query_address> <identity_attributes> <name_tag>
The -generate-config-out flag creates:
# __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"
}
}
Generated configuration includes all attributes. Clean up by:
# Before: 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
}
# 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
}
}
Generated imports use identity-based import (Terraform 1.12+):
import {
to = aws_instance.web
provider = aws
identity = {
account_id = "123456789012"
id = "i-0abc123"
region = "us-west-2"
}
}
limit to prevent overwhelming output| 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 |
# main.tf - Initialize provider
terraform {
required_version = ">= 1.14"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0" # Always use latest version
}
}
}
# discovery.tfquery.hcl - Define queries
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
}
# Execute workflow
terraform init
terraform query
terraform query -generate-config-out=generated.tf
# Review and clean generated.tf
terraform plan
terraform apply
Weekly Installs
308
Repository
GitHub Stars
477
First Seen
Mar 3, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex299
opencode298
github-copilot298
gemini-cli296
amp296
kimi-cli296
Azure Data Explorer (Kusto) 查询技能:KQL数据分析、日志遥测与时间序列处理
100,500 周安装