Skip to main content

Terraform parallel execution for each workspace.

Project description

TFP

Test CodeQL Code Coverage Maintainability Dependabot Python versions PyPI - Downloads X URL

Terraform parallel execution for each workspace.

Advantage

  • Terraform has no built-in way to run plan across multiple workspaces/environments at once — you either run each one by hand or script your own orchestration. TFP parallelizes them with pytest-xdist, so a project with N environments takes roughly as long as its slowest environment instead of the sum of all of them.
  • terraform workspace select mutates state shared by every worker, so TFP serializes just that one step behind a cross-process file lock while still running the actual plan concurrently — real parallelism without workspace corruption.
  • Terraform's plan-log wording has shifted across versions (0.13 / 0.14 / 0.15 / 1.0.6+). TFP's report generator recognizes all of them, so upgrading Terraform doesn't silently break your summaries.
  • The bundled tfp-report command folds every environment's plan output into a single Markdown file — paste it into a PR description or CI job summary instead of linking to N separate logs.

Quickstart

pip install tfpcli

TFP assumes that Terraform is installed. We recommend to use tenv (see install-tenv.sh for a reference install script).

Describe your project's environments in tfp.yml in your working directory:

# You can define multiple terraform projects as dictionary.
# The key of dictionary is the value you pass to `tfp`, e.g. `tfp my-project`.
projects:
  my-project:
    # The directory to run `terraform plan` in.
    # This supports jinja to use a different directory per environment
    # (see the per-environment values example below).
    directory: infra
    # The command to select environment and prepare to plan.
    # This also supports jinja.
    command_select_environment: terraform workspace select {{ environment }}
    # The command to plan.
    # This also supports jinja.
    command_plan: terraform plan -detailed-exitcode -no-color
    # The dictionary of environments.
    # The key of each entry can be referenced from jinja (here, as `environment`).
    # You can add additional jinja parameters as the values of each entry.
    environments:
      dev: {}
      prod: {}

Then run:

tfp my-project

Each environment's terraform plan output is written to plan_logs/<environment>.log (here, plan_logs/dev.log and plan_logs/prod.log), and the command exits non-zero if any environment's plan errors out.

How do I...

How do I control the number of parallel workers?

Pass -n/--numprocesses, forwarded to pytest-xdist's -n. Defaults to "auto", which tfp resolves itself to min(environment count, CPU count) for the selected project — never more workers than there are environments to plan:

tfp my-project -n 4

How do I use per-environment values inside my Terraform commands?

directory, command_select_environment, and command_plan are all rendered as Jinja templates with each environment's dictionary as variables, so you can vary the working directory or commands per environment:

projects:
  my-project:
    # directory, command_select_environment, and command_plan are all rendered as jinja
    # templates with each environment's dictionary as variables, so you can vary the
    # working directory or commands per environment.
    directory: my-project/environments/{{ environment_name }}
    command_select_environment: make reconfigure
    command_plan: make plan
    environments:
      dev:
        environment_name: development
      stg:
        environment_name: staging
      prod:
        environment_name: production

How do I generate a single Markdown report from all environment plans?

tfp-report

This reads every *.log file under plan_logs/ and writes a combined report.md, one section per environment.

Credits

This package was created with Cookiecutter and the yukihiko-shinoda/cookiecutter-pypackage project template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tfpcli-0.1.0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tfpcli-0.1.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file tfpcli-0.1.0.tar.gz.

File metadata

  • Download URL: tfpcli-0.1.0.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for tfpcli-0.1.0.tar.gz
Algorithm Hash digest
SHA256 79d745c38973244d27066cede6d74e2af4ed8b3baab0f4d53ed1d7eb3435b21d
MD5 bc66cfd6da000f92181b786f88ec9251
BLAKE2b-256 30e1f38331e62e6d55d547044533228da7661f801610fd99e14c58b85cd7004e

See more details on using hashes here.

File details

Details for the file tfpcli-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tfpcli-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for tfpcli-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 171c8a26098d12760bd6b8d96d1092e3e21805f34688d541db876182e2d7e3a9
MD5 eeffaef46126810f41ba0b84aabf7e46
BLAKE2b-256 4275a35cee2f985682474ac1f04d4779e69b1310cc3fdcdad049bbac2fa08f20

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page