Terraform配置生成工具

fcp-terraform是一个基于Go语言的工具,用于通过描述基础设施意图生成有效的Terraform HCL配置,支持资源、数据源、变量和输出的管理。

  • by os-tack
  • Go
  • 本地部署
  • 更新于
在 GitHub 查看 aetherwing-io/fcp-terraform

使用说明

fcp地形

Go中FCP模式的参考实现,带有嵌入式 fcpcore 港口(internal/fcpcore/).Terraform HCL是一种大多数LLM已经可以流利地手工编写的格式,因此该项目的主要价值不是作为一种生产创作工具,而是作为一个3层FCP架构(MCP意图层->语义域模型->FCP核心)的工作示例,该架构通过HashiCorp在Go中原生实现,针对真实的、非平凡的目标格式 hclwrite.

它的作用

fcp-terraform允许LLM通过描述基础设施意图(资源、数据源、变量、输出)来构建terraform配置,并将其呈现为有效的HCL。调用者发送以下操作,而不是编写原始的HCL语法 add resource aws_instance web ami:"ami-0c55b159" instance_type:t2.micro fcp-terraform管理语义模型、依赖图和序列化。建立在 FCP 框架。

使用HashiCorp的Go编写 hclwrite 用于原生HCL AST生成的库——无需字符串连接或模板呈现。

快速示例

terraform_session('new "Main Infrastructure"')

terraform([
  'add resource aws_instance web ami:"ami-0c55b159" instance_type:t2.micro',
  'add resource aws_s3_bucket assets bucket:"my-assets"',
  'add variable region default:"us-east-1" type:string',
  'add output instance_ip value:"aws_instance.web.public_ip"',
])

terraform_query('plan')

这 plan 查询产生:

variable "region" {
  type    = string
  default = "us-east-1"
}

resource "aws_instance" "web" {
  ami           = "ami-0c55b159"
  instance_type = "t2.micro"
}

resource "aws_s3_bucket" "assets" {
  bucket = "my-assets"
}

output "instance_ip" {
  value = aws_instance.web.public_ip
}

可用的MCP工具

工具 目的
terraform(ops) 批量突变——添加、设置、删除、连接、嵌套、标签、样式
terraform_query(q) 检查配置——映射、列表、描述、计划、图表、查找
terraform_session(action) 生命周期——新建、打开、保存、检查点、撤消、重做
terraform_help() 完整参考卡

动词引用

动词 语法
add resource add resource TYPE LABEL [key:value...]
add provider add provider PROVIDER [region:R] [key:value...]
add variable add variable NAME [type:T] [default:V] [description:D]
add output add output NAME value:EXPR [description:D]
add data add data TYPE LABEL [key:value...]
add module add module LABEL source:PATH [key:value...]
connect connect SRC -> TGT [label:TEXT] --发出真实 depends_on = [...] SRC的HCL块上的条目,除了记录 graph 查询
set set LABEL key:value [key:value...]
nest nest LABEL BLOCK_TYPE[/CHILD_TYPE] [key:value...]
remove remove LABEL 或 remove @SELECTOR
label label OLD_LABEL "new_label"
style style LABEL tags:"Key=Val,Key2=Val2"

选择器

@type:aws_instance      All resources of a given type
@provider:aws            All blocks from a given provider
@kind:resource           All blocks of kind (resource, variable, output, data)
@tag:KEY or @tag:KEY=VAL Blocks matching a tag
@all                     All blocks

安装

去安装

go install github.com/os-tack/fcp-terraform/cmd/fcp-terraform@latest

GitHub发布

从下载预构建的二进制文件 发布 并把 fcp-terraform 在你的路上。

MCP客户端配置

{
  "mcpServers": {
    "fcp-terraform": {
      "command": "fcp-terraform"
    }
  }
}

建筑

cmd/fcp-terraform/main.go     MCP server — 4 tools, stdio transport
        │
internal/terraform/            Domain layer
  ├── model.go                 Semantic model (blocks, attributes, connections)
  ├── adapter.go               FCP adapter (dispatch ops → handlers)
  ├── handlers.go              Verb handlers (add, set, remove, nest, etc.)
  ├── queries.go               Query dispatcher (plan, graph, describe, etc.)
  ├── selectors.go             @type, @provider, @kind, @tag, @all
  ├── values.go                Attribute type inference, hclwrite value generation
  ├── verb_specs.go            Verb specifications and reference card sections
  ├── block_ref.go             Terraform reference detection
  └── index.go                 Label index for O(1) lookups
        │
