Skip to content

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:

text
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 tell

The 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 ​

sh
tfcensus scan [paths...] [flags]

Arguments ​

ArgumentRequiredRepeatable
pathsnoyes

Flags ​

FlagDescriptionTypeDefaultValuesConfig keyNotes
--accountkeep only this account ID (repeatable; config accounts.include)string, repeatableaccounts.include
--all-regionsuse every region each account has enabled, from account:ListRegions (config all_regions)boolall_regionsnot with --region
--concurrencystates read at the same timeint16
--decrypt-with-tofuread OpenTofu encrypted state with tofu init and tofu show -json in the unit, which downloads and runs its providers (config state.decrypt_with_tofu)boolstate.decrypt_with_tofu
--exclude-accountdrop this account ID (repeatable; config accounts.exclude)string, repeatableaccounts.exclude
--explainprint how the resource with this ARN or ID was categorizedstringnot with --explain-coverage
--explain-coverageprint which layer listed each type in each account and region, and the AWS Config types scan doesn't mapboolnot with --explain
--fail-onexit 3 when findings are in these categories: unmanaged or double-managedlist, repeatableunmanaged: no state manages it
double-managed: more than one state manages it
--filterterragrunt filter expression, passed to terragrunt find unchanged (repeatable)string, repeatable
--generate-stacksrun terragrunt stack generate first, so units in stacks are listed (config discovery.generate_stacks)booldiscovery.generate_stacks
--indexread the state index from this file (from inventory --save-index) instead of reading statesstring
--layersinventory layers: auto, or a comma-separated list of any of config, re, cc or native (config scan.layers)stringautoauto: every layer the capability probe allows
config: AWS Config
re: Resource Explorer
cc: Cloud Control, for allowlisted types and the types only it lists
native: every native lister
scan.layers
--management-profileprofile that lists the organization's accounts, else the default AWS credentials (config accounts.org.management_profile)stringaccounts.org.management_profile
--md-max-charscut Markdown output to this many characters at a row or section boundary; 0 means no limitint60000
--min-countfindings --fail-on needs before it exits 3int1
--orglist the accounts of the AWS organization and assume --org-role in each (config accounts.org.enabled)boolaccounts.org.enabled
--org-rolerole assumed in each member account (config accounts.org.role)stringOrganizationAccountAccessRoleaccounts.org.role
--outalso write the report to a file as format=path (repeatable); format is one of plain, md, json, csv, sarif or githubstring, repeatableplain: table without color
md: Markdown
json: JSON envelope for scripts
csv: comma-separated values
sarif: SARIF 2.1.0 for code scanning
github: GitHub Actions annotations
--profileAWS profile from ~/.aws/config, or a glob such as 'acme-*' (repeatable; config accounts.profiles)string, repeatableaccounts.profiles
--refreshlist and read states again even when the cache is freshbool
--regionAWS region (repeatable; config regions)string, repeatable34 values; shell completion lists themregionsnot with --all-regions
--renderwhen to use terragrunt render: auto, always or never (config discovery.render)stringautoauto: parse terragrunt.hcl, render the units it can't resolve
always: run terragrunt render for every unit
never: never run terragrunt render
discovery.render
--rulesYAML file with more ownership rules under rules: (repeatable)string, repeatable
--typesresource 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:

ValueMatches
unmanagedno state manages it
double-managedmore than one state manages it

Exit codes ​

CodeMeaning
0The command finished. With --fail-on, nothing matched.
1The command failed: a read, a write, a child process or a check failed.
2Wrong usage: an unknown command or flag, a bad value or argument, an invalid config file, or a prompt with no terminal.
3A --fail-on gate matched. The report is still written.
4Credentials were missing or rejected for every source the command needed.
130Interrupted by Ctrl-C (SIGINT) or a cancelled context.

All codes are listed on the exit codes page.

Examples ​

sh
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