Skip to content

tfcensus codify ​

codify reads the unmanaged findings of a scan report (tfcensus scan --format json) and writes import blocks into --into as imports_tfcensus_<YYYYMMDD>.tf, dated by the UTC clock. The blocks are a starting point for adoption: plan with -generate-config-out, review the generated config, and move it into your code. codify never runs plan or apply and writes nothing outside --into.

Each block imports by identity when the target can: terraform or OpenTofu 1.12 or later, an aws provider of 6.0 or later in the directory's .terraform.lock.hcl (or no lock file), and a type whose identity attributes the scan gives. Otherwise it imports by id, from the typemap's id template. A finding with neither is listed as skipped with the reason.

The target engine comes from the lock file's registry (registry.opentofu.org means OpenTofu), else from the --tf-path binary's name; its version from '<tf-path> version -json'. --target terraform@1.12.2 or opentofu@1.11.0 skips the detection. When detection fails, codify writes id imports.

Names come from the Name tag, then the resource's name, then the last part of its ID, as lowercase letters, digits and underscores. A name that is taken, in this run or by a resource or import block already in --into, gets _2, _3 and so on. A resource that an import block in --into already imports is skipped.

--query also writes query_tfcensus_<YYYYMMDD>.tfquery.hcl with one list block per type and region, for types that have a provider list resource without required list config. terraform query -generate-config-out turns it into config (terraform 1.14 or later; OpenTofu has no query).

--select takes a CEL expression over the same resource variable as ownership rules.

codify refuses to overwrite a file unless --force is set. The exit code is 2 for bad flags or input and 1 when a file can't be written.

Usage ​

sh
tfcensus codify --from scan.json --into <dir> [flags]

Arguments ​

ArgumentRequiredRepeatable
diryesno

Flags ​

FlagDescriptionTypeDefaultValuesConfig keyNotes
--forceoverwrite files codify wrote beforebool
--fromscan report from tfcensus scan --format json, or - for stdinstringrequired
--import-byimport by auto or id; auto uses identity when the target canstringautoauto: identity when the target and the type allow it, else id
id: always the id from the typemap template
--intodirectory of the unit to write the files into; it must existstringrequired
--queryalso write a .tfquery.hcl file with list blocks for terraform querybool
--selectCEL expression over resource that picks the findings to codifystring
--targetengine and version the blocks are for, such as terraform@1.12.2 or opentofu@1.11.0 (default: detect)string
--typesresource types to codify, as CloudFormation or TF type globs such as 'AWS::IAM::*' or 'aws_iam_role'list, repeatable

The global flags work with every command.

Output formats ​

--format accepts tty, plain, json. Without it, the format is tty when stdout is a terminal and plain otherwise. Formats are described on the global flags page.

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.
130Interrupted by Ctrl-C (SIGINT) or a cancelled context.

All codes are listed on the exit codes page.

Examples ​

sh
tfcensus scan --format json > scan.json
tfcensus codify --from scan.json --into live/prod/eu-west-1/network --types 'AWS::EC2::*'
tfcensus codify --from scan.json --into live/prod/iam --select 'resource.tags.?team.orValue("") == "platform"'
tfcensus codify --from scan.json --into iam --target terraform@1.14.0 --query