internal/fcpcore/              Shared FCP framework (Go port)
  ├── tokenizer.go             DSL tokenizer
  ├── parsed_op.go             Operation parser
  ├── verb_registry.go         Verb spec registry + reference card generator
  ├── event_log.go             Event sourcing (undo/redo)
  ├── session.go               Session lifecycle (new, open, save, checkpoint)
  └── formatter.go             Response formatter

HCL代使用HashiCorp的 hclwrite 用于本地AST操作的软件包。从资源类型前缀自动检测提供程序(aws_, google_, azurerm_).

示例:AWS生产Web堆栈

一个真实的部署,显示操作是如何组合的。此示例创建了一个包含子网、EC2、RDS、S3、IAM和安全组的VPC,总共13个资源。

terraform_session('new "Acme Corp Web Stack"')

# Provider + Variables
terraform([
  'add provider aws region:us-east-1',
  'add variable environment type:string default:"production" description:"Deployment environment"',
  'add variable project_name type:string default:"acme-web" description:"Project name"',
  'add variable vpc_cidr type:string default:"10.0.0.0/16" description:"VPC CIDR block"',
  'add variable instance_type type:string default:"t3.medium" description:"EC2 instance type"',
  'add variable db_instance_class type:string default:"db.t3.medium" description:"RDS instance class"',
])

# Networking
terraform([
  'add resource aws_vpc main cidr_block:var.vpc_cidr enable_dns_support:true enable_dns_hostnames:true',
  'add resource aws_subnet public_a vpc_id:aws_vpc.main.id cidr_block:"10.0.1.0/24" map_public_ip_on_launch:true',
  'add resource aws_subnet public_b vpc_id:aws_vpc.main.id cidr_block:"10.0.2.0/24" map_public_ip_on_launch:true',
  'add resource aws_internet_gateway igw vpc_id:aws_vpc.main.id',
  'add resource aws_route_table public vpc_id:aws_vpc.main.id',
])

# Nested blocks for route table and security groups
terraform([
  'nest public route cidr_block:"0.0.0.0/0" gateway_id:aws_internet_gateway.igw.id',
  'add resource aws_security_group web name:"${var.project_name}-web-sg" vpc_id:aws_vpc.main.id',
  'nest web ingress from_port:80 to_port:80 protocol:"tcp"',
  'nest web ingress from_port:443 to_port:443 protocol:"tcp"',
  'nest web egress from_port:0 to_port:0 protocol:"-1"',
])

# Compute + Database
terraform([
  'add resource aws_instance webserver ami:"ami-0c55b159" instance_type:var.instance_type subnet_id:aws_subnet.public_a.id',
  'nest webserver root_block_device volume_size:20 volume_type:"gp3"',
  'add resource aws_db_subnet_group dbsubnet name:"${var.project_name}-db-subnet"',
  'set dbsubnet subnet_ids:"[aws_subnet.public_a.id,aws_subnet.public_b.id]"',
  'add resource aws_db_instance rds engine:"postgresql" instance_class:var.db_instance_class allocated_storage:20',
  'add resource aws_s3_bucket assets bucket:"${var.project_name}-assets-${var.environment}"',
])

# Outputs
terraform([
  'add output vpc_id value:aws_vpc.main.id',
  'add output web_ip value:aws_instance.webserver.public_ip',
  'add output db_endpoint value:aws_db_instance.rds.endpoint',
])

# Tags (all resources at once)
terraform([
  'style main tags:"Name=${var.project_name}-vpc,Environment=${var.environment}"',
  'style public_a tags:"Name=${var.project_name}-public-a,Environment=${var.environment}"',
  'style public_b tags:"Name=${var.project_name}-public-b,Environment=${var.environment}"',
  'style igw tags:"Name=${var.project_name}-igw,Environment=${var.environment}"',
  'style webserver tags:"Name=${var.project_name}-web,Environment=${var.environment}"',
  'style rds tags:"Name=${var.project_name}-db,Environment=${var.environment}"',
  'style assets tags:"Name=${var.project_name}-assets,Environment=${var.environment}"',
])

# Day-2 modifications
terraform([
  'label webserver app_server',                         # Rename
  'set instance_type default:"t3.large"',               # Change default
  'remove assets',                                       # Replace S3 bucket
  'add resource aws_s3_bucket static_assets bucket:"${var.project_name}-static-${var.environment}"',
])

terraform_query('plan')  # Export final HCL

发展

go test ./...                        # Run all tests
go build ./cmd/fcp-terraform         # Build binary

许可证

麻省理工学院