@aaa-sentinel/iac-cost 0.9.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +117 -2
  2. package/dist/cli.js +4650 -796
  3. package/package.json +3 -1
package/README.md CHANGED
@@ -7,7 +7,9 @@ internet is caught in review instead of after the deploy. With `iac-cost fix`, t
7
7
  your last scan on resources this Terraform manages come back to you as **pull requests**, not as
8
8
  console changes (see [Fixes as pull requests](#fixes-as-pull-requests)).
9
9
 
10
- > **New in 0.9.0:** `iac-cost env-cost --env <name>` prints what an ephemeral environment has cost so far, resource by resource, with when its lease expires, and `--comment` posts it on the merge request (see [Environment cost](#environment-cost)).
10
+ > **New in 0.10.0:** `iac-cost owners sync` sends which team owns what, from Backstage `catalog-info.yaml` files and from CODEOWNERS plus the Terraform state, so AAA Sentinel can split the bill by team (see [Owners from code](#owners-from-code)).
11
+ >
12
+ > **In 0.9.0:** `iac-cost env-cost --env <name>` prints what an ephemeral environment has cost so far, resource by resource, with when its lease expires, and `--comment` posts it on the merge request (see [Environment cost](#environment-cost)).
11
13
  >
12
14
  > **In 0.8.0:** `--fail-on-budget` fails the pull request when the change takes an AAA Sentinel budget over (see [Budget gate](#budget-gate)).
13
15
  >
@@ -128,7 +130,8 @@ neither can change it. A check that **could not be decided** (see below) never b
128
130
  With `--fail-on-budget` the CLI sends the estimated monthly change (`totalMonthlyDeltaUsd`, one
129
131
  number) to AAA Sentinel, which compares it with your budgets: for each budget that applies, this
130
132
  month's spend so far scaled to the whole month, plus the change. The budgets that apply are the
131
- organization ones, and those of the account in `--budget-account` (or `--drift-account`).
133
+ organization ones, those of the account in `--budget-account` (or `--drift-account`) and, when
134
+ that account belongs to one of your MSP clients, the budgets of that client.
132
135
 
133
136
  **Limitation of this version: budgets scoped to one billing service are not checked.** A
134
137
  Terraform plan names resource types, not the billing services they are charged under, so the CLI
@@ -180,6 +183,118 @@ npx --yes @aaa-sentinel/iac-cost env-cost --env pr-412 --comment # GitLab
180
183
  - It always exits `0`, so a report can never fail the environment pipeline. Only a missing or
181
184
  invalid `--env` or `--format` exits `2`.
182
185
 
186
+ ## Owners from code
187
+
188
+ `iac-cost owners sync` tells AAA Sentinel which team owns what, from the files your repository
189
+ already has, and sends it to `POST /v1/owners/sync`. AAA Sentinel then splits the bill of the
190
+ closed month by team (chargeback by team) and estimates the current month per team. Business plan.
191
+
192
+ | Source | What the CLI reads | What it sends |
193
+ | --- | --- | --- |
194
+ | `backstage` | Every `catalog-info.yaml` / `.yml` (several documents per file, `---`); entities of kind `Component`, `Resource` or `System` with `metadata.name` and `spec.owner` | One rule per component: resources whose `service` or `app` tag (`--tag-keys`) equals the component name belong to its owner. `group:default/payments` and `payments` both become `payments`. Only groups are teams: an owner `user:…` is a person and the component is skipped with a warning |
195
+ | `codeowners_tf` | `CODEOWNERS` and the output of `terraform show -json` | Every managed resource of the state, child modules included, by its `arn`, plus the short `id` only when AWS never repeats it across accounts (`i-`, `vol-`, `sg-`, `snap-`, `eni-`, `subnet-`, `vpc-` and S3 bucket names); without an `arn` (Azure, Google Cloud), by its `id`. The owner is the CODEOWNERS owner of the root module directory: the last rule that matches `<tf-dir>/main.tf`, as git does (`infra/*` owns the files of `infra`, not its subdirectories). Only team owners count: `@acme/payments` becomes `payments`; a `@person` or an email is a person, so that directory has no owner and nothing is sent. With several teams, the first |
196
+
197
+ ```bash
198
+ # Backstage: catalog-info.yaml files anywhere in the repository
199
+ npx --yes @aaa-sentinel/iac-cost owners sync
200
+
201
+ # CODEOWNERS + Terraform state of one root module
202
+ terraform -chdir=infra/payments show -json > state.json
203
+ npx --yes @aaa-sentinel/iac-cost owners sync --tf-state state.json --tf-dir infra/payments --no-backstage
204
+
205
+ # See what would change, and the conflicts with other sources, without writing
206
+ npx --yes @aaa-sentinel/iac-cost owners sync --tf-state state.json --tf-dir infra/payments --dry-run
207
+ ```
208
+
209
+ ```text
210
+ codeowners_tf acme/payments · infra/payments @ 4f1c2a9e0b7d (src_3f9a1c0d2b4e6f8a1c3e5d7b)
211
+ 0 rules, 214 resources · synced
212
+ added 12 · removed 3 · changed 0
213
+ 1 conflict with other sources (the oldest source wins):
214
+ conflict resource arn:aws:s3:::acme-logs: platform (src_9b8a7c6d5e4f3a2b1c0d9e8f) wins over payments (src_3f9a1c0d2b4e6f8a1c3e5d7b)
215
+ ```
216
+
217
+ | Option | Effect |
218
+ | --- | --- |
219
+ | `--backstage <glob>` | Catalog files, relative to the repository root (`*`, `**`, `?`, `{a,b}`). Default: every `catalog-info.yaml` / `.yml` under `--path`, skipping `node_modules`, `.git` and `.terraform` |
220
+ | `--no-backstage` | Do not look for catalog files (a pipeline that only syncs a Terraform state) |
221
+ | `--tag-keys <k1,k2>` | Tag keys that carry the component name on your resources, up to 5. Default `service,app` |
222
+ | `--tf-state <file>` | `terraform show -json` output: the state, or a plan (its prior state is read) |
223
+ | `--tf-dir <dir>` | Root module directory, relative to the repository root. Default: the current directory |
224
+ | `--codeowners <file>` | Default: `.github/CODEOWNERS`, `CODEOWNERS`, `docs/CODEOWNERS` or `.gitlab/CODEOWNERS` |
225
+ | `--repo <owner/name>` | Default: `GITHUB_REPOSITORY`, `CI_PROJECT_PATH`, `BITBUCKET_REPO_FULL_NAME`, or the `origin` remote |
226
+ | `--path <dir>` | Source path, for monorepos with several sources of the same kind. Default: the root for Backstage, `--tf-dir` for Terraform, so each root module is its own source |
227
+ | `--ref` / `--commit` | Default: the branch and commit of the CI run (`GITHUB_REF_NAME`/`GITHUB_SHA`, `CI_COMMIT_REF_NAME`/`CI_COMMIT_SHA`, `BITBUCKET_BRANCH`/`BITBUCKET_COMMIT`), or git |
228
+ | `--dry-run` | Prints what would be added, removed or changed and the conflicts; writes nothing |
229
+ | `--json` | Prints the API answers as JSON |
230
+
231
+ - **Each repository and path is one source.** A sync replaces only that source; the rules set by
232
+ hand in the console and the other sources are never touched. Syncing the same content again
233
+ answers `unchanged` and writes nothing, so it can run on every push to the main branch.
234
+ - **Precedence:** the real `team` tag of a resource first, then the manual rules, then a full
235
+ resource id from any source (the oldest source first), then a short id, and last the tag rules
236
+ of the sources (the oldest first). The same resource or tag value claimed by two teams is a
237
+ conflict: the CLI prints it, and the Owners page and `GET /v1/owners` list them all.
238
+ - **What leaves your machine:** component names, tag keys, resource ids (ARN, id), Terraform
239
+ addresses and team names, with the repository, path, branch and commit. Never your code, the
240
+ state values or the plan, and never people: user owners, `@person` owners and emails are not
241
+ teams, and an id or address that contains `@` (an IAM user named by email, a `for_each` key) is
242
+ left out.
243
+ - **Nothing partial is sent.** If a catalog file is not valid YAML, the state cannot be read or
244
+ CODEOWNERS gives the root module no owner, that source is skipped with exit `1`: sending what is
245
+ left would delete the missing part. The same goes for a directory the CLI cannot read while
246
+ looking for catalog files. Limits per source: 2000 Backstage components, 10000
247
+ resources and a 5 MB request, checked before sending; 50 sources per organization.
248
+ - Two sources in one run (catalog files found and `--tf-state` given) are two calls, one per kind.
249
+ - The OAuth2 application in `IAC_COST_CLIENT_ID` needs the `owners:write` scope. Exit `0` when every
250
+ source synced or nothing changed, `1` when a source could not be read or sent, `2` for invalid
251
+ options or nothing to sync.
252
+
253
+ ### In the CI
254
+
255
+ GitHub Actions:
256
+
257
+ ```yaml
258
+ owners:
259
+ if: github.ref == 'refs/heads/main'
260
+ runs-on: ubuntu-latest
261
+ steps:
262
+ - uses: actions/checkout@v4
263
+ - uses: hashicorp/setup-terraform@v3
264
+ - run: terraform -chdir=infra/payments init -input=false && terraform -chdir=infra/payments show -json > state.json
265
+ - run: npx --yes @aaa-sentinel/iac-cost owners sync --tf-state state.json --tf-dir infra/payments
266
+ env:
267
+ IAC_COST_CLIENT_ID: ${{ secrets.AAA_SENTINEL_CLIENT_ID }}
268
+ IAC_COST_CLIENT_SECRET: ${{ secrets.AAA_SENTINEL_CLIENT_SECRET }}
269
+ ```
270
+
271
+ GitLab CI:
272
+
273
+ ```yaml
274
+ owners:
275
+ image: node:20
276
+ rules:
277
+ - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
278
+ script:
279
+ - npx --yes @aaa-sentinel/iac-cost owners sync
280
+ ```
281
+
282
+ Bitbucket Pipelines:
283
+
284
+ ```yaml
285
+ pipelines:
286
+ branches:
287
+ main:
288
+ - step:
289
+ name: Owners
290
+ image: node:20
291
+ script:
292
+ - npx --yes @aaa-sentinel/iac-cost owners sync
293
+ ```
294
+
295
+ On GitLab and Bitbucket, set `IAC_COST_CLIENT_ID` and `IAC_COST_CLIENT_SECRET` as masked or secured
296
+ repository variables.
297
+
183
298
  ## What actually leaves your machine
184
299
 
185
300
  Not the plan. `terraform show -json` output carries CIDRs, account identifiers and, often,