Terraform engineer

From seaworld008/Commonly-used-high-value-skills · View interactive version

Terraform engineer is an AI agent skill from the GitHub repository seaworld008/Commonly-used-high-value-skills. Use when designing Terraform IaC, reviewing modules, managing state, planning provider upgrades, or improving repeatable infrastructure deployment workflows.

How to use this skill

Skill file

Terraform Engineer

触发条件

核心能力

1. HCL 语法与最佳实践 (HCL Best Practices)

2. 模块设计模式 (Module Design Patterns)

3. 状态管理策略 (State Management)

4. 工作流 (Plan/Apply Workflow)

5. Drift 检测与修复 (Drift Detection & Remediation)

6. Provider 配置与管理

常用命令/模板

基础运维组合

# 初始化并拉取所有模块/插件
terraform init

# 生成变更计划并保存到文件(CI/CD 常用)
terraform plan -out=tfplan

# 应用之前保存的计划,跳过确认
terraform apply "tfplan"

# 请求替换资源,可能销毁并重建;先审查计划和依赖影响
terraform plan -replace="aws_instance.web" -out=replace.tfplan
# 仅在本次替换已获授权后应用
terraform apply "replace.tfplan"

# 图形化展示资源依赖关系
terraform graph | dot -Tsvg > graph.svg

Module 定义模板 (VPC 示例)

# modules/vpc/main.tf
resource "aws_vpc" "this" {
  cidr_block = var.vpc_cidr
  tags = {
    Name = var.project_name
  }
}

resource "aws_subnet" "public" {
  for_each = var.public_subnets
  vpc_id     = aws_vpc.this.id
  cidr_block = each.value
  map_public_ip_on_launch = true
}

# modules/vpc/variables.tf
variable "vpc_cidr" {
  description = "VPC CIDR block"
  type        = string
}

variable "public_subnets" {
  type        = map(string)
  default     = {
    "us-east-1a" = "10.0.1.0/24"
    "us-east-1b" = "10.0.2.0/24"
  }
}

Backend 远程状态配置模板

terraform {
  required_version = ">= 1.14"

  backend "s3" {
    bucket       = "my-tf-state-bucket"
    key          = "prod/terraform.tfstate"
    region       = "us-east-1"
    encrypt      = true
    use_lockfile = true
  }

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 5.0"
    }
    google = {
      source  = "hashicorp/google"
      version = "~> 8.0"
    }
  }
}

# 原生单元测试示例 (tests/vpc_unit_test.tftest.hcl)
run "rejects_invalid_cidr" {
  command = plan
  variables {
    vpc_cidr = "invalid"
  }
  expect_failures = [var.vpc_cidr]
}

边界与限制

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.