tfcensus scan
scan lists the live resources of every selected account and region, matches them against the state of the discovered units, and puts each resource in one category:
managed matched to one state resource
double-managed matched in more than one state
unmanaged no state manages it and no ownership rule matches
other-iac owned by CloudFormation, CDK, Pulumi or a Kubernetes controller
aws-managed created by AWS, such as the default VPC
derived a child of another resource, such as a NAT gateway's network interface
ignored a user rule hides it
undetermined its type has no mapping, so tfcensus can't tellThe flow is: capability probe, discovery, state index, inventory layers, matching, CloudFormation stacks, then the ownership rules (the built-in packs, then rules from the config and --rules files, first match wins). A child that is still unmanaged or undetermined, such as a policy attachment of a service-linked role, then takes the category of an aws-managed, other-iac or derived parent (rule inherit/<category>); a child of a managed parent stays unmanaged. scan --explain <arn> prints how one resource was categorized, with its parent chain; --explain-coverage prints which layer listed each type in each account and region, the records a layer left out, and the AWS Config types scan doesn't map, with counts (config:GetDiscoveredResourceCounts).
Coverage is (managed + double-managed) / (managed + double-managed + unmanaged) per account. aws-managed, derived, other-iac, ignored and undetermined resources don't count.
Resources are listed in the --region regions, else the regions of the units that target the account. --all-regions lists every region the account has enabled (account:ListRegions). --account and --exclude-account choose the accounts to list; the state of units in other accounts is still read with their credentials.
With accounts.aggregator set, the config layer reads every account and region whose aggregator sources are current with one SelectAggregateResourceConfig query in the aggregator's account. An account and region the aggregator returns nothing for is queried in that account. doctor shows the aggregator's sources.
scan lists the resources of every CloudFormation stack (cloudformation:ListStacks and ListStackResources) unless scan.cloudformation is false. A resource a stack lists and no state manages is other-iac with rule source/cloudformation; the finding names the stack. A resource a state manages stays managed, and the report warns that a stack lists it too.
scan caches the inventory and state index under $XDG_CACHE_HOME/tfcensus/scan for cache.ttl (default 1h), keyed by the accounts, regions, layers, types, units, scan.cloudformation and state.decrypt_with_tofu. A re-run with other rules makes no AWS calls. --refresh lists again.
scan warns about each state that is still OpenTofu-encrypted, because a resource only it manages shows as unmanaged. --decrypt-with-tofu reads those states.
The exit code is 3 when --fail-on finds at least --min-count findings, 1 when a layer, a state read, a stack listing or discovery failed, 4 when every failure was a credential problem, and 0 otherwise.
Usage
tfcensus scan [paths...] [flags]Arguments
| Argument | Required | Repeatable |
|---|---|---|
paths | no | yes |
Flags
| Flag | Description | Type | Default | Values | Config key | Notes |
|---|---|---|---|---|---|---|
--account | keep only this account ID (repeatable; config accounts.include) | string, repeatable | accounts.include | |||
--all-regions | use every region each account has enabled, from account:ListRegions (config all_regions) | bool | all_regions | not with --region | ||
--concurrency | states read at the same time | int | 16 | |||
--decrypt-with-tofu | read OpenTofu encrypted state with tofu init and tofu show -json in the unit, which downloads and runs its providers (config state.decrypt_with_tofu) | bool | state.decrypt_with_tofu | |||
--exclude-account | drop this account ID (repeatable; config accounts.exclude) | string, repeatable | accounts.exclude | |||
--explain | print how the resource with this ARN or ID was categorized | string | not with --explain-coverage | |||
--explain-coverage | print which layer listed each type in each account and region, and the AWS Config types scan doesn't map | bool | not with --explain | |||
--fail-on | exit 3 when findings are in these categories: unmanaged or double-managed | list, repeatable | unmanaged: no state manages itdouble-managed: more than one state manages it | |||
--filter | terragrunt filter expression, passed to terragrunt find unchanged (repeatable) | string, repeatable | ||||
--generate-stacks | run terragrunt stack generate first, so units in stacks are listed (config discovery.generate_stacks) | bool | discovery.generate_stacks | |||
--index | read the state index from this file (from inventory --save-index) instead of reading states | string | ||||
--layers | inventory layers: auto, or a comma-separated list of any of config, re, cc or native (config scan.layers) | string | auto | auto: every layer the capability probe allowsconfig: AWS Configre: Resource Explorercc: Cloud Control, for allowlisted types and the types only it listsnative: every native lister | scan.layers | |
--management-profile | profile that lists the organization's accounts, else the default AWS credentials (config accounts.org.management_profile) | string | accounts.org.management_profile | |||
--md-max-chars | cut Markdown output to this many characters at a row or section boundary; 0 means no limit | int | 60000 | |||
--min-count | findings --fail-on needs before it exits 3 | int | 1 | |||
--org | list the accounts of the AWS organization and assume --org-role in each (config accounts.org.enabled) | bool | accounts.org.enabled | |||
--org-role | role assumed in each member account (config accounts.org.role) | string | OrganizationAccountAccessRole | accounts.org.role | ||
--out | also write the report to a file as format=path (repeatable); format is one of plain, md, json, csv, sarif or github | string, repeatable | plain: table without colormd: Markdownjson: JSON envelope for scriptscsv: comma-separated valuessarif: SARIF 2.1.0 for code scanninggithub: GitHub Actions annotations | |||
--profile | AWS profile from ~/.aws/config, or a glob such as 'acme-*' (repeatable; config accounts.profiles) | string, repeatable | accounts.profiles | |||
--refresh | list and read states again even when the cache is fresh | bool | ||||
--region | AWS region (repeatable; config regions) | string, repeatable | 34 values; shell completion lists them | regions | not with --all-regions | |
--render | when to use terragrunt render: auto, always or never (config discovery.render) | string | auto | auto: parse terragrunt.hcl, render the units it can't resolvealways: run terragrunt render for every unitnever: never run terragrunt render | discovery.render | |
--rules | YAML file with more ownership rules under rules: (repeatable) | string, repeatable | ||||
--types | resource types to scan, as CloudFormation or TF type globs such as 'AWS::IAM::*' or 'aws_security_group*' | list, repeatable |
The global flags work with every command.
Output formats
--format accepts tty, plain, md, json, csv, sarif, github. Without it, the format is tty when stdout is a terminal and plain otherwise. Formats are described on the global flags page.
Fail-on values
--fail-on makes the command exit 3 when the report has a match:
| Value | Matches |
|---|---|
unmanaged | no state manages it |
double-managed | more than one state manages it |
Exit codes
| Code | Meaning |
|---|---|
| 0 | The command finished. With --fail-on, nothing matched. |
| 1 | The command failed: a read, a write, a child process or a check failed. |
| 2 | Wrong usage: an unknown command or flag, a bad value or argument, an invalid config file, or a prompt with no terminal. |
| 3 | A --fail-on gate matched. The report is still written. |
| 4 | Credentials were missing or rejected for every source the command needed. |
| 130 | Interrupted by Ctrl-C (SIGINT) or a cancelled context. |
All codes are listed on the exit codes page.
Examples
tfcensus scan ~/src/infra/acme-live --profile acme-nonprod --region eu-west-1
tfcensus scan --types 'AWS::IAM::*' --fail-on unmanaged
tfcensus scan --rules extra-rules.yaml --format md > scan.md
tfcensus scan --explain arn:aws:ec2:eu-west-1:111111111111:security-group/sg-0123456789abcdef0