Skip to content

Python libterraform

Python libterraform logo

libterraform is a Python binding for Terraform. It bundles Terraform as a shared library and exposes Python APIs for running Terraform CLI commands and parsing Terraform configuration directories.

What It Provides

  • TerraformCommand for invoking Terraform commands from Python.
  • AsyncTerraformCommand for awaiting Terraform commands in asyncio applications without blocking the event loop.
  • TerraformConfig for loading .tf and .tf.json files into Terraform's internal module representation.
  • Wheels that include the Terraform shared library, so callers do not need a separate terraform executable on PATH.

Start Here

Version Comparison

libterraform Terraform
0.15.3 1.15.5
0.14.0 1.14.9
0.13.0 1.13.5
0.12.0 1.12.2
0.11.0 1.11.4
0.10.0 1.10.5
0.9.0 1.9.8
0.8.2 1.8.4
0.7.0 1.6.6
0.6.0 1.5.7
0.5.0 1.3.0
0.4.0 1.2.2
0.3.1 1.1.7

Runtime Constraints

TerraformCommand and AsyncTerraformCommand use process-isolated execution by default. Terraform still uses process-wide state such as the current working directory, stdio, checkpoint state, and plugin client cleanup, so running the CLI in a controlled worker process prevents that state from leaking into the caller process.

Use backend="thread" only when you explicitly want the current-process backend. That path is lower overhead and restores global state after each call, but other Python code in the same process can observe Terraform's temporary global-state changes while a command is running. Use TerraformPool to reuse worker processes or run independent Terraform operations in parallel.

Terraform operations can still affect real infrastructure, so use apply, destroy, state commands, imports, and tests with the same caution as the Terraform CLI.