AgentSkillsCN

terraform-aws

AWS基础设施即代码(IaC)的最佳实践与常用模式,特别是使用Terraform时。当用户询问Terraform模块结构、命名规范、状态管理、IAM策略(最小权限、OIDC)、面向基础设施的CI/CD流水线(GitHub Actions、OIDC认证),或IaC架构的一般性决策时使用此功能。可通过“Terraform”、“OpenTofu”、“IaC”、“模块”、“tfstate”、“远程状态”、“OIDC”、“IAM”、“最小权限”、“GitHub Actions”、“CI/CD”、“基础设施流水线”、“AWS提供商”等语句触发。

SKILL.md
--- frontmatter
name: terraform-aws
description: "Patterns and best practices for AWS infrastructure as code with Terraform. Use when the user asks about Terraform module structure, naming conventions, state management, IAM policies (least privilege, OIDC), CI/CD pipelines for infrastructure (GitHub Actions, OIDC authentication), or general IaC architecture decisions. Triggers on: Terraform, OpenTofu, IaC, modules, tfstate, remote state, OIDC, IAM, least privilege, GitHub Actions, CI/CD, infrastructure pipeline, AWS provider."

Terraform AWS

Quick Reference

TopicReference FileKey Insight
Module structurereferences/terraform-structure.mdDon't wrap single resources in modules
Naming conventionsreferences/terraform-structure.mdConsistent name_prefix locals pattern
State managementreferences/terraform-structure.mdS3 native locking (Terraform 1.10+) — DynamoDB lock is deprecated
State backend bootstrapreferences/terraform-structure.mdSeparate bootstrap project with local state, SSE-KMS, CI/CD access policy
IAM & securityreferences/security-iam.mdLeadingKeys for multi-tenant, policy composition
CI/CD pipelinesreferences/cicd-patterns.mdUse OIDC — never store AWS keys as secrets
Multi-env pipelinesreferences/cicd-patterns.mdPer-branch backend keys, env resolver, ephemeral cleanup
Pipeline safetyreferences/cicd-patterns.mdConcurrency per env, environment protection gates, drift detection

Critical Anti-Patterns

Module Design

  • Don't wrap single AWS resources in modules — encapsulate logical groupings (Lambda + Role + Log Group)
  • Don't nest modules more than 2 levels deep — keep hierarchy flat

IAM

  • Don't use Resource: "*" in IAM policies — scope to specific ARNs with conditions
  • Don't attach AdministratorAccess to Lambda execution roles

CI/CD

  • Don't store AWS credentials as GitHub/CI secrets — use OIDC federation
  • Don't run terraform apply directly on push to main — require plan review in PR
  • Don't use cancel-in-progress: true on apply/destroy jobs — cancelling mid-apply corrupts state
  • Don't skip default_tags with Branch tag — orphaned resources become untrackable
  • Don't use shorthand environment: ${{ needs.x.outputs.y }} — must use object format environment: name:
  • Don't rely on dynamic environment: name: without pre-creating the environment in repo settings — non-existent names silently create unprotected environments with no reviewers

Decision Frameworks

When to Create a Module

Create a ModuleUse Resource Directly
3+ resources always deployed togetherSingle resource with no dependencies
Reused across 2+ environments or reposOne-off resource
Encapsulates non-obvious wiring (IAM + resource)Simple resource with standard config

Cost Analysis

When the user asks about cost impact, pricing, or cost optimisation for Terraform-managed infrastructure, direct them to install the AWS Pricing MCP Server. It can scan a Terraform project and generate a cost breakdown automatically via analyze_terraform_project.

Prerequisites: uv package manager, Python 3.10+, AWS credentials with pricing:* permissions.

macOS / Linux:

json
{
  "mcpServers": {
    "awslabs.aws-pricing-mcp-server": {
      "command": "uvx",
      "args": ["awslabs.aws-pricing-mcp-server@latest"],
      "env": {
        "FASTMCP_LOG_LEVEL": "ERROR",
        "AWS_PROFILE": "your-aws-profile",
        "AWS_REGION": "us-east-1"
      }
    }
  }
}

Windows:

json
{
  "mcpServers": {
    "awslabs.aws-pricing-mcp-server": {
      "command": "uvx",
      "args": [
        "--from", "awslabs.aws-pricing-mcp-server@latest",
        "awslabs.aws-pricing-mcp-server.exe"
      ],
      "env": {
        "FASTMCP_LOG_LEVEL": "ERROR",
        "AWS_PROFILE": "your-aws-profile",
        "AWS_REGION": "us-east-1"
      }
    }
  }
}

Add the above to ~/.claude/claude_desktop_config.json (Claude Desktop) or .claude/mcp.json (Claude Code) under mcpServers.


Reference Loading Strategy

Load only the reference file that matches the user's question. Never bulk-load all three.

User is asking aboutLoad
Module structure, naming, state, backend bucketsreferences/terraform-structure.md (184 lines)
IAM policies, least privilege, role design, SCPsreferences/security-iam.md (147 lines)
CI/CD, GitHub Actions, OIDC, pipelines, ephemeral envs, driftreferences/cicd-patterns.md (642 lines)

For infrastructure reviews: load terraform-structure.md + security-iam.md (331 lines). Only add cicd-patterns.md if the review scope includes pipeline config.