@aaa-sentinel/iac-cost 0.8.0 → 0.10.0

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 +145 -1
  2. package/dist/cli.js +4700 -737
  3. package/package.json +3 -1
package/README.md CHANGED
@@ -7,7 +7,11 @@ 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.8.0:** `--fail-on-budget` fails the pull request when the change takes an AAA Sentinel budget over (see [Budget gate](#budget-gate)).
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)).
13
+ >
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)).
11
15
  >
12
16
  > **In 0.7.0:** the plan check flags RDS and Aurora instances with `auto_minor_version_upgrade = false`
13
17
  > (CIS AWS 2.3.2; RDS Custom is skipped), and `iac-cost fix` turns it back on. Security findings have a
@@ -150,6 +154,146 @@ change and the projected total, in the budget's currency (`≈` when converted f
150
154
  npx --yes @aaa-sentinel/iac-cost plan.json --fail-on-budget --budget-account acc_9f2c1e
151
155
  ```
152
156
 
157
+ ## Environment cost
158
+
159
+ `iac-cost env-cost --env <name>` reads `GET /v1/environments/{env}/cost` and prints what an
160
+ ephemeral environment (the preview of a pull request, a load test) has cost so far. An environment
161
+ is every resource tagged `aaa-sentinel:env=<name>` (`aaa-sentinel_env` on GCP) or covered by an
162
+ AAA Sentinel lease with that `env`. For each resource it shows the type, account and region, its
163
+ monthly list cost, what it has cost over the days it has been alive, and when its lease expires.
164
+
165
+ ```bash
166
+ npx --yes @aaa-sentinel/iac-cost env-cost --env pr-412 # text table
167
+ npx --yes @aaa-sentinel/iac-cost env-cost --env pr-412 --format markdown > env.md
168
+ npx --yes @aaa-sentinel/iac-cost env-cost --env pr-412 --comment # GitLab merge request note
169
+ ```
170
+
171
+ | Option | Effect |
172
+ | --- | --- |
173
+ | `--env <name>` | Environment name: lowercase letters, digits, `.`, `_` or `-`, up to 63. Also `IAC_COST_ENV` |
174
+ | `--format <text\|markdown\|json>` | `text` by default, `markdown` with `--comment` |
175
+ | `--comment` | Posts the Markdown as a note on the GitLab merge request of the pipeline and updates it on later runs, like [`iac-cost comment`](#gitlab-ci) (needs `GITLAB_TOKEN`). It has its own marker, so it never replaces the plan cost note. On GitHub Actions and Bitbucket Pipelines, post stdout with the snippets below, changing the marker they look for to `<!-- aaa-sentinel:iac-cost-env -->` |
176
+
177
+ - **The amounts are an estimate**, not the bill: the list price of each resource in the last daily
178
+ scan, prorated over its days alive, in your organization currency and marked `≈`. A resource
179
+ created after the last scan, or tagged since, shows up after the next one; a resource without a
180
+ known list price shows `—` and does not add to the total.
181
+ - The OAuth2 application in `IAC_COST_CLIENT_ID` needs the `leases:read` scope (Pro plan or above).
182
+ - It always exits `0`, so a report can never fail the environment pipeline. Only a missing or
183
+ invalid `--env` or `--format` exits `2`.
184
+
185
+ ## Owners from code
186
+
187
+ `iac-cost owners sync` tells AAA Sentinel which team owns what, from the files your repository
188
+ already has, and sends it to `POST /v1/owners/sync`. AAA Sentinel then splits the bill of the
189
+ closed month by team (chargeback by team) and estimates the current month per team. Business plan.
190
+
191
+ | Source | What the CLI reads | What it sends |
192
+ | --- | --- | --- |
193
+ | `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 |
194
+ | `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 |
195
+
196
+ ```bash
197
+ # Backstage: catalog-info.yaml files anywhere in the repository
198
+ npx --yes @aaa-sentinel/iac-cost owners sync
199
+
200
+ # CODEOWNERS + Terraform state of one root module
201
+ terraform -chdir=infra/payments show -json > state.json
202
+ npx --yes @aaa-sentinel/iac-cost owners sync --tf-state state.json --tf-dir infra/payments --no-backstage
203
+
204
+ # See what would change, and the conflicts with other sources, without writing
205
+ npx --yes @aaa-sentinel/iac-cost owners sync --tf-state state.json --tf-dir infra/payments --dry-run
206
+ ```
207
+
208
+ ```text
209
+ codeowners_tf acme/payments · infra/payments @ 4f1c2a9e0b7d (src_3f9a1c0d2b4e6f8a1c3e5d7b)
210
+ 0 rules, 214 resources · synced
211
+ added 12 · removed 3 · changed 0
212
+ 1 conflict with other sources (the oldest source wins):
213
+ conflict resource arn:aws:s3:::acme-logs: platform (src_9b8a7c6d5e4f3a2b1c0d9e8f) wins over payments (src_3f9a1c0d2b4e6f8a1c3e5d7b)
214
+ ```
215
+
216
+ | Option | Effect |
217
+ | --- | --- |
218
+ | `--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` |
219
+ | `--no-backstage` | Do not look for catalog files (a pipeline that only syncs a Terraform state) |
220
+ | `--tag-keys <k1,k2>` | Tag keys that carry the component name on your resources, up to 5. Default `service,app` |
221
+ | `--tf-state <file>` | `terraform show -json` output: the state, or a plan (its prior state is read) |
222
+ | `--tf-dir <dir>` | Root module directory, relative to the repository root. Default: the current directory |
223
+ | `--codeowners <file>` | Default: `.github/CODEOWNERS`, `CODEOWNERS`, `docs/CODEOWNERS` or `.gitlab/CODEOWNERS` |
224
+ | `--repo <owner/name>` | Default: `GITHUB_REPOSITORY`, `CI_PROJECT_PATH`, `BITBUCKET_REPO_FULL_NAME`, or the `origin` remote |
225
+ | `--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 |
226
+ | `--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 |
227
+ | `--dry-run` | Prints what would be added, removed or changed and the conflicts; writes nothing |
228
+ | `--json` | Prints the API answers as JSON |
229
+
230
+ - **Each repository and path is one source.** A sync replaces only that source; the rules set by
231
+ hand in the console and the other sources are never touched. Syncing the same content again
232
+ answers `unchanged` and writes nothing, so it can run on every push to the main branch.
233
+ - **Precedence:** the real `team` tag of a resource first, then the manual rules, then a full
234
+ resource id from any source (the oldest source first), then a short id, and last the tag rules
235
+ of the sources (the oldest first). The same resource or tag value claimed by two teams is a
236
+ conflict: the CLI prints it, and the Owners page and `GET /v1/owners` list them all.
237
+ - **What leaves your machine:** component names, tag keys, resource ids (ARN, id), Terraform
238
+ addresses and team names, with the repository, path, branch and commit. Never your code, the
239
+ state values or the plan, and never people: user owners, `@person` owners and emails are not
240
+ teams, and an id or address that contains `@` (an IAM user named by email, a `for_each` key) is
241
+ left out.
242
+ - **Nothing partial is sent.** If a catalog file is not valid YAML, the state cannot be read or
243
+ CODEOWNERS gives the root module no owner, that source is skipped with exit `1`: sending what is
244
+ left would delete the missing part. The same goes for a directory the CLI cannot read while
245
+ looking for catalog files. Limits per source: 2000 Backstage components, 10000
246
+ resources and a 5 MB request, checked before sending; 50 sources per organization.
247
+ - Two sources in one run (catalog files found and `--tf-state` given) are two calls, one per kind.
248
+ - The OAuth2 application in `IAC_COST_CLIENT_ID` needs the `owners:write` scope. Exit `0` when every
249
+ source synced or nothing changed, `1` when a source could not be read or sent, `2` for invalid
250
+ options or nothing to sync.
251
+
252
+ ### In the CI
253
+
254
+ GitHub Actions:
255
+
256
+ ```yaml
257
+ owners:
258
+ if: github.ref == 'refs/heads/main'
259
+ runs-on: ubuntu-latest
260
+ steps:
261
+ - uses: actions/checkout@v4
262
+ - uses: hashicorp/setup-terraform@v3
263
+ - run: terraform -chdir=infra/payments init -input=false && terraform -chdir=infra/payments show -json > state.json
264
+ - run: npx --yes @aaa-sentinel/iac-cost owners sync --tf-state state.json --tf-dir infra/payments
265
+ env:
266
+ IAC_COST_CLIENT_ID: ${{ secrets.AAA_SENTINEL_CLIENT_ID }}
267
+ IAC_COST_CLIENT_SECRET: ${{ secrets.AAA_SENTINEL_CLIENT_SECRET }}
268
+ ```
269
+
270
+ GitLab CI:
271
+
272
+ ```yaml
273
+ owners:
274
+ image: node:20
275
+ rules:
276
+ - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
277
+ script:
278
+ - npx --yes @aaa-sentinel/iac-cost owners sync
279
+ ```
280
+
281
+ Bitbucket Pipelines:
282
+
283
+ ```yaml
284
+ pipelines:
285
+ branches:
286
+ main:
287
+ - step:
288
+ name: Owners
289
+ image: node:20
290
+ script:
291
+ - npx --yes @aaa-sentinel/iac-cost owners sync
292
+ ```
293
+
294
+ On GitLab and Bitbucket, set `IAC_COST_CLIENT_ID` and `IAC_COST_CLIENT_SECRET` as masked or secured
295
+ repository variables.
296
+
153
297
  ## What actually leaves your machine
154
298
 
155
299
  Not the plan. `terraform show -json` output carries CIDRs, account identifiers and, often,