Skip to main content

Flatplan

Flatplan is a command line tool that can be used to flatten the resources and providers found inside a terraform plan or state.

You can obtain a JSON version of the plan or state by running:

terraform plan -out=planfile
terraform show -json planfile > plan.json

Now, we can feed our exported JSON plan to flatplan:

flatplan --path=plan.json --output=flattened_plan.json --debug

The problem we are trying to solve with flatplan is that, when you export the plan or state to JSON the resources might be in different locations which makes it hard for other tools to find them. Therefore, flatplan will extract all resources and providers for you and return a much simpler JSON structure.

For example, let's say we have a complex terraform project that uses many modules and submodules. When we create the plan file for this project and then export it to JSON we might end up with something like this:

{
    "planned_values": {
        "root_module": {
            "resources": [
                "... a lot of resources here ..."
            ],
            "child_modules": [{
                "resources": [
                    "... a lot of resources here ..."
                ],
                "child_modules": [{
                    "resources": [
                        "... a lot of resources here ..."
                    ],
                    "child_modules": [{
                        "... and so on ...": "..."
                    }]
                }]
            }, {
                "resources": [
                    "... a lot of resources here ..."
                ],
                "child_modules": [{
                    "resources": [
                        "... a lot of resources here ..."
                    ],
                    "child_modules": [{
                        "... and so on ...": "..."
                    }]
                }]
            }]
        }
    },

    "... some other things here ...": [],

    "configuration": {
        "provider_config": {
            "aws": {
                "name": "aws",
                "expressions": {
                    "region": {
                        "constant_value": "us-east-1"
                    }
                }
            }
        }
    }
}

If you are interested in this format you can check this Terraform documentation.

As you can see this recursive nature of the plan can get quite ugly if you use a lot of modules and submodules. After, flatplan process this file we will get:

{
    "resources": [ "... all resources here ..." ],
    "providers": [ "... all providers here ..." ]
}

This makes it easy for tools like Open Policy Agent to process our file since it has no way to recursively traverse a raw terraform plan.

Install

If you use pip:

pip install flatplan

If you use pipx:

pipx install flatplan

Usage

Flatplan accepts the following command line parameters:

--debug Sets log level to debug, default: False.

--output="path" Writes flattened plan to the specified path, default: STDOUT.

--path="path" Reads JSON plan from the specified path, default: STDIN.

--remove="tag=value" Removes the resources that contain a certain tag, default: empty

--state Whether the file passed in the path option is a state file instead of a plan file, default: false

Examples:

flatplan --debug --output=flattened.json --path=plan.json --remove="remove=true"

Alternatively, if you want to flatten a state:

flatplan --debug --output=flattened.json --path=state.json --remove="remove=true" --state

Release files for flatplan 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flatplan 1.3.0
File Size Uploaded
flatplan-1.3.0.tar.gz 39.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flatplan 1.3.0
File Interpreter ABI Platform
flatplan-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 62.6 kB

Release files / flatplan-1.3.0.tar.gz

Download URL flatplan-1.3.0.tar.gz
Size 39.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a5938aba5b9e4c1cbaa23eb1d8d438594f2037addbf53d2fa2a3692a317f211a
BLAKE2b-256 checksum
How to use checksums
8f20613a4f3710291e6445344bc469f36163cc258b2c0073de7287e2db92c5ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

Release files / flatplan-1.3.0-py3-none-any.whl

Download URL flatplan-1.3.0-py3-none-any.whl
Size 23.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c0622c98f43de51d2fd58f842883211bb5fc4174e20b6e77f3cfd2bc5e776a0a
BLAKE2b-256 checksum
How to use checksums
79ad94c61cc3d143699d5f0d10ef8193aed7ddd3e2ffe56e1bce4283362b95e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page