Skip to main content

Terragrunt Python

terragrunt-python is a typed Python wrapper for running Terragrunt, OpenTofu, and Terraform executables. It keeps subprocess execution, executable discovery, version checks, capability checks, output handling, and common infrastructure commands in one small API.

The library is useful when Python code needs to orchestrate infrastructure commands without depending on an unstable Go or Terragrunt internal API.

Features

  • Select Terragrunt, OpenTofu, or Terraform with a typed Executable enum.
  • Resolve executables by name through PATH or use an explicit executable path.
  • Stream command output by default, with capture mode available per client or call.
  • Raise a TerragruntError for failed commands with the CommandResult in .resp.
  • Configure the working directory, environment, output streams, and timeout.
  • Check Terragrunt capabilities against the installed version.
  • Reject Terragrunt versions older than 0.73.7.
  • Warn when the installed Terragrunt version is newer than the tested version.
  • Run provider lock operations with platform and mirror options.
  • Use run(...) as an escape hatch for commands not yet represented by a typed method.

Prerequisites

Install the following tools before using the library:

Verify the tools you plan to use:

python --version
uv --version
terragrunt --version

Replace terragrunt --version with tofu --version or terraform --version when using another executable.

Installation

Install the package from a checked-out copy with uv:

uv sync

For an application that consumes this package, install it from the package index after it is published:

uv add terragrunt-python

The project does not currently have a configured Git remote. Once a remote is assigned, clone it with the repository's URL and then run uv sync.

Quick Start

The client resolves terragrunt through PATH, checks its version, and streams command output to the process's standard streams:

from terragrunt import TerragruntClient


client = TerragruntClient(working_dir="infrastructure/dev")
client.init()
client.plan()

Use an explicit executable path when the binary is not on PATH:

from pathlib import Path

from terragrunt import Executable, TerragruntClient


client = TerragruntClient(
    executable=Path("/opt/tools/terragrunt"),
    working_dir=Path("infrastructure/prod"),
)

Select OpenTofu or Terraform with the typed enum:

from terragrunt import Executable, TerragruntClient


tofu = TerragruntClient(executable=Executable.OPENTOFU)
terraform = TerragruntClient(executable=Executable.TERRAFORM)

tofu.plan()
terraform.output("-raw", "instance_ip")

The common typed methods are init, plan, apply, destroy, output, and providers_lock. Terragrunt-specific methods include render, list, find, stack_run, stack_generate, stack_output, stack_clean, exec_command, and dag_graph.

Output Handling

Commands stream output by default. Set the client-wide mode to capture output instead:

from terragrunt import OutputMode, TerragruntClient


client = TerragruntClient(output_mode=OutputMode.CAPTURE)
result = client.plan()

print(result.stdout)
print(result.stderr)

Override the mode for one command without changing the client default:

result = client.plan(output_mode=OutputMode.CAPTURE)

Every successful command returns a CommandResult containing args, returncode, stdout, and stderr.

Errors

Non-zero exit codes raise TerragruntError. The failed command result is available as error.resp:

from terragrunt import TerragruntClient, TerragruntError


try:
    TerragruntClient().plan()
except TerragruntError as error:
    if error.resp is not None:
        print(error.resp.returncode)
        print(error.resp.stderr)

Pass check=False when a caller needs to inspect a failure without raising:

result = client.run("validate", check=False)
if not result.succeeded:
    print(result.stderr)

Provider Locking

providers_lock supports the common provider lock options and accepts extra arguments for CLI features that are not yet modeled by the library:

from pathlib import Path


result = client.providers_lock(
    platforms=["darwin_arm64", "linux_amd64"],
    fs_mirror=Path("/opt/provider-mirror"),
    providers=["registry.terraform.io/hashicorp/aws"],
    extra_args=["-var-file=dev.tfvars"],
)

This produces the equivalent of:

