SuperAPI AI Directory

codeguard-terraform

Deployment & operations

View repository

Updated 10-15 21:48

About this skill

Terraform / OpenTofu Codeguard Gate

Goal: turn "looks fine" into repeatable tool evidence, and honestly distinguish passed, failed, unverifiable, and planned.

Start in 30 seconds

  1. At the repository root, confirm 无专用标记文件 or the target file exists.
  2. Detect tools:tflint --version。
  3. Read-only check first:tflint。
  4. Classify results as: code violations, missing configuration, missing tools, tool internal errors.
  5. Only perform safe fixes:terraform fmt。
  6. Re-run lint, and run the project's existing test/build gates.

You can directly ask:

  • "Check this Terraform / OpenTofu project, don't modify anything yet, and give me a classification of lint failures."
  • "Only fix Terraform / OpenTofu formatting issues; list semantic issues first."
  • "Verify Terraform / OpenTofu lint, test, and build evidence before committing."

Capability boundaries

✅ Good at handling

  1. Identify the Terraform / OpenTofu workspace based on .tf, .tfvars, .tofu and project markers.
  2. Run tflint and preserve exit codes, scope, and tool versions.
  3. Distinguish formatting, static rules, configuration, dependency, and environment failures.
  4. Use terraform fmt to handle reversible issues and re-scan.
  5. Report local results in layers with CI/build/test evidence.

⚠️ Conditions required

  1. The target tool must be installed and its version compatible with project constraints.
  2. Must be executed from the correct repository or module root.
  3. If the project has custom configuration, the committed configuration prevails.
  4. Whether generated code, vendor, and cache directories are scanned must follow repository rules.

❌ Not applicable

  1. Do not treat formatter passing as compilation, test, or security audit passing.
  2. Do not silently install tools, upgrade dependencies, or modify team rules.
  3. Do not use suppress/ignore/raising thresholds to conceal real issues.
  4. Do not claim "check passed" when tools or configuration are missing.

When to use

  • The user requests checking or fixing Terraform / OpenTofu code standards.
  • AI modified .tf, .tfvars, .tofu, needs pre-commit verification.
  • CI shows failures related to tflint .
  • Need to determine whether an issue can be fixed automatically or must be handled manually.

When not to use

  • When the main task is learning language syntax, framework design, or business modeling, use the corresponding language professional skill instead.
  • When the main task is dependency vulnerabilities, security auditing, or license governance, use a dedicated security skill.
  • When the user only asks to explain one error and there is no project context, provide a local explanation first and do not claim the repository is all green.

Data and security

This skill does not collect, upload, send, or store user code or credentials. By default it only reads the local repository configuration and runs local tools; it does not install dependencies over the network, and it does not record tokens, passwords, or private source code. Before any automatic fix, check the diff first; destructive or large-scale changes must be explicitly confirmed by the user.

Execution Contract

Item Current Fact
Status stable(Since Codeguard V0.3)
File Extensions .tf, .tfvars, .tofu
Project Markers No dedicated marker file
Integration Configuration Based on the repository's existing configuration
Tool Preparation brew install tflint
Probe Command 使用命令查找与项目配置检查
lint tflint
format/fix terraform fmt
Project-level gate tflint

Standard Workflow

Step 1: Determine the Real Scope

Confirm the repository root, module roots, generated directories, vendor directories, and user-specified files. For multi-module projects, list the modules to be scanned first; do not guess based on the current directory.

Step 2: Read the Project Contract

Read the actual commands in the repository's existing configuration and CI. Repository configuration takes precedence over this skill's examples; if the two conflict, report the difference first.

Step 3: Probe Tools and Versions

Run 对应工具 --version. Missing tools, incompatible versions, or configuration that cannot be loaded are all recorded as "unable to verify", not as passing.

Step 4: Run Read-Only Checks

Run tflint; record the command, working directory, exit code, tool version, and the first actionable error.

Step 5: Failure Classification

Type Typical Signal Handling
Format Indentation, whitespace, import order Formatter allowed, then check the diff
Static Rules Unused, complexity, naming, dangerous APIs Minimal semantic fix; no bulk guessing
Configuration parser/config/schema not found Fix the integration or report the prerequisites
Environment command not found, version conflict Report installation/version requirements; do not install silently
Tool Internal Error crash, timeout, parser exception Preserve the original evidence and narrow the reproduction scope

Step 6: Controlled Fixes

Prefer running terraform fmt. Handle only issues that are certain and reversible; dependency upgrades, rule exemptions, generated files, and business semantic changes must be explained separately.

Step 7: Verification Loop

Re-run the same lint command, then run the project's existing tests and build. Write "pass" only when the command, scope, and exit code are all clear; otherwise write "not run" or "unable to verify".

Rules

  1. Re-verify with the same command: after a fix, the original command that triggered the failure must be re-run.
  2. Layered Evidence: report lint, format, compile, test, and security separately.
  3. Minimal Changes: do not refactor along the way, and do not expand a style fix into a business rewrite.
  4. Configuration First: do not override the project's existing ignore, dialect, target, or version constraints.
  5. No Fabrication: Tools that were not run, configurations that were not seen, and platforms that were not verified must be explicitly marked.

Output template

Terraform / OpenTofu Codeguard 结果
- 范围:<仓库/模块/文件>
- 工具:<名称与版本>
- 命令:<实际命令>
- 状态:PASS / FAIL / UNVERIFIED / PLANNED
- 发现:<按格式/规则/配置/环境分类>
- 修改:<文件与原因;无修改写 none>
- 复验:<命令、退出码>
- 未验证:<测试/构建/平台差异>

Gotchas

  1. A missing tool is not a pass — must output UNVERIFIED and a clear preparation method.
  2. A formatter is not a linter — all-green formatting cannot override static rules or compilation errors.
  3. The root directory determines the result — running in the wrong module may miss checks or load the wrong configuration.
  4. Placeholders are not literal arguments — {file} must be replaced with real, authorized paths.
  5. Automatic fixes may expand the diff — after fixing, review the diff first, then run the regression gate.
  6. Generated and vendor directories require an explicit policy — third-party content must not be arbitrarily scanned or modified across the whole repository.

Domain pitfalls and fixes

  1. Domain pitfalls — terraform fmt、terraform validate and TFLint are three types of gates and cannot substitute for each other.

When information is insufficient

Do not just say "please provide more information." First give a safe read-only checking plan, and list the specific items that still need confirmation:

  1. repository/module root directory;
  2. target tool and version;
  3. project configuration file;
  4. the authoritative command in CI;
  5. whether automatic fixes are allowed.

FAQ

Q1: The tool is not installed; can it be determined that the code has no problems? No. The status must be UNVERIFIED。

Q2: Can ignore or suppression be added automatically? No. Exemption is only allowed when the user explicitly accepts it and the reason is recorded.

Q3: Why does it still fail after formatting? The formatter only covers formatting; static rules, configuration, compilation, and tests still need to be handled separately.

Q4: Should generated code be scanned? By default, follow the repository configuration; when there are no rules, first exclude generated/vendor, then explain to the user.

Q5: How can too many changes in one go be avoided? First fix in batches by failure category and file, and rerun the original command and review the diff for each batch.

Q6: If it passes locally, can it be said that CI will pass? No. The CI version, environment variables, operating system, and command scope still need to be aligned.

Load resources on demand

  • When encountering exit codes, failure classification, or exemption decisions, read references/rules/gate-rules.md。
  • When the language registry, toolchain, and project configuration need to be checked, read references/tooling/toolchain.md。
  • When a copyable task example is needed, read by scenario examples/, and it is not necessary to load all of them at once.