Infracost
$ infracost 官方出品 低风险infracost 在本地解析 Terraform/OpenTofu HCL 或 Plan JSON,并查询 Infracost 云定价 API 生成月度成本明细与变更差额。它同时提供 --format json 与精简的 --llm 输出,非常适合代码 Agent 在改代码前评估 FinOps 成本。
brew install infracost 本地 HCL 解析器搭配原生 --format json 与 --llm 参数,比让大模型估算 AWS/GCP/Azure 价格更快更准。
Agent 友好度体检单
当前为静态评估(本机尚未安装或尚未运行探针)。可在本地运行 npm run data:probe 并提交 data/probes/ 升级为实测证据。
FCRS-1.0 六维评分拆解
支持 --format json、--json 日志、--llm 紧凑格式与 --no-color,但查询定价需提前配置 INFRACOST_API_KEY。
- Supports
--format json,--json, and--llmoutput flags - Non-interactive execution when
INFRACOST_API_KEYis set
仅对本地 Terraform/OpenTofu 代码做只读分析,不需云账号凭证,仅发送脱敏后的资源计价参数。
- Zero cloud provider credentials required; read-only local parsing
静态 Go 单二进制,支持 macOS、Linux 与 Windows 的 Homebrew、Docker 与发布包。
- Static Go binary across all major platforms
用确定性的逐资源定价差异取代低效的人工云价目表查询。
- Supports AWS, Azure, and GCP resource pricing from HCL or plan JSON
官方 infracost/infracost 仓库,Apache-2.0 协议,更新活跃。
- Official vendor CLI under Apache-2.0
infracost.io/docs/ 文档详尽,完整涵盖命令参数与 infracost-usage.yml。
- Detailed CLI reference at infracost.io/docs/
安全边界、鉴权与放权指南
将定价 API 密钥保存在 ~/.config/infracost/credentials.yml,或通过环境变量 INFRACOST_API_KEY 注入。
CLI 为 Apache-2.0 开源免费;云定价 API 对个人与小团队提供免费额度,大型团队 CI 护栏需商业订阅。
# Infracost Agent Rules - Ensure `INFRACOST_API_KEY` is configured before running `breakdown` or `diff`. - Pass `--format json` or `--no-color` when parsing outputs programmatically. - Treat `infracost breakdown` as read-only cost estimation; never expose API keys in logs.
Agent 高频安全命令配方
只读查询(安全推荐)
infracost breakdown --path . --format json
从本地 HCL 文件估算月度云开销并返回按资源拆分的 JSON 数据。
infracost diff --path . --compare-to infracost-base.json --format json
对比当前代码与历史基线文件,计算精确的月度成本差额。
核心优势
- + 原生
--format json与--llm参数专为自动化与 Agent 优化。 - + 直接解析 HCL,无需先执行
terraform init或提供云凭证。 - +
infracost diff可在合并前精确量化月度成本变化。
注意事项与踩坑点
- ! 查询实时云价格必须配置
INFRACOST_API_KEY(通过infracost auth login或环境变量)。 - ! 按量计费资源(如流量、Lambda 调用)依赖
infracost-usage.yml估算。