掃描多個帳戶
適合誰閱讀: 你管理不止一個 AWS 帳戶,想得到一份合併的報告。
讀完後你可以: 把帳戶列出一次,並在一次執行中全部掃描。
工作原理
InvenTag 讀取一份帳戶列表。對每個帳戶,它要麼從指定的 AWS CLI 設定檔(profile)獲取憑證, 要麼用你執行它時所用的身份去擔任一個 IAM 角色。它會掃描每個帳戶(預設最多同時掃描四個), 並寫出一份帶有 Account 欄的報告。
InvenTag 不會替你列出 AWS Organization 中的帳戶。這份列表需要你自己寫。
在每個帳戶中建立唯讀角色
請按照設定唯讀存取操作。
所有帳戶使用同一個角色名稱,例如 InvenTagReadOnly。
獲取帳戶列表
在管理帳戶或委派管理員帳戶中:
aws organizations list-accounts \
--query "Accounts[?Status=='ACTIVE'].[Id,Name]" --output text
未針對真實的 AWS 驗證:aws organizations list-accounts 命令及其列出的帳戶。
把列表寫成設定範本
inventag templates init org-multi-account --output .inventag/templates/our-org.yaml
編輯 accounts 部分。每個帳戶都需要它的 12 位 ID(要加引號),
以及 role_arn 或 profile_name 之一:
spec:
accounts:
- account_id: "123456789012"
account_name: Production
role_arn: arn:aws:iam::123456789012:role/InvenTagReadOnly
- account_id: "210987654321"
account_name: Development
profile_name: dev-readonly
regions: [us-east-1, eu-west-1]
先檢查,再執行:
inventag templates validate our-org
inventag --template our-org
validate 能發現常見的錯誤:沒有加引號的帳戶 ID、指向與 account_id 不同帳戶的角色 ARN,
以及檔案中的存取金鑰(範本中不允許)。如果仍保留著佔位符 ID,它會提示(範例資料):
WARNING ~/acme-infra/.inventag/templates/our-org.yaml: account IDs 111111111111, 222222222222, 333333333333 look like placeholders; replace them with your own
OK ~/acme-infra/.inventag/templates/our-org.yaml (config 'our-org')
針對三個範例帳戶的一次執行:
Wrote bom_output/tag_compliance_20261112_090000.json
Wrote bom_output/multi_account_bom_20261112_090000.docx
Wrote bom_output/multi_account_bom_20261112_090000.xlsx
Tag policy 'finops-cost-allocation': 0 of 39 resources compliant (0.0%), 39 non-compliant, 42 out of scope.
工作表: Summary、ATHENA、EVENTS、KMS、S3、VPC、Tag Compliance
Summary 工作表:
AWS Resource Inventory
Bill of materials generated by InvenTag
Generated: 2026-11-12 09:00 UTC
Accounts: 3 Regions: 1
| Service | Resources | Share |
|---|---|---|
| ATHENA | 3 | 3.7% |
| EVENTS | 3 | 3.7% |
| KMS | 42 | 51.9% |
| S3 | 3 | 3.7% |
| VPC | 30 | 37.0% |
| Total | 81 | 100.0% |
或者使用帳戶檔案
--accounts-file 選項同樣可用,並且它接受存取金鑰(如果可以,請避免使用)。
如果每個帳戶使用相同的角色名稱,就不需要逐個寫出 ARN:
inventag --accounts-file accounts.yaml --cross-account-role InvenTagReadOnly --create-excel
帳戶檔案的格式見設定檔。
inventag --accounts-file accounts.yaml --create-excel
調整執行
| 設定 | 設定範本欄位 | 引數 |
|---|---|---|
| 同時掃描的帳戶數(預設 4) | max_concurrent_accounts | --max-concurrent-accounts |
| 整個執行中同時在途的 AWS 請求數(預設 64,1 到 256) | concurrency | --concurrency |
| 每個帳戶允許的秒數(預設 1800,至少 60) | account_processing_timeout | --account-processing-timeout |
| 一次一個帳戶 | parallel_processing: false | --disable-parallel-processing |
InvenTag 平行檢查各帳戶的憑證,同時最多檢查 --max-concurrent-accounts 個。結果保持你的帳戶列表的順序。帶 mfa_serial 的設定檔需要輸入驗證碼,所以在連線了終端時,InvenTag 會先處理這類設定檔,並且一次處理一個。
--concurrency 的上限由所有帳戶共享。一個帳戶同時在途的請求從不超過 16 個。行和分析工作表按你的帳戶列表的順序排列,與帳戶完成的先後無關。見併發。
如果某個帳戶失敗,例如因為無法擔任它的角色,InvenTag 會記錄錯誤,並報告成功的那些帳戶。
它不會用你的預設憑證掃描該帳戶,除非這些憑證屬於該帳戶。Excel 報告的 Summary 工作表和 Word 報告都有「執行備註」(Run notes)部分,
列出每個未被掃描的帳戶及原因。日誌中有同樣的列表:
1 account(s) were not scanned, or only partly. The report does not include them:
- 222222222222 (Dev): Context establishment failed: Role assumption failed for account 222222222222: An error occurred (AccessDenied) when calling the AssumeRole operation: ...
執行仍然以結束代碼 0 結束。加上 --fail-on-account-error,當有帳戶無法掃描或超出時間限制時會以結束代碼 5 結束。報告仍會寫出。
如果所有帳戶都失敗,執行會以結束代碼 1 結束,並且不寫出任何報告。
超時的帳戶(--account-processing-timeout)算作失敗,但它在限制之前找到的資源仍留在報告中,備註會說明該帳戶不完整。
見時間限制與平行帳戶。
失敗之後繼續
InvenTag 會儲存已完成的帳戶。排除原因後,用同一條命令加上 --resume,只掃描其餘帳戶:
inventag --accounts-file accounts.yaml --create-excel --resume
InvenTag 會複用同一條命令(相同的帳戶、區域、服務和篩選條件)儲存的、不早於 --resume-max-age-hours(預設 24,最多 168)的結果。
見失敗的執行之後繼續。
一個會話在中途過期的帳戶不會被儲存,所以 --resume 會重新掃描它。
在 GitLab CI/CD 流水線中
未針對 GitLab Runner 或真實的 AWS 帳戶驗證:此流水線尚未在 GitLab 上執行。它的 YAML 和 inventag 命令列會在建置文件時檢查。它的作業已在本地用 gitlab-ci-local 4.77.1(用替身代替 rsync)針對模擬的 AWS 執行,使用的是虛擬權杖。
這個作業按流水線排程掃描 accounts.yaml 中的每個帳戶。它透過 OIDC 登入到一個角色,然後 InvenTag 在每個帳戶中擔任 InvenTagReadOnly 角色。該作業使用 --resume,所以快取加上一次 Retry 就能完成之前缺少的帳戶。accounts.yaml 會提交到儲存庫,因此它只能包含帳戶 ID 和名稱,不能包含存取金鑰。使用之前:
- 按照在 CI/CD 中執行 InvenTag中的說明,建立 GitLab 用來登入的角色和 CI/CD 變數
AWS_ROLE_ARN。該部分也說明了如何建立流水線排程。 - 給該角色授予對每個帳戶中
InvenTagReadOnly角色的sts:AssumeRole權限,並讓這些角色都信任它:見設定唯讀存取。策略檔案config/defaults/iam-policy-read-only.json中沒有sts:AssumeRole語句,所以需要你自己新增。
# .gitlab-ci.yml
stages:
- inventory
org-inventory:
stage: inventory
image: python:3.12
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
- if: $CI_PIPELINE_SOURCE == "web"
timeout: 3 hours # the ID token expires with the job
id_tokens:
GITLAB_OIDC_TOKEN:
aud: sts.amazonaws.com
variables:
AWS_DEFAULT_REGION: us-east-1
cache:
key: inventag-state
paths:
- bom_output/state/ # saved results of finished accounts, for --resume
when: always # save them when the job fails, too
script:
- pip install "inventag-aws==1.4.1" # pin a release
- export AWS_WEB_IDENTITY_TOKEN_FILE="$(mktemp)"
- printf '%s' "$GITLAB_OIDC_TOKEN" > "$AWS_WEB_IDENTITY_TOKEN_FILE"
- |
inventag --accounts-file accounts.yaml --cross-account-role InvenTagReadOnly \
--create-excel --resume --resume-max-age-hours 12 \
--fail-on-account-error --fail-if-empty
artifacts:
when: always # keep the reports of the accounts that worked
paths:
- bom_output/
expire_in: 30 days
所有 GitLab 範例共有的行,在在 CI/CD 中執行 InvenTag中說明。下表只說明這個範例特有的部分。
| 部分 | 作用 |
|---|---|
--accounts-file accounts.yaml --cross-account-role InvenTagReadOnly | 作業透過 OIDC 登入到一個角色。InvenTag 再從該角色出發,在檔案中的每個帳戶裡擔任 InvenTagReadOnly 角色。 |
cache:,帶 key: inventag-state、paths: - bom_output/state/、when: always | InvenTag 會把已完成的帳戶儲存在 bom_output/state/partial/(失敗的執行之後繼續)。新的作業從空資料夾開始,所以由快取把 bom_output/state/ 帶給下一個作業。when: always 讓作業失敗時也儲存快取;預設只在成功時儲存(GitLab:cache:when、GitLab:快取)。 |
--resume --resume-max-age-hours 12 | 執行失敗後,在作業上點選 Retry(或等待下一次執行):只會掃描缺少的帳戶。選擇 12 是有意的。若使用預設的 24 小時,下一個夜間執行可能會重用上一晚失敗那次執行的結果。沒有已儲存的結果時,--resume 不起任何作用。如果快取不在,就會重新掃描每個帳戶:更慢,但不會出錯。重用已儲存結果的規則見失敗的執行之後繼續。 |
--fail-on-account-error --fail-if-empty | 有帳戶無法掃描時,結束代碼為 5;沒有找到任何資源時,結束代碼為 6。其他帳戶的報告會被寫出,並由 artifacts: when: always 保留(GitLab:artifacts:when)。 |
timeout: 3 hours | 作業在 GitLab.com 的 GitLab 託管 Runner 上最長可以執行的時間(GitLab:託管 Runner)。 |
本地執行沒有覆蓋的部分:
- 未針對 GitLab 或 AWS 驗證:GitLab 簽發 ID 權杖,以及 AWS 為該角色接受它,包括信任策略中對
aud和sub的條件。 - 未針對 GitLab 驗證:
image、timeout、expire_in和cache(gitlab-ci-local 會忽略它們)。 - 未針對 GitLab 流水線排程驗證:本地執行時,流水線來源是手動設定的。
- 未針對 AWS 驗證:OIDC 角色在每個帳戶中擔任
InvenTagReadOnly角色,使用真實的信任策略。
檢查帳戶檔案
InvenTag 以 UTF-8 文字讀取帳戶檔案(有無位元組順序標記均可),檔案不能超過 1 MiB。出現以下情況時,它會以結束代碼 1 停止並指出問題:
- 檔案為空,或頂層是列表而不是一組
key: value; - 某個鍵寫了兩次(訊息會給出行號,例如
accounts file accounts.yaml:3: duplicate key 'accounts'); - 檔案型別不是
.json、.yaml或.yml; - 某個帳戶沒有非空的
account_id,或某個欄位的型別不對。
如果檔案中有 secret_access_key 或 session_token,並且其他使用者可以讀取它,InvenTag 會發出警告,提示你執行 chmod 600。
請使用 profile_name 或 role_arn,而不要使用金鑰。不掃描也能檢查檔案:
inventag --accounts-file accounts.yaml --validate-config-only --skip-credential-validation。
有兩種選項錯誤會讓執行以結束代碼 2 停止:--accounts-file 與 --accounts-prompt 同時給出,
以及 --cross-account-role 中的完整角色 ARN 與帳戶檔案同用(那裡應給角色名,或在檔案中寫 role_arn)。