terragrunt providers lock \
  -platform=darwin_arm64 \
  -platform=linux_amd64 \
  -fs-mirror=/opt/provider-mirror \
  registry.terraform.io/hashicorp/aws \
  -var-file=dev.tfvars

Compatibility

The initial compatibility baseline is Terragrunt 0.73.7.

  • Older Terragrunt versions raise TerragruntVersionError during client setup.
  • Newer Terragrunt versions emit a warning because they have not been tested.
  • OpenTofu and Terraform use their own versioning and are not subject to the Terragrunt 0.73.7 minimum.
  • run(...) remains available for commands that are not covered by a typed method.

Development

Install the locked development environment:

uv sync

Run the same checks used by GitHub Actions:

uv run ruff check .
uv run ruff format --check .
uv run ty check
uv run pytest

Tests use pytest and mirror the source package under tests/terragrunt/. Coverage is reported automatically by the pytest configuration.

Compatibility

Terragrunt compatibility is maintained through versioned CLI inventories, capability rules, and integration tests. See CONTRIBUTING.md for the release support workflow and docs/terragrunt-command-inventories.md for the inventory tooling reference.

Releases

Pushing a tag matching v*.*.* runs the checks and publishes the package to PyPI using GitHub Actions and PyPI trusted publishing. No PyPI token is stored in GitHub Actions.

Before the first release, configure a PyPI trusted publisher for this GitHub repository with:

  • Owner and repository name
  • Workflow filename: .github/workflows/publish.yml
  • Environment name: pypi

Then create and push a version tag after updating the version in pyproject.toml:

git tag v0.1.0
git push origin v0.1.0

Run the executable integration tests explicitly when Terragrunt, OpenTofu, or Terraform is installed locally:

uv run pytest -m integration

The integration tests skip executables that are not installed. Terragrunt-only tests also skip commands unavailable in the installed version. They use temporary directories with minimal configurations, disable backend access, and do not create or modify infrastructure. The tested Terragrunt command set includes rendering, discovery, DAG graph execution, and command forwarding.

Project Layout

src/terragrunt/       Library source
tests/terragrunt/     Mirrored pytest suite
pyproject.toml        Project metadata and tool configuration
.github/workflows/    GitHub Actions checks

Contributing

Bug reports, feature requests, and merge requests are welcome once the project remote and issue tracker are configured.

Before opening a merge request:

  1. Add or update tests for behavior changes.
  2. Run all four development checks locally.
  3. Update this README when public APIs or supported commands change.
  4. Keep command arguments as argument lists rather than shell strings.

License

This project is licensed under the MIT License.

Metadata

Release files for terragrunt-python 0.2.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 terragrunt-python 0.2.0
File Size Uploaded Provenance Actions
terragrunt_python-0.2.0.tar.gz 337.8 kB With provenance Details for terragrunt_python-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for terragrunt-python 0.2.0
File Interpreter ABI Platform Actions
terragrunt_python-0.2.0-py3-none-any.whl Python 3 none any Details for terragrunt_python-0.2.0-py3-none-any.whl

Total release size: 348.8 kB

Release files / terragrunt_python-0.2.0.tar.gz

Download URL terragrunt_python-0.2.0.tar.gz
Size 337.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4d72f6dbecf767f53f7c82e98f4537d0ef9cb2376d3cab69c70c73eb5273f60c
BLAKE2b-256 checksum
How to use checksums
d97c3c56a4e2861f58d2380dbf0e4ef261c65475f5e4dc4c3c16313f11eef6b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

Release files / terragrunt_python-0.2.0-py3-none-any.whl

Download URL terragrunt_python-0.2.0-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf6cc3be94a19204e553fa117dca029636eead708dd08f033b9ed221edb42f13
BLAKE2b-256 checksum
How to use checksums
18b7c510f82cfd6104f08ec864b15436b120ea104c5340afe3805c749f65de27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

Security for terragrunt-python 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for terragrunt_python-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

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