跳到主要内容
版本:v1.2.0

Run InvenTag in CI/CD

Who this is for: you want an inventory on a schedule, or a pipeline that fails when resources are not tagged properly.

What you can do after reading: set up a GitHub Actions workflow that runs InvenTag with short-lived credentials and keeps the reports.

The pieces​

  1. Credentials without stored keys. Use OpenID Connect (OIDC): GitHub proves who the workflow is, and AWS gives it a temporary role session. No access keys live in your repository secrets.
  2. A config template with your regions, outputs and tag policy. The built-in ci-pipeline template is a good start.
  3. An artifact to keep bom_output/.

Let GitHub assume a read-only role​

In AWS, add GitHub as an OIDC identity provider (token.actions.githubusercontent.com) and create a role with the read-only policy from Set up read-only access. Limit its trust policy to your repository and branch. The AWS guide Use IAM roles to connect GitHub Actions to actions in AWS walks through it.

Add a config template to your repository​

Shell
inventag templates init ci-pipeline --output .inventag/templates/nightly.yaml

Set your regions and tag policy, then commit the file.

Add the workflow​

YAML
# .github/workflows/inventory.yml
name: AWS inventory
on:
schedule:
- cron: "17 2 * * 1-5" # 02:17 UTC on weekdays
workflow_dispatch:

permissions:
contents: read
id-token: write # needed for OIDC

jobs:
inventory:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.12"
- run: pip install "inventag-aws==1.2.0" # pin a release
- uses: aws-actions/configure-aws-credentials@<full-commit-SHA> # pin the current release
with:
role-to-assume: arn:aws:iam::123456789012:role/InvenTagGitHubReadOnly
aws-region: us-east-1
- name: Run InvenTag
run: inventag --template nightly --fail-on-account-error --fail-if-empty
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
if: always() # keep the reports even when the tag check fails
with:
name: inventory
path: bom_output/
retention-days: 30

Pin each action to a full commit SHA, with the version in a comment. That protects you if a tag is moved to malicious code. The SHAs above for checkout, setup-python and upload-artifact are the ones this repository's own workflows use. For aws-actions/configure-aws-credentials, look up the SHA of its current release on its GitHub releases page: we did not verify one for this page. Update the SHAs when you update the versions.

InvenTag runs from your repository's folder, so .inventag/templates/nightly.yaml is found by name.

Without the two exit-code flags, the job succeeds even when an account could not be scanned or nothing was found. --fail-on-account-error makes the job fail with exit code 5 in the first case, and --fail-if-empty with exit code 6 in the second. The reports are still written, so the artifact step can keep them.

What happens on a violation​

With fail_on_tag_violations: true, InvenTag writes every report first, then exits with code 3. The job fails, and the artifact still holds the Excel file and tag_compliance_<timestamp>.json with every non-compliant resource.

Sample output of inventag --template ci-pipeline from an offline run (sample data):

Terminal
inventag - WARNING - - No account specification method provided. Using default AWS credentials.
Wrote bom_output/tag_compliance_20261112_090000.json
Wrote bom_output/multi_account_bom_20261112_090000.xlsx
Tag policy 'finops-cost-allocation': 1 of 23 resources compliant (4.3%), 22 non-compliant, 14 out of scope.
Tag policy violations found and fail_on_tag_violations is true (exit code 3).
$ echo $?
3

Exit codes​

Exit codeMeaning
0Reports written, no tag violations (or no failing policy). Also when an account failed or nothing was found, unless you pass the flags for codes 5 and 6
1Error: invalid template or configuration file, credentials, every account failed, a report format that could not be written, or a failed S3 upload
2Invalid or conflicting options, for example --accounts-file with --accounts-prompt, or --compliance-standard (not implemented). Nothing is scanned
3Reports written, tag policy violations found (fail_on_tag_violations: true)
4Reports written, a security group rule open to the internet at or above --risk-threshold
5Reports written, an account could not be scanned or only partly (--fail-on-account-error)
6No resources to report (--fail-if-empty)

If more than one applies, 5 comes first, then 6, then 3, then 4, because the data behind a tag or risk result is incomplete when an account is missing. Reports are written for codes 3, 4 and 5. With code 6, a run with --template writes a report that lists no resources, and a run without it writes no file.

Keep it safe​

  • Avoid --debug in CI. Secrets are masked in debug logs, but the logs are long and still describe your accounts.
  • To keep reports in S3 instead of (or as well as) artifacts, add --s3-bucket and grant s3:PutObject on that bucket. See Upload reports to S3 on a schedule.
  • Keep the role read-only and scoped to the repository and branch that need it.
  • Pin InvenTag to a release (inventag-aws==1.2.0), not to the latest version. Raise the number when you choose to upgrade.

Next steps​