Aller au contenu principal
Version : v1.2.2

Check tags against a policy

Who this is for: you own a tagging standard and want to know which resources break it.

What you can do after reading: run a tag check, read the results, and write your own policy.

Run a check​

Pick a built-in policy, or your own:

Shell
inventag --template finops-cost-allocation --create-excel --account-regions us-east-1

InvenTag scans as usual, then checks every resource in scope. Sample output from an offline run with --template finops-cost-report (sample data):

Output
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.docx
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.

Read the results​

WhereWhat you get
Excel, Tag Compliance sheetOne row per resource in scope: account, region, service, type, resource, ARN, status, missing tags and other problems. Non-compliant rows come first. Below the table, each rule with how many resources miss it or have a wrong value.
Excel, Summary sheetCounts: checked, compliant, non-compliant, out of scope, percentage.
Word, Tag compliance sectionThe same counts, the rules, and a table of non-compliant resources (up to 100 rows by default).
tag_compliance_<timestamp>.jsonCounts per rule and every non-compliant resource, for scripts and CI.

If no resource is in scope, the summary reads 0 of 0 resources compliant (100.0%): nothing was checked.

A resource is compliant when it has every required tag and every value passes its rule. Problems are spelled out, for example:

  • CostCenter: '1234' does not match CC-[0-9]{4} (wrong format)
  • Environment: 'Production' is not one of prod, staging, dev, test, sandbox
  • Owner: found 'owner' instead (tag keys are case-sensitive)

The Tag Compliance sheet in the same sample:

Sheets: Summary, ATHENA, DYNAMODB, EC2, EVENTS, IAM, KMS, LAMBDA, S3, VPC, Tag Compliance

Tag Compliance sheet, first rows:

AccountRegionServiceTypeResourceARNStatusMissing tagsProblems
123456789012us-east-1DYNAMODBTableacme-ordersarn:aws:dynamodb:us-east-1:123456789012:table...Non-compliantCostCenter, Project
123456789012us-east-1EC2Instanceacme-web-1arn:aws:ec2:us-east-1:123456789012:instance/i...Non-compliantCostCenter, Project
123456789012us-east-1EC2Volumeacme-build-cachearn:aws:ec2:us-east-1:123456789012:volume/vol...Non-compliantCostCenter, Owner, Project, Environment
123456789012globalIAMRoleacme-orders-api-rolearn:aws:iam::123456789012:role/acme-orders-ap...Non-compliantCostCenter, Owner, Project, Environment

The Word section:

Document outline:

  • Cost Allocation Tag Report (title)
  • Summary
  • Tag compliance
    • Required tags
    • Non-compliant resources
  • Resources by service
  • About this report

A table from the document:

Tag keyRuleMissingInvalid value
CostCenterMatches: CC-[0-9]{4}201
OwnerAny value170
ProjectAny value210
EnvironmentOne of: prod, staging, dev, test, sandbox (an...171

The summary part of the JSON file:

summary
{
"checked": 23,
"compliant": 1,
"non_compliant": 22,
"out_of_scope": 14,
"percent_compliant": 4.3,
"missing_by_key": {
"CostCenter": 20,
"Owner": 17,
"Project": 21,
"Environment": 17
},
"invalid_by_key": {
"CostCenter": 1,
"Owner": 0,
"Project": 0,
"Environment": 1
}
}

Out of scope resources are counted but not checked: the policy's scope excludes them, for example KMS aliases, which cannot carry tags.

Not verified against AWS: KMS aliases cannot carry tags.

Know this before you trust the numbers: InvenTag matches tags from the Resource Groups Tagging API to resources by ARN, and falls back to the service, region and resource ID when the ARN differs. Offline tests with moto check this for every service with its own discovery code. Real AWS may answer differently, so compare a few rows of the ARN column with the console before you rely on a policy result.

Write your own policy​

Start from a built-in one and change only what differs:

Shell
inventag templates init finops-cost-allocation --output .inventag/templates/our-tags.yaml --extend
YAML
format_version: 1
kind: tag-policy
name: our-tags
description: Our cost-allocation tags
extends: finops-cost-allocation
spec:
required_tags:
- key: Environment # replaces the inherited rule
allowed_values: [production, staging, development]
- key: Project # drops an inherited tag
remove: true
- key: DataClassification # adds a new one
allowed_values: [public, internal, confidential]
scope:
services: [EC2, S3, RDS, LAMBDA] # only check these services

Rules for each tag:

  • allowed_values: the value must be one of these. Case does not matter unless you set case_sensitive: true.
  • pattern: the whole value must match this regular expression.
  • Neither: any value is fine, as long as the tag exists.

Use either allowed_values or pattern for a tag, not both. All fields are in the template reference.

Fail a pipeline on violations​

Add a config template with fail_on_tag_violations: true. InvenTag still writes every report, then exits with code 3. If you also pass --fail-on-account-error or --fail-if-empty and one of them applies, the exit code is 5 or 6 instead, because the data is incomplete. See Enforce a tag policy in CI.

The older checker script​

scripts/legacy/tag_compliance_checker.py --config policy.yaml predates templates and has its own options (--help lists them). It only checks that the required tag keys exist. It does not enforce allowed_values, pattern or any other field of its policy file: it logs a warning and lists those fields in summary.ignored_policy_fields of its report. It ignores tags whose key starts with aws:, because AWS creates them, so a resource with only aws: tags counts as untagged, and a required tag key that starts with aws: is an error. Beyond that it is not covered by this documentation. Prefer tag-policy templates.

The legacy scripts in scripts/legacy/ run under the read-only guard and write owner-only files. aws_resource_inventory.py --s3-bucket uploads with SSE-S3 encryption. The tag checker upload is encrypted too. Both uploads are allowed only to the bucket you name; a write to any other bucket is blocked by the read-only guard.

Next steps​