Infracost v2.18.2 常用 CLI 命令详解
逐条讲解 Infracost v2.18.2 全部命令:scan/diff/inspect 核心三件套与 auth/org/policies/agent/mcp/cache/doctor/update,含 v0.x 迁移表、未登录行为与排障实录,回显实测采集。
Infracost v2.18.2 常用 CLI 命令详解
目标:作为本专栏核心篇,逐条讲解 Infracost CLI v2.18.2 的全部命令——总览分类、认证、扫描(scan)、基线对比(diff)、结果查询(inspect)、云端查询(org/budgets/guardrails/policies)、集成(setup/agent/ide/ci/mcp)与维护(cache/doctor/update/plugin),并给出 v0.x → v2 命令迁移表与排障实录。所有帮助文本与命令回显均在 Ubuntu 24.04 服务器上用 v2.18.2 实测逐行采集(未登录行为同样如实收录);配置参数细节见《Infracost v2.18.2 配置参数详解》。
一、环境信息
| 项目 | 取值 |
|---|---|
| 操作系统 | Ubuntu 24.04 LTS(x86_64) |
| CLI 版本 | Infracost 2.18.2(官方最新稳定版,2026-10-22 发布) |
| CLI 位置 | /usr/local/bin/infracost |
| 演示工程 | /root/demo-terraform(AWS:1 台 t3.medium EC2 + 1 个 S3 桶) |
| 全局缓存目录 | ~/.cache/infracost(插件、results/parser 缓存) |
| 登录状态 | 未登录(凭据未初始化)——文中同时给出未登录实测回显与登录后的预期形态说明 |
二、前置条件
- CLI 已安装(《Infracost v2.18.2 安装指南》);
- 理解 v2 的凭据门禁规则(本机反复实测的结论):
scan/diff/inspect/org/budgets/guardrails/policies等业务命令在无凭据时直接输出登录 URL 并等待交互(未处理 stdin 时命令会一直挂着,直到被信号终止);auth/cache/plugin/doctor/update/agent status/completion可在未登录状态独立运行; - 演示工程
main.tf(与配置篇(《Infracost v2.18.2 配置参数详解》)一致):
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.medium"
tags = {
Name = "infracost-demo-web"
}
}
resource "aws_s3_bucket" "logs" {
bucket = "infracost-demo-logs-q1w2e3r4"
}
三、命令总览
infracost --help 实测完整回显:
Shift FinOps Left: Prevent cloud waste and budget overruns before every deploy
Usage:
infracost [command]
Examples:
# First-time setup (auth, agents, IDE, CI)
$ infracost setup
# Scan the current directory for costs and policy violations
$ infracost scan
# View a summary of the latest scan results
$ infracost inspect --summary
Setup & integrations:
agent Manage AI coding agent integrations
auth Manage authentication
ci Manage CI integrations
ide Manage IDE integrations
org Manage organizations
plugin Manage Infracost plugins
setup Set up Infracost integrations
Analyze infrastructure:
diff Show the cost change between a saved baseline and your current code
inspect Inspect cached analysis results with filtering and grouping
scan Scan your IaC and derive FinOps costs and policy violations
Organization settings:
budgets List cost budgets for the current organization
guardrails List cost guardrails for the current repository
policies List all available FinOps and tagging policies
CLI maintenance:
cache Inspect and manage the on-disk Infracost cache
doctor Run diagnostic checks on your Infracost installation
mcp Run as a Model Context Protocol stdio server
update Update to the latest version
Additional Commands:
completion Generate the autocompletion script for the specified shell
Flags:
--access-token-use-cache Read and save access tokens from a cache file (disable to force a fresh login) (default true)
--debug Enable debug logging
-h, --help help for infracost
--json Output logs and command results as JSON
--llm Output command results in a compact, token-efficient format intended for LLM prompts
--no-color Disable colored output
--oauth-use-device-flow Use device flow for authentication instead of PKCE (useful when you don't have access to localhost)
--org string The organization slug or ID to use
-v, --version version for infracost
Use "infracost [command] --help" for more information about a command.
命令面五大类(照抄分组):Setup & integrations(agent/auth/ci/ide/org/plugin/setup)、Analyze infrastructure(diff/inspect/scan,核心三件套)、Organization settings(budgets/guardrails/policies,依赖 Infracost Cloud)、CLI maintenance(cache/doctor/mcp/update)、Additional(completion)。
全局 flags 速览(完整说明见配置篇(《Infracost v2.18.2 配置参数详解》)四章):--json(结构化输出)、--llm(LLM 紧凑输出)、--no-color、--org(指定组织)、--debug、--oauth-use-device-flow、--access-token-use-cache(默认 true)。
v0.x → v2 命令迁移表(逐条实测)
网上旧教程大量基于 v0.x,v2.18.2 上的真实行为:
| v0.x 命令 | v2.18.2 实测行为 | 替代做法 |
|---|---|---|
infracost breakdown | 已废弃,自动转 scan:infracost breakdown is deprecated, running infracost scan instead | 直接用 scan |
infracost output | 已移除:infracost output is no longer supported. This command has been removed.(退出码 1) | scan --json / scan -o |
infracost configure | 已移除:infracost configure is no longer supported. This command has been removed.(退出码 1) | 配置走 infracost.yml 与 flags |
infracost diff --path-to-code ... | 语义变更:--compare-to <基线 JSON> | 见六章 |
export INFRACOST_API_KEY=... | 环境变量不再被读取(doctor 仍报 No credentials found) | auth login 落盘 |
infracost auth setup-ci | 已移除(infracost auth --help 实测仅 login/logout/whoami) | CI 令牌注入(配置篇(《Infracost v2.18.2 配置参数详解》)六章) |
四、认证命令族(auth)
4.1 auth login(登录)
infracost auth login --help 实测:flags 仅 -h, --help,登录方式由全局 flag --oauth-use-device-flow 控制。
形态一:默认 OAuth PKCE 回调(本机有浏览器)
infracost auth login
实测回显:
Please go to the following URL to log in:
https://login.infracost.io/authorize?audience=https%3A%2F%2Fdashboard.api.infracost.io&client_id=uMF6vrdl42thCHGI6Os6UqMqr2Px412O&code_challenge=Lj5AMvuwgdK2RnrUjOfSlevtlg06PzZrT28iGfYUTdg&code_challenge_method=S256&icSource=cli&redirect_uri=http%3A%2F%2Flocalhost%3A26372%2Fcallback&response_type=code&scope=offline_access+orgs%3Aread+policies%3Aread+prices%3Aread+runs%3Awrite&state=Mfu6z4CkVTDyFL5SZh_Eiw%3D%3D
Press Enter to open the browser automatically...
CLI 在 localhost:26372 起回调监听,浏览器完成授权后自动落盘凭据。
形态二:device flow(无浏览器服务器,推荐 SSH 场景)
infracost auth login --oauth-use-device-flow
实测回显:
Please go to the following URL to log in:
https://login.infracost.io/activate
And enter the code:
QRJF-SFZP
在任一电脑浏览器打开 https://login.infracost.io/activate、登录(免费注册,无需信用卡)并输入设备码,本机 CLI 轮询到授权完成即退出并落盘凭据。完成后再执行业务命令即可正常扫描(此步骤无法跳过,见三章门禁规则)。
4.2 auth whoami(查看当前用户)
infracost auth whoami
未登录实测回显(进入登录引导而非报错):
Please go to the following URL to log in:
https://login.infracost.io/authorize?audience=https%3A%2F%2Fdashboard.api.infracost.io&client_id=uMF6vrdl42thCHGI6Os6UqMqr2Px412O&code_challenge=I0Ni4aEh--8GaBEqVadEIu6tsGO8CibMJCja5tGxw2o&code_challenge_method=S256&icSource=cli&redirect_uri=http%3A%2F%2Flocalhost%3A26372%2Fcallback&response_type=code&scope=offline_access+orgs%3Aread+policies%3Aread+prices%3Aread+runs%3Awrite&state=veN1_upKSK2H89p0cqc3ZQ%3D%3D
Press Enter to open the browser automatically...
登录后输出当前用户与组织信息(命令描述原文:Show the current user)。
4.3 auth logout(登出)
infracost auth logout --no-color
实测回显(未登录状态同样成功,幂等):
Logged out
exit=0
五、scan(扫描:成本 + 策略)
v2 的核心命令,官方描述:Scan your IaC and derive FinOps costs and policy violations——一次扫描同时产出成本估算与 FinOps/标签策略违规。
infracost scan --help 实测完整回显:
Scan your IaC and derive FinOps costs and policy violations
Usage:
infracost scan [path] [flags]
Examples:
# Scan the current directory
$ infracost scan
# Scan a specific project path
$ infracost scan ./terraform
# Scan against a different organization's policies & prices
$ infracost scan --org acme
Flags:
--currency string ISO 4217 currency code to use for prices (e.g. USD, EUR, GBP)
-h, --help help for scan
--include-warnings Also show warning-severity diagnostics in the summary
-o, --option stringArray Specify a plugin-specific option e.g. -o infracost/terraform.option=yes
--ssh-key-file string Comma-separated SSH private key file(s) to use for fetching private modules over SSH (defaults to the standard ~/.ssh keys)
Global Flags:
--access-token-use-cache Read and save access tokens from a cache file (disable to force a fresh login) (default true)
--debug Enable debug logging
--json Output logs and command results as JSON
--llm Output command results in a compact, token-efficient format intended for LLM prompts
--no-color Disable colored output
--oauth-use-device-flow Use device flow for authentication instead of PKCE (useful when you don't have access to localhost)
--org string The organization slug or ID to use
要点拆解:
[path]位置参数:不传扫描当前目录,传路径扫指定工程(infracost scan ./terraform);--currency:改展示币种(ISO 4217:USD/EUR/GBP…),只影响展示换算,不影响定价数据;--include-warnings:默认 summary 只显示 error 级诊断,加此参数把 warning 级也带出来;-o/--option:插件专属选项通道(示例infracost/terraform.option=yes),不常用但排障时官方可能要求指定;--ssh-key-file:私有 Terraform module 经 SSH 拉取时的私钥(默认已尝试标准~/.ssh密钥);- 项目发现:自动探测目录树;复杂 monorepo 用仓库根
infracost.yml/infracost.yml.tmpl声明(配置篇(《Infracost v2.18.2 配置参数详解》)五章)。
未登录实测(演示工程,模拟真实服务器上的首跑):
cd /root/demo-terraform
infracost scan --no-color
Please go to the following URL to log in:
https://login.infracost.io/authorize?audience=https%3A%2F%2Fdashboard.api.infracost.io&client_id=uMF6vrdl42thCHGI6Os6UqMqr2Px412O&code_challenge=I0Ni4aEh--8GaBEqVadEIu6tsGO8CibMJCja5tGxw2o&code_challenge_method=S256&icSource=cli&redirect_uri=http%3A%2F%2Flocalhost%3A26372%2Fcallback&response_type=code&scope=offline_access+orgs%3Aread+policies%3Aread+prices%3Aread+runs%3Awrite&state=veN1_upKSK2H89p0cqc3ZQ%3D%3D
Press Enter to open the browser automatically...
⚠️ 这是 v2 必须如实说明的行为:凭据校验先于一切业务执行(连
--providers参数校验都排在登录之后,见十一章实录 3)。完成四章任一登录形态后,scan即进入「下载插件 → 解析 IaC → 查价 → 策略检查」流水线;扫描结果同时写入本地缓存,供inspect/diff消费。scan --json的结构化输出(官方文档指定的项目清单核对方式)与infracost.yml项目映射验证方法见配置篇(《Infracost v2.18.2 配置参数详解》)。
六、diff(基线成本对比)
官方描述:Show the cost change between a saved baseline and your current code。v2 的 diff 是「当前代码 vs 保存的基线 JSON」——不再像 v0.x 那样直接对比 git 分支。
infracost diff --help 实测完整回显:
Show the cost change between a saved baseline and your current code
Usage:
infracost diff [path] [flags]
Examples:
# Save a baseline, change the code, then diff against it
$ infracost scan --json > infracost-base.json
$ infracost diff --compare-to infracost-base.json
# Diff a specific project path
$ infracost diff ./terraform --compare-to infracost-base.json
Flags:
--compare-to string Path to an Infracost JSON file to compare against (defaults to the last cached scan of this path)
--currency string ISO 4217 currency code to use for prices (e.g. USD, EUR, GBP)
-h, --help help for diff
-o, --option stringArray Specify a plugin-specific option e.g. -o infracost/terraform.option=yes
--ssh-key-file string Comma-separated SSH private key file(s) to use for fetching private modules over SSH (defaults to the standard ~/.ssh keys)
Global Flags:
(与 scan 相同的 7 个全局 flags)
标准工作流(--help 示例原样):
cd /root/demo-terraform
# 1. 保存基线:把当前成本固化成 JSON
infracost scan --json > infracost-base.json
# 2. 修改代码(改实例规格/加资源/换 region)……
# 3. 对比:输出相对基线的成本增减
infracost diff --compare-to infracost-base.json
省略 --compare-to 时:默认与「该路径最近一次缓存扫描」对比(flags 说明原文:defaults to the last cached scan of this path)——即本地反复改代码时,直接 infracost diff 就能看相对上一次扫描的变化。CI/PR 场景由官方集成自动执行同类对比(配置篇(《Infracost v2.18.2 配置参数详解》)六章)。
💡 与 v0.x 的差异:旧版
infracost diff --path-to-code /path/to/branch直接吃两个代码路径;v2 改为「基线 JSON」机制,基线可来自本地scan --json、CI 构建产物或官方 GitHub App 的 merge-base 计算。
七、inspect(结果查询与过滤)
v2 的重头查询命令,官方描述:Inspect cached analysis results with filtering and grouping——消费最近一次 scan 写入的缓存(或 --file 指定的 JSON),提供资源排序、策略违规、标签缺失、成本阈值、分组统计等丰富查询面。不重新查价、不需要重新解析,适合在 PR 评论脚本、LLM 管道里反复查询。
infracost inspect --help 实测的官方示例(原文照抄,覆盖主要用法):
Examples:
# Headline numbers (totals + per-policy counts):
$ infracost inspect --summary
# Single scalar from the summary (e.g. "how many failing FinOps policies?"):
$ infracost inspect --summary --fields failing_policies
# Top 10 most expensive resources:
$ infracost inspect --top 10
# Total potential yearly savings if every FinOps issue were fixed:
$ infracost inspect --total-savings
# Top 5 FinOps issues, just the addresses (one per line):
$ infracost inspect --top-savings 5 --fields address
# Top 5 FinOps issues with a custom column projection (aligned table):
$ infracost inspect --top-savings 5 --fields address,yearly_savings,policy
# Every resource missing the 'team' tag, one address per line:
$ infracost inspect --missing-tag team
# Same, with extra columns for context (add --llm/--json for scripting):
$ infracost inspect --missing-tag team --fields address,type,monthly_cost
# Resources whose 'environment' tag is set but uses a disallowed value:
$ infracost inspect --invalid-tag environment
# Resources with monthly cost > $100:
$ infracost inspect --min-cost 100
# Composable filter: missing 'team' tag AND on AWS:
$ infracost inspect --filter "tag.team=missing,provider=aws"
# All addresses failing a specific policy (full list, no truncation):
$ infracost inspect --policy "Required Tags" --addresses-only
# Per-project diagnostics from the latest scan (errors + warnings):
$ infracost inspect --diagnostics
# Machine-readable, token-efficient output for LLM pipelines:
$ infracost inspect --summary --llm
flags 全表(实测 --help,照抄默认值):
| 参数 | 说明 |
|---|---|
--addresses-only | 只输出资源地址(每行一个),等价 --fields=address,可与其他筛选叠加 |
--budget string | 按名称/ID 查看预算明细 |
--costs-only | 隐藏免费资源 |
--diagnostics | 最近一次扫描的按项目诊断(critical + warning) |
--failing | 只显示违规(failing)的策略 |
--fields strings | 列投影(逗号分隔);可用列随视图变化(如 address,yearly_savings,policy) |
--file string | 从 JSON 文件读结果(跳过缓存) |
--filter string | 过滤表达式(逗号分隔取 AND):支持 policy/project/provider/tag.<key>=missing |
--group-by strings | 分组维度:type/provider/project/resource/file/policy/guardrail/budget |
--guardrail string | 按名称/ID 查看 guardrail 明细 |
--invalid-tag string | 标签已设置但值不在策略允许列表内的资源 |
--max-cost float | 月成本 ≤ N 的资源 |
--min-cost float | 月成本 ≥ N 的资源 |
--missing-tag string | 完全缺失指定标签键的资源 |
--policy string | 按策略名/slug 过滤 |
--project string | 按项目名过滤 |
--provider string | 按 provider 过滤(aws、google、azurerm) |
--resource string | 按资源地址过滤 |
--summary | 汇总视图(总量 + 各策略计数) |
--top int | 按成本取前 N 个资源 |
--top-savings int | 按 yearly_savings 排序取前 N 个 FinOps 问题 |
--total-savings | 所有 FinOps 问题的年化节省合计(单标量) |
高频组合模式:
# 成本盘点:最贵资源 + 汇总
infracost inspect --summary
infracost inspect --top 10
# FinOps 行动清单:按「修复后年省多少」排序取前 5,只要地址(可喂给脚本/AI 代理)
infracost inspect --top-savings 5 --fields address
# 标签治理:缺 team 标签的资源地址清单(门禁脚本直接消费)
infracost inspect --missing-tag team --addresses-only
# 组合过滤:AWS 上缺 team 标签、月成本超 100 的资源
infracost inspect --filter "tag.team=missing,provider=aws" --min-cost 100
# 给 LLM 的紧凑输出
infracost inspect --summary --llm
八、云端查询命令族(org / budgets / guardrails / policies)
这一组命令消费 Infracost Cloud 的组织数据,未登录时同样先进入登录引导:
8.1 org(组织管理)
infracost org --help 实测子命令:
| 子命令 | 说明 |
|---|---|
org create | 创建新组织 |
org current | 显示当前组织 |
org list | 列出我的组织 |
org switch | 切换活动组织 |
8.2 budgets(预算)
infracost budgets [flags]——列出当前组织的成本预算(List cost budgets for the current organization),无专属 flags。
8.3 guardrails(护栏)
infracost guardrails [path] [flags]——列出当前仓库的成本护栏(List cost guardrails for the current repository),带 [path] 位置参数,无专属 flags。
8.4 policies(策略清单)
infracost policies --help 实测完整 flags(本组里唯一有专属参数的命令):
| 参数 | 缩写 | 说明 |
|---|---|---|
--finops-only | -f | 只列 FinOps 策略 |
--tagging-only | -t | 只列标签策略 |
--providers strings | -p | 限定 FinOps 策略查询的 provider(aws、azure、google);可减少下载的 provider 插件(flags 说明原文) |
--ssh-key-file string | — | 同 scan |
九、集成命令族(setup / agent / ide / ci / mcp / completion)
9.1 setup(交互式向导)
Walk through setting up Infracost for your coding agents, IDE, and CI pipeline
Usage:
infracost setup [flags]
Examples:
# Run the interactive setup walkthrough
$ infracost setup
新机器一条命令打通 auth → agent → IDE → CI。
9.2 agent(AI 编码代理集成)
子命令(实测 --help):setup(为代理安装 Infracost skills)/ remove(移除)/ status(查看哪些代理已安装、是否最新)。
infracost agent status --no-color 实测回显(无需登录,退出码 0):
No AI agents with Infracost skills detected. Run infracost agent setup to install them.
9.3 ide / ci
infracost ide setup:为 IDE 安装 Infracost 扩展(Install the Infracost extension for your IDE);infracost ci setup:为当前仓库生成 CI 集成配置(Set up Infracost CI integration for this repository)。
9.4 mcp(MCP 服务模式)
infracost mcp --help 实测说明(原文):
Run as a Model Context Protocol (MCP) stdio server.
Exposes the same operations as the top-level CLI commands as MCP tools,
backed by the same Go functions. Intended to be launched by an MCP-aware
client (an IDE assistant, for example) which communicates over
stdin/stdout.
把 CLI 全部能力以 MCP stdio 服务暴露——MCP 客户端(IDE 助手等)经 stdin/stdout 调用,复用同一套 Go 函数。接入方式:在客户端 MCP 配置里把 infracost mcp 注册为 stdio server。
9.5 completion(Shell 自动补全)
支持 bash/zsh/fish/powershell。实测 bash 补全生成:
infracost completion bash | head -3
# bash completion V2 for infracost -*- shell-script -*-
__infracost_debug()
落地:
infracost completion bash > /etc/bash_completion.d/infracost
source /etc/bash_completion.d/infracost
十、维护命令族(cache / doctor / update / plugin)
10.1 cache(缓存治理)
子命令(实测):info(各缓存磁盘占用)/ clear(清空 results 与 parser 缓存)/ prune(移除陈旧条目与游离文件)。
Inspect and manage the on-disk Infracost cache
Usage:
infracost cache [command]
Available Commands:
clear Wipe the results and parser caches
info Show on-disk size of each Infracost cache
prune Remove old cache entries and any stray files
infracost cache info
Cache root: /root/.cache/infracost
results cache 0 B
parser cache 0 B
parser results cache 0 B
10.2 doctor(诊断,排障首选)
Run diagnostic checks on your Infracost installation
完整实测回显(未登录态,7 项检查):
Infracost Doctor 2.18.2 - running 7 checks
Authentication
✗ No credentials found
→ Run `infracost auth login` to authenticate
⊘ Token valid
→ skipped (No credentials found)
⊘ Organization accessible
→ skipped (No credentials found)
⊘ API reachable
→ skipped (Organization accessible)
CLI
✔ Version 2.18.2 (latest)
Configuration
✔ Config file valid
! Default org set
→ No organization data available
✔ 2 passed ! 1 warning ✗ 1 issue ⊘ 3 skipped
Run infracost doctor --fix to attempt auto-remediation
Info: 1 health check(s) failed
检查维度:凭据存在性 → 令牌有效性 → 组织可达 → API 连通(前项失败后项自动 ⊘ skipped);版本新旧;配置文件合法性;默认组织。每项失败都带 → 行动提示,--fix 自动修复。
10.3 update(自更新)
infracost update --no-color # 成功时无输出,退出码 0
infracost --version
infracost version 2.18.2
已是最新版时(实测):
✔ Already up to date (2.18.2).
旧版本运行任意命令时会主动提示(2.17.0 下实测):
Update: A new version of Infracost is available: 2.17.0 → v2.18.2
$ infracost update
10.4 plugin(插件管理)
Manage Infracost plugins
Usage:
infracost plugin [command]
Available Commands:
list List Infracost parser and provider plugins
update Update all Infracost plugins to the latest version
infracost plugin list 实测(未安装态):
Plugins
Path: /root/.cache/infracost/plugins
Parsers:
infracost/terraform not installed
infracost/terragrunt not installed
infracost/cloudformation not installed
infracost/ciscostacks not installed
infracost/arm not installed
infracost/terraform-plan not installed
infracost/kubernetes not installed
Providers:
infracost/aws not installed
infracost/google not installed
infracost/azure not installed
infracost/kubernetes not installed
infracost plugin update 后复查,全部转为 installed(实测,无需登录)。首次 scan 也会按需自动下载对应插件。
十一、排障实录
实录 1:业务命令挂起不退出
现象:infracost scan(未登录)执行后不返回,终端卡住。
定位:v2 未登录时输出登录 URL 后等待 stdin(Press Enter to open the browser automatically...),脚本/CI 环境里 stdin 永不关闭即挂起。
处理:交互场景按回车继续或改用 --oauth-use-device-flow;脚本场景登录引导不是错误——先把 auth login 完成;已挂起进程用 pkill -x infracost 清理(本机验证有效)。
实录 2:infracost output / infracost configure 报 removed
infracost output is no longer supported. This command has been removed.
infracost configure is no longer supported. This command has been removed.
定位:v0.x 命令在 v2 已移除(退出码 1)。处理:按三章迁移表替换——output 用 scan --json,configure 改 infracost.yml。
实录 3:参数写错了为什么不报错,还是登录引导?
实测 infracost policies --providers aws,foo --no-color(foo 是非法 provider 值)在未登录时输出的是登录 URL 而非参数错误,且命令挂起直到被终止(本机实测 exit 124)。定位:凭据校验先于参数/业务校验。处理:先登录;登录后同一命令才会进入参数校验。
实录 4:inspect 查不到刚扫描的结果
定位:inspect 只读缓存;若上次 scan 因未登录/网络中断未写缓存,inspect 无数据可查。处理:先 infracost scan 成功一次;或用 inspect --file <JSON> 直接消费 scan --json 导出的文件;infracost cache info 可确认缓存状态。
十二、常见问题(FAQ)
Q1:scan 和 diff 有什么区别?
scan 产出全量成本与策略结果(并写缓存);diff 对比「基线 JSON 或上次缓存」与当前代码的成本变化。PR 场景:主干存基线 → PR 分支 diff。
Q2:inspect --top-savings 的 yearly_savings 是怎么算的?
CLI 对每个 FinOps 策略违规估算「修复后年化节省」(如改用更便宜实例族/存储类的差价 ×12 个月);--total-savings 是全部门禁项的合计。金额均为估算口径(A1 篇第六章的限制说明)。
Q3:--llm 输出能用脚本解析吗?
--llm 面向 LLM prompt 的紧凑格式,结构可能随版本调整;脚本解析一律用 --json(两者同为全局 flags,可叠加 inspect 各查询参数)。
Q4:怎么只扫 AWS、避免下载 Azure/GCP 插件?
infracost policies -p aws(flags 原文:reduces which provider plugins are downloaded);scan 侧用 -o infracost/terraform.option=... 等插件选项控制(一般无需干预——插件只在项目用到时才生效)。
Q5:多组织账号怎么切换?
全局 --org <slug> 单次生效,或 infracost org switch 切活动组织;org list/org current 查看现状。
Q6:CI 里怎么避免挂起?
CI 不跑交互登录:使用官方 CI 集成(GitHub App 免维护,或 ci setup 生成的自管配置 + 令牌注入),业务命令在凭据就绪后直接执行,不会进入登录等待(三章门禁规则只影响无凭据环境)。
十三、参考链接
- 官方 Getting Started:https://www.infracost.io/docs/
- Config file 参考:https://www.infracost.io/docs/config_file
- CLI 仓库(源码与 issues 入口):https://github.com/infracost/cli
- 主仓库:https://github.com/infracost/infracost
- Infracost Cloud 控制台:https://dashboard.infracost.io/
- 官方社区 Slack:https://www.infracost.io/community-chat