Find unmanaged resources and gate CI with --fail-on
tfcensus scan lists the live resources of your accounts, matches them against the state of every unit it discovers, and puts each resource in a category. This guide runs a scan, follows one finding back to its cause, and turns the scan into a CI gate.
Run a scan
Point scan at the repositories that hold your units and at the accounts they deploy to:
tfcensus scan ~/src/infra/acme-live --profile acme-nonprod --region eu-west-1Without paths, scan discovers units under discovery.roots, else the working directory. Without --region, it lists the regions that the units in each account target, so an account that no unit targets needs --region or --all-regions. The accounts guide covers --profile, --org and the include and exclude lists.
The output starts with one row per account and region, then lists the findings by category:
ACCOUNT REGION MANAGED DOUBLE-MANAGED UNMANAGED OTHER-IAC AWS-MANAGED DERIVED IGNORED UNDETERMINED COVERAGE
111111111111 global 1 1 2 0 1 0 0 0 50.0%
111111111111 eu-west-1 5 0 3 2 4 1 2 1 62.5%
111111111111 total 6 1 5 2 5 1 2 1 58.3%
Unmanaged (5)
AWS::EC2::SecurityGroup (1)
clickops-debug arn:aws:ec2:eu-west-1:111111111111:security-group/sg-0c11c0b5000000003 owner=alice 40d
AWS::ElasticLoadBalancingV2::ListenerRule (1)
- arn:aws:elasticloadbalancing:eu-west-1:111111111111:listener-rule/app/web/50dc6c495c0c9188/f2f7dc8efc522ab2/9683b2d02a6cabee - -
AWS::IAM::Role (1)
clickops-admin arn:aws:iam::111111111111:role/clickops-admin created-by=bob 89d
AWS::IAM::User (1)
sandbox-user arn:aws:iam::111111111111:user/sandbox-user owner=sandbox -
AWS::S3::Bucket (1)
acme-growth-experiments arn:aws:s3:::acme-growth-experiments - -
Double-managed (1)
AWS::IAM::Role arn:aws:iam::111111111111:role/acme-shared: tf-scan/iam (aws_iam_role.shared) and tf-scan/iam-dup (aws_iam_role.shared)
CloudFormation stacks (2)
billing: 2 resources
billing-AlarmsStack-1ABCDEF (nested in billing): 1 resource
Undetermined (1)
no type mapping for AWS::EC2::KeyPair: 1
warning: AWS::Lambda::Function arn:aws:lambda:eu-west-1:111111111111:function:acme-api is in a state and in CloudFormation stack billing as Api
warning: rule "growth-trial" is past its expires date and no longer matches
23 resources: 6 managed, 1 double-managed, 5 unmanaged, 2 other-iac, 5 aws-managed, 1 derived, 2 ignored, 1 undetermined; coverage 58.3%Coverage is managed and double-managed resources divided by those plus the unmanaged ones. Resources in the other categories don't count either way. The warnings at the end name rules past their expires date and resources that a state and a CloudFormation stack both list.
Narrow the scan
--typestakes CloudFormation or Terraform type globs, such as'AWS::IAM::*'or'aws_security_group*'. It is repeatable.--accountand--exclude-accountkeep or drop accounts.scanstill reads the state of units in a dropped account, so a resource managed from there still counts as managed.--filterpasses a terragrunt filter expression toterragrunt find, to select units.--layerspicks inventory layers. The default,auto, uses every layerdoctorfound.
A scan of many accounts is faster with an AWS Config aggregator and Resource Explorer views; see Speed up scans.
Find out why a resource is in its category
--explain takes one ARN or ID and prints how scan decided its category: the layers that listed it, the match against the states, every ownership rule it tried and the parent it inherited from:
tfcensus scan --explain arn:aws:iam::111111111111:role/clickops-adminThe trace for a policy of a service-linked role ends like this. The resource matched no rule itself, so it took the category of its parent role (rule inherit/aws-managed):
parent AWS::IAM::Role arn:aws:iam::111111111111:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS, aws-managed (pack/aws-service/service-linked-role)
category aws-managed, owner aws: child of AWS::IAM::Role arn:aws:iam::111111111111:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS, aws-managed (pack/aws-service/service-linked-role) (inherit/aws-managed)With --format json, .data.resource is the resource as rules see it. Save it and give it to tfcensus rules test --resource to try a new rule against it offline, as the rules guide shows.
--explain-coverage answers a different question: which layer listed each type in each account and region, which records a layer left out, and which AWS Config types tfcensus doesn't map. A type with no mapping is undetermined, never managed. The type coverage page lists every mapping.
Hide what you accept
The built-in rule packs already sort out default VPCs, service-linked roles, resources that CloudFormation, Pulumi or Kubernetes controllers own, and AWS-created children. What stays unmanaged and is fine, such as a vendor's integration role, gets a rule of your own with a reason and an optional expires date. Write and test ownership rules shows how.
Two scan settings change what the packs see:
scan.cloudformation(on by default) lists the resources of every CloudFormation stack. A resource a stack lists and no state manages isother-iacwith rulesource/cloudformation, and the finding names the stack. Turn it off to save thecloudformation:ListStacksandListStackResourcescalls.scan.external_dnssets the TXT registry naming of your external-dns deployments, so theexternal-dnsrule finds the records they own. Settxt_prefixandtxt_suffixto the values of external-dns's--txt-prefixand--txt-suffix; either may hold%{record_type}. When both are empty, tfcensus tries no prefix, thenexternal-dns-.
scan:
external_dns:
txt_prefix: "%{record_type}-extdns-"Write reports for CI
--format sets the format on stdout, and --out format=path writes more files from the same run. --out is repeatable:
tfcensus scan --profile acme-nonprod --out json=reports/scan.json --out sarif=reports/scan.sarif --out md=reports/scan.mdThe JSON follows the scan schema. SARIF goes to GitHub code scanning, and github writes workflow annotations. Markdown is cut at a row or section boundary once it reaches --md-max-chars (60,000 characters), under GitHub's comment limit, with a footer that says what was cut. Output formats describes each format.
Fail the job
--fail-on makes scan exit 3 when it finds resources in the categories you name: unmanaged, double-managed, or both. --min-count sets how many findings it takes:
tfcensus scan --profile acme-prod --types 'AWS::IAM::*' --fail-on unmanaged --fail-on double-managed --min-count 1The report is still written before the exit, so a CI step can upload it. The other exit codes tell a gate apart from a broken run:
| Code | Meaning |
|---|---|
| 0 | The scan finished, and nothing matched --fail-on. |
| 1 | A layer, a state read, a stack listing or discovery failed. |
| 3 | --fail-on matched at least --min-count findings. |
| 4 | Every failure was a credential problem. |
Every code is on the exit codes page. To publish the report as a Slack message, an issue or a pull request comment, pass the JSON to tfcensus notify (Publish reports). tfcensus report html turns it into a single HTML file (HTML report).
Run it again without AWS calls
scan caches the inventory and the state index for cache.ttl, one hour by default, keyed by the accounts, regions, layers, types, units, scan.cloudformation and whether state decryption is on. A second run with other rules or another format reads the cache and makes no AWS calls. --refresh lists again. In CI, tfcensus inventory --save-index writes the state index once, and scan --index reads it in place of the states. Caches explains what is kept.