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
tfcensus codify --from scan.json --into <dir> [flags]Arguments
| Argument | Required | Repeatable |
|---|---|---|
dir | yes | no |
Flags
| Flag | Description | Type | Default | Values | Config key | Notes |
|---|---|---|---|---|---|---|
--force | overwrite files codify wrote before | bool | ||||
--from | scan report from tfcensus scan --format json, or - for stdin | string | required | |||
--import-by | import by auto or id; auto uses identity when the target can | string | auto | auto: identity when the target and the type allow it, else idid: always the id from the typemap template | ||
--into | directory of the unit to write the files into; it must exist | string | required | |||
--query | also write a .tfquery.hcl file with list blocks for terraform query | bool | ||||
--select | CEL expression over resource that picks the findings to codify | string | ||||
--target | engine and version the blocks are for, such as terraform@1.12.2 or opentofu@1.11.0 (default: detect) | string | ||||
--types | resource 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
| 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. |
| 130 | Interrupted by Ctrl-C (SIGINT) or a cancelled context. |
All codes are listed on the exit codes page.
Examples